在 Ubuntu 上使用 CMake 和 Clion 构建 Boost 时出现未定义的引用错误

Undefined reference error while building Boost with CMake and Clion on Ubuntu

(undefinierter Verweis 是德语,意思是未定义的参考)

嗨,我正在使用 CLion 和 CMake 3.14,Ubuntu,BOoost 1.72.0 默认编译器 gcc/g++

我收到这个错误(删除 CMakeCache.txt 后):

我用

安装了 boost

sudo apt-get 安装 libboost-all-dev

AND/OR 和

./bootstap.sh ./b2 安装 --prefix="/home/sebastian/boost_1_72_0"

这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)
project(Sceduling)

set(CMAKE_CXX_STANDARD 14)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")


set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_NO_BOOST_CMAKE ON)

find_package(Boost COMPONENTS filesystem REQUIRED)

add_executable(Sceduling main.cpp account.cpp bank_t.cpp alternation.cpp transaction.c)
target_link_libraries(Sceduling PRIVATE Boost::filesystem)

这是结果:

====================[ Clean | Debug ]===========================================
/opt/clion-2019.1.4/bin/cmake/linux/bin/cmake --build /home/sebastian/Schreibtisch/git/ba-sebastian-dietl/Code/Bank_TM_Sceduling/cmake-build-debug --target clean -- -j 3
CMake Error: The current CMakeCache.txt directory /home/sebastian/Schreibtisch/CMakeCache.txt is different than the directory /home/sebastian/Schreibtisch/git/ba-sebastian-dietl/Code/Bank_TM_Sceduling/cmake-build-debug where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt

Clean finished

====================[ Build | all | Debug ]=====================================
/opt/clion-2019.1.4/bin/cmake/linux/bin/cmake --build /home/sebastian/Schreibtisch/git/ba-sebastian-dietl/Code/Bank_TM_Sceduling/cmake-build-debug --target all -- -j 3
CMake Error: The current CMakeCache.txt directory /home/sebastian/Schreibtisch/CMakeCache.txt is different than the directory /home/sebastian/Schreibtisch/git/ba-sebastian-dietl/Code/Bank_TM_Sceduling/cmake-build-debug where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at /opt/clion-2019.1.4/bin/cmake/linux/share/cmake-3.14/Modules/FindBoost.cmake:905 (message):
  New Boost version may have incorrect or missing dependencies and imported
  targets
Call Stack (most recent call first):
  /opt/clion-2019.1.4/bin/cmake/linux/share/cmake-3.14/Modules/FindBoost.cmake:1027 (_Boost_COMPONENT_DEPENDENCIES)
  /opt/clion-2019.1.4/bin/cmake/linux/share/cmake-3.14/Modules/FindBoost.cmake:1709 (_Boost_MISSING_DEPENDENCIES)
  CMakeLists.txt:15 (find_package)


-- Boost version: 1.72.0
-- Found the following Boost libraries:
--   filesystem
-- Configuring done
-- Generating done
-- Build files have been written to: /home/sebastian/Schreibtisch/git/ba-sebastian-dietl/Code/Bank_TM_Sceduling/cmake-build-debug
Scanning dependencies of target Sceduling
[ 16%] Building CXX object CMakeFiles/Sceduling.dir/main.cpp.o
[ 33%] Building CXX object CMakeFiles/Sceduling.dir/bank_t.cpp.o
[ 50%] Building CXX object CMakeFiles/Sceduling.dir/account.cpp.o
[ 66%] Building CXX object CMakeFiles/Sceduling.dir/alternation.cpp.o
[ 83%] Building C object CMakeFiles/Sceduling.dir/transaction.c.o
[100%] Linking CXX executable Sceduling
CMakeFiles/Sceduling.dir/main.cpp.o: In Funktion »boost::context::fiber::~fiber()«:
main.cpp:(.text._ZN5boost7context5fiberD2Ev[_ZN5boost7context5fiberD5Ev]+0x5b): Warnung: undefinierter Verweis auf »ontop_fcontext«
CMakeFiles/Sceduling.dir/main.cpp.o: In Funktion »boost::context::fiber::resume() &&«:
main.cpp:(.text._ZNO5boost7context5fiber6resumeEv[_ZNO5boost7context5fiber6resumeEv]+0x6e): Warnung: undefinierter Verweis auf »jump_fcontext«
CMakeFiles/Sceduling.dir/main.cpp.o: In Funktion »boost::fibers::context::context(unsigned long, boost::fibers::type, boost::fibers::launch)«:
main.cpp:(.text._ZN5boost6fibers7contextC2EmNS0_4typeENS0_6launchE[_ZN5boost6fibers7contextC5EmNS0_4typeENS0_6launchE]+0x1a): Warnung: undefinierter Verweis auf »vtable for boost::fibers::context«
CMakeFiles/Sceduling.dir/main.cpp.o: In Funktion »boost::fibers::fiber::fiber<boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>, , void, void, void>(boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>&&)«:
main.cpp:(.text._ZN5boost6fibers5fiberC2INS_3_bi6bind_tIvPFvvENS3_5list0EEEJEvvvEEOT_DpT0_[_ZN5boost6fibers5fiberC5INS_3_bi6bind_tIvPFvvENS3_5list0EEEJEvvvEEOT_DpT0_]+0x21): Warnung: undefinierter Verweis auf »boost::context::stack_traits::default_size()«
CMakeFiles/Sceduling.dir/main.cpp.o: In Funktion »boost::fibers::fiber::fiber<boost::context::basic_fixedsize_stack<boost::context::stack_traits>, boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>>(boost::fibers::launch, std::allocator_arg_t, boost::context::basic_fixedsize_stack<boost::context::stack_traits>&&, boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>&&)«:
main.cpp:(.text._ZN5boost6fibers5fiberC2INS_7context21basic_fixedsize_stackINS3_12stack_traitsEEENS_3_bi6bind_tIvPFvvENS7_5list0EEEJEEENS0_6launchESt15allocator_arg_tOT_OT0_DpT1_[_ZN5boost6fibers5fiberC5INS_7context21basic_fixedsize_stackINS3_12stack_traitsEEENS_3_bi6bind_tIvPFvvENS7_5list0EEEJEEENS0_6launchESt15allocator_arg_tOT_OT0_DpT1_]+0x5f): Warnung: undefinierter Verweis auf »boost::fibers::fiber::start_()«
CMakeFiles/Sceduling.dir/main.cpp.o: In Funktion »boost::fibers::worker_context<boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>>::worker_context<boost::context::basic_fixedsize_stack<boost::context::stack_traits> >(boost::fibers::launch, boost::context::preallocated const&, boost::context::basic_fixedsize_stack<boost::context::stack_traits>&&, boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>&&)«:
main.cpp:(.text._ZN5boost6fibers14worker_contextINS_3_bi6bind_tIvPFvvENS2_5list0EEEJEEC2INS_7context21basic_fixedsize_stackINSA_12stack_traitsEEEEENS0_6launchERKNSA_12preallocatedEOT_OS7_[_ZN5boost6fibers14worker_contextINS_3_bi6bind_tIvPFvvENS2_5list0EEEJEEC5INS_7context21basic_fixedsize_stackINSA_12stack_traitsEEEEENS0_6launchERKNSA_12preallocatedEOT_OS7_]+0x12f): Warnung: undefinierter Verweis auf »boost::fibers::context::~context()«
CMakeFiles/Sceduling.dir/main.cpp.o: In Funktion »boost::fibers::worker_context<boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>>::run_(boost::context::fiber&&)«:
main.cpp:(.text._ZN5boost6fibers14worker_contextINS_3_bi6bind_tIvPFvvENS2_5list0EEEJEE4run_EONS_7context5fiberE[_ZN5boost6fibers14worker_contextINS_3_bi6bind_tIvPFvvENS2_5list0EEEJEE4run_EONS_7context5fiberE]+0x94): Warnung: undefinierter Verweis auf »boost::fibers::context::terminate()«
CMakeFiles/Sceduling.dir/main.cpp.o: In Funktion »void* boost::context::detail::create_fiber2<boost::context::detail::fiber_record<boost::context::fiber, boost::context::basic_fixedsize_stack<boost::context::stack_traits>, std::_Bind<boost::context::fiber (boost::fibers::worker_context<boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>>::*(boost::fibers::worker_context<boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>>*, std::_Placeholder<1>))(boost::context::fiber&&)> >, boost::context::basic_fixedsize_stack<boost::context::stack_traits>, std::_Bind<boost::context::fiber (boost::fibers::worker_context<boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>>::*(boost::fibers::worker_context<boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>>*, std::_Placeholder<1>))(boost::context::fiber&&)> >(boost::context::preallocated, boost::context::basic_fixedsize_stack<boost::context::stack_traits>&&, std::_Bind<boost::context::fiber (boost::fibers::worker_context<boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>>::*(boost::fibers::worker_context<boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>>*, std::_Placeholder<1>))(boost::context::fiber&&)>&&)«:
main.cpp:(.text._ZN5boost7context6detail13create_fiber2INS1_12fiber_recordINS0_5fiberENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFMNS_6fibers14worker_contextINS_3_bi6bind_tIvPFvvENSB_5list0EEEJEEEFS4_OS4_EPSH_St12_PlaceholderILi1EEEEEES7_SP_EEPvNS0_12preallocatedEOT0_OT1_[_ZN5boost7context6detail13create_fiber2INS1_12fiber_recordINS0_5fiberENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFMNS_6fibers14worker_contextINS_3_bi6bind_tIvPFvvENSB_5list0EEEJEEEFS4_OS4_EPSH_St12_PlaceholderILi1EEEEEES7_SP_EEPvNS0_12preallocatedEOT0_OT1_]+0xea): Warnung: undefinierter Verweis auf »make_fcontext«
main.cpp:(.text._ZN5boost7context6detail13create_fiber2INS1_12fiber_recordINS0_5fiberENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFMNS_6fibers14worker_contextINS_3_bi6bind_tIvPFvvENSB_5list0EEEJEEEFS4_OS4_EPSH_St12_PlaceholderILi1EEEEEES7_SP_EEPvNS0_12preallocatedEOT0_OT1_[_ZN5boost7context6detail13create_fiber2INS1_12fiber_recordINS0_5fiberENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFMNS_6fibers14worker_contextINS_3_bi6bind_tIvPFvvENSB_5list0EEEJEEEFS4_OS4_EPSH_St12_PlaceholderILi1EEEEEES7_SP_EEPvNS0_12preallocatedEOT0_OT1_]+0x127): Warnung: undefinierter Verweis auf »jump_fcontext«
CMakeFiles/Sceduling.dir/main.cpp.o: In Funktion »void boost::context::detail::fiber_entry<boost::context::detail::fiber_record<boost::context::fiber, boost::context::basic_fixedsize_stack<boost::context::stack_traits>, std::_Bind<boost::context::fiber (boost::fibers::worker_context<boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>>::*(boost::fibers::worker_context<boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>>*, std::_Placeholder<1>))(boost::context::fiber&&)> > >(boost::context::detail::transfer_t)«:
main.cpp:(.text._ZN5boost7context6detail11fiber_entryINS1_12fiber_recordINS0_5fiberENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFMNS_6fibers14worker_contextINS_3_bi6bind_tIvPFvvENSB_5list0EEEJEEEFS4_OS4_EPSH_St12_PlaceholderILi1EEEEEEEEvNS1_10transfer_tE[_ZN5boost7context6detail11fiber_entryINS1_12fiber_recordINS0_5fiberENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFMNS_6fibers14worker_contextINS_3_bi6bind_tIvPFvvENSB_5list0EEEJEEEFS4_OS4_EPSH_St12_PlaceholderILi1EEEEEEEEvNS1_10transfer_tE]+0x7c): Warnung: undefinierter Verweis auf »jump_fcontext«
main.cpp:(.text._ZN5boost7context6detail11fiber_entryINS1_12fiber_recordINS0_5fiberENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFMNS_6fibers14worker_contextINS_3_bi6bind_tIvPFvvENSB_5list0EEEJEEEFS4_OS4_EPSH_St12_PlaceholderILi1EEEEEEEEvNS1_10transfer_tE[_ZN5boost7context6detail11fiber_entryINS1_12fiber_recordINS0_5fiberENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFMNS_6fibers14worker_contextINS_3_bi6bind_tIvPFvvENSB_5list0EEEJEEEFS4_OS4_EPSH_St12_PlaceholderILi1EEEEEEEEvNS1_10transfer_tE]+0xdd): Warnung: undefinierter Verweis auf »ontop_fcontext«
CMakeFiles/Sceduling.dir/main.cpp.o: In Funktion »boost::fibers::worker_context<boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>>::~worker_context()«:
main.cpp:(.text._ZN5boost6fibers14worker_contextINS_3_bi6bind_tIvPFvvENS2_5list0EEEJEED2Ev[_ZN5boost6fibers14worker_contextINS_3_bi6bind_tIvPFvvENS2_5list0EEEJEED5Ev]+0x22): Warnung: undefinierter Verweis auf »boost::fibers::context::~context()«
CMakeFiles/Sceduling.dir/main.cpp.o:(.data.rel.ro._ZTIN5boost6fibers14worker_contextINS_3_bi6bind_tIvPFvvENS2_5list0EEEJEEE[_ZTIN5boost6fibers14worker_contextINS_3_bi6bind_tIvPFvvENS2_5list0EEEJEEE]+0x10): Warnung: undefinierter Verweis auf »typeinfo for boost::fibers::context«
collect2: error: ld returned 1 exit status
CMakeFiles/Sceduling.dir/build.make:144: recipe for target 'Sceduling' failed
make[2]: *** [Sceduling] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/Sceduling.dir/all' failed
make[1]: *** [CMakeFiles/Sceduling.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

更新:构建工作正常,如果我不使用光纤,程序将以退出代码 0 结束。如果我使用光纤,我将完成进程,退出代码为 135(被信号 7 中断:SIGEMT)

这是我的代码:

#include <iostream>
#include <boost/fiber/all.hpp>                                                                      

using namespace std;
using namespace boost;

inline
void fn() {
    cout << "Hello World" << endl;
}

int main() {
    fibers::fiber f1(fn);
    f1.join();
    return 0;
}

如果可以使用 Ubuntu Boost 包,请使用以下命令安装它:

sudo apt-get install libboost-all-dev

那么您的 CMakeLists.txt 文件应该如下所示:

set(CMAKE_CXX_STANDARD 11)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_NO_BOOST_CMAKE ON)

find_package(Boost COMPONENTS fiber system filesystem REQUIRED)

add_executable(Sceduling main.cpp account.cpp bank_t.cpp alternation.cpp transaction.c)
target_link_libraries(Sceduling PRIVATE Boost::headers Boost::fiber Boost::system Boost::filesystem)

否则,如果您打算自己构建 boost 并将其安装在自定义文件夹中,您的 CMakeLists.txt 文件应如下所示:

set(CMAKE_CXX_STANDARD 11)
set(Boost_ADDITIONAL_VERSIONS 1.72.0 1.72)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_NO_SYSTEM_PATHS ON)
set(Boost_NO_BOOST_CMAKE ON)
set(Boost_ROOT <mydir>)
set(Boost_INCLUDEDIR <a_directory_with_include_files>)
set(Boost_LIBRARYDIR <a_directory_with_library_files>)

find_package(Boost 1.72.0 COMPONENTS fiber system filesystem REQUIRED)

add_executable(Sceduling main.cpp account.cpp bank_t.cpp alternation.cpp transaction.c)
target_link_libraries(Sceduling PRIVATE Boost::headers Boost::fiber Boost::system Boost::filesystem)

其中 1.72.0 是您之前构建并安装到 <mydir>Boost 的版本。如果安装将包含文件和库放在单独的目录下,您需要在变量 Boost_INCLUDEDIRBoost_LIBRARYDIR.

中引用它们
  • 您的 CMake (3.14) 很旧,它的 FindBoost 模块现在与 Boost (1.72.0) 的最新版本无关。这就是为什么您可能需要设置 Boost_ADDITIONAL_VERSIONS 变量。

  • 无需检查 Boost_FOUND,因为如果找不到 BoostREQUIRED 关键字会使 CMake 失败。

  • 你的目标叫 Sceduling 而不是 executable

  • 您至少需要启用 C++11 才能 Boost::fiber 工作。

有关设置的更多说明,请参阅 FindBoost CMake 文档。