5 c++ multithreading boost cmake
我使用 Mac OS 10.11,clang 作为我的 C/C++ 编译器。我用最简单的方法安装了 boostbrew install boost
首先我尝试了这些代码
#include <iostream>
#include <boost/array.hpp>
int main() {
boost::array<int, 4> arr = {1, 2, 3, 4};
std::cout << arr[3];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它可以正常工作。所以看起来 boost 设置正确。
但是什么时候尝试使用#include<boost/thread.hpp>
和示例代码如下所示:
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>
void wait(int seconds)
{
boost::this_thread::sleep_for(boost::chrono::seconds{seconds});
}
void thread()
{
for (int i = 0; i < 5; ++i)
{
wait(1);
std::cout << i << '\n';
}
}
int main()
{
boost::thread t{thread};
t.join();
}
Run Code Online (Sandbox Code Playgroud)
然后我得到了这些
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/vita-nove/CLionProjects/untitled/cmake-build-debug --target untitled -- -j 4
[ 50%] Linking CXX executable untitled
Undefined symbols for architecture x86_64:
"boost::this_thread::hidden::sleep_for(timespec const&)", referenced from:
boost::this_thread::sleep_for(boost::chrono::duration<long long, boost::ratio<1l, 1000000000l> > const&) in main.cpp.o
"boost::detail::thread_data_base::~thread_data_base()", referenced from:
boost::detail::thread_data<void (*)()>::~thread_data() in main.cpp.o
"boost::system::system_category()", referenced from:
boost::thread_exception::thread_exception(int, char const*) in main.cpp.o
___cxx_global_var_init.2 in main.cpp.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in main.cpp.o
___cxx_global_var_init.1 in main.cpp.o
"boost::thread::join_noexcept()", referenced from:
boost::thread::join() in main.cpp.o
"boost::thread::native_handle()", referenced from:
boost::thread::get_id() const in main.cpp.o
"boost::thread::start_thread_noexcept()", referenced from:
boost::thread::start_thread() in main.cpp.o
"boost::thread::detach()", referenced from:
boost::thread::~thread() in main.cpp.o
"typeinfo for boost::detail::thread_data_base", referenced from:
typeinfo for boost::detail::thread_data<void (*)()> in main.cpp.o
"vtable for boost::detail::thread_data_base", referenced from:
boost::detail::thread_data_base::thread_data_base() in main.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2
Run Code Online (Sandbox Code Playgroud)
我谷歌了一段时间这个问题。我试图设置 library dir set(Boost_LIBRARY_DIRS /usr/local/lib)
link_directories(${Boost_LIBRARY_DIRS})
。
我的 CMakeLists.txt 在这里:
cmake_minimum_required(VERSION 3.7)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
add_executable(untitled main.cpp)
set(Boost_LIBRARY_DIRS /usr/local/lib)
link_directories(${Boost_LIBRARY_DIRS})
Run Code Online (Sandbox Code Playgroud)
我也试过了clang++ main.cpp -lboost_thread-mt
。然后得到了这些:
vita-nove@MacBook-Pro:~/CLionProjects/untitled$ clang++ main.cpp -lboost_thread-mt
Undefined symbols for architecture x86_64:
"boost::system::system_category()", referenced from:
boost::thread_exception::thread_exception(int, char const*) in main-964777.o
___cxx_global_var_init.2 in main-964777.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in main-964777.o
___cxx_global_var_init.1 in main-964777.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
任何人都可以给我一些帮助吗?非常感谢!
更新:使用@Angew 所说的 find_package 命令,我遇到了一些新错误。
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/vita-nove/CLionProjects/untitled/cmake-build-debug --target untitled -- -j 4
CMake Warning at /Applications/CLion.app/Contents/bin/cmake/share/cmake-3.7/Modules/FindBoost.cmake:744 (message):
Imported targets not available for Boost version 106300
Call Stack (most recent call first):
/Applications/CLion.app/Contents/bin/cmake/share/cmake-3.7/Modules/FindBoost.cmake:848 (_Boost_COMPONENT_DEPENDENCIES)
/Applications/CLion.app/Contents/bin/cmake/share/cmake-3.7/Modules/FindBoost.cmake:1435 (_Boost_MISSING_DEPENDENCIES)
CMakeLists.txt:9 (find_package)
-- Boost version: 1.63.0
-- Found the following Boost libraries:
-- thread
-- Configuring done
CMake Error at CMakeLists.txt:7 (add_executable):
Target "untitled" links to target "Boost::thread" but the target was not
found. Perhaps a find_package() call is missing for an IMPORTED target, or
an ALIAS target is missing?
-- Generating done
-- Build files have been written to: /Users/vita-nove/CLionProjects/untitled/cmake-build-debug
make: *** [cmake_check_build_system] Error 1
Run Code Online (Sandbox Code Playgroud)
更新
我尝试了一些老版本的 boost 但还是一样。如果我使用target_include_directories(untitled PRIVATE ${Boost_INCLUDE_DIRS})
和target_link_libraries(untitled ${Boost_LIBRARIES})
Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
Undefined symbols for architecture x86_64:
"boost::system::system_category()", referenced from:
boost::thread_exception::thread_exception(int, char const*) in main.cpp.o
___cxx_global_var_init.2 in main.cpp.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in main.cpp.o
___cxx_global_var_init.1 in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [untitled] Error 1
make[1]: *** [CMakeFiles/untitled.dir/all] Error 2
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)
更新
@Angew 找到了真正的答案。
我又更新了答案。Boost.Thread 似乎也要求您使用 Boost.System。这将由导入的目标处理,但由于您无法使用这些目标,因此您显然必须明确请求它。
find_package(Boost REQUIRED COMPONENTS thread system)
使它工作正常。
与第三方包交互的正确 CMake 方法是使用调用find_package()
,该调用将使用查找模块(随 CMake 附带)或包配置文件(由第三方包附带)来查找必要的库、目录等
对于 Boost,CMake 本身附带了一个查找模块。它提供了一个非常可调的接口,您可以阅读其文档以了解详细信息。但是,这是使用它的最基本方法:
cmake_minimum_required(VERSION 3.7)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
find_package(Boost REQUIRED COMPONENTS thread)
add_executable(untitled main.cpp)
target_link_libraries(untitled Boost::thread)
Run Code Online (Sandbox Code Playgroud)
根据您收到的此消息:
导入的目标不适用于 Boost 版本 106300
看来导入目标方法不能与您的 Boost 版本一起使用。因此,您将不得不使用 find 模块提供的变量:
cmake_minimum_required(VERSION 3.7)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
find_package(Boost REQUIRED COMPONENTS thread system)
add_executable(untitled main.cpp)
target_include_directories(untitled PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(untitled ${Boost_LIBRARIES})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3887 次 |
最近记录: |