[英]Why can't I link to boost's thread libraries?
我使用的是Mac OS 10.11,並且用作我的C / C ++編譯器。 我用最簡單的brew install boost
方式brew 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;
}
它可以正常工作。 所以似乎正確設置了提升。
但是何時嘗試使用#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();
}
然后我得到了這些
/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
我谷歌這個問題一段時間了。 我試圖設置庫目錄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})
我也試過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)
誰能給我一些幫助? 非常感謝你!
更新:使用findApackage命令@Angew說,我收到了一些新錯誤。
/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
UPDATE
我嘗試了一些老版本的提升,但同樣的。 如果我使用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
UPDATE
@Angew找到了真正的答案。
我再次更新了答案。 似乎Boost.Thread也要求你使用Boost.System。 這將由導入的目標處理,但由於您無法使用它們,您顯然必須明確請求它。
find_package(Boost REQUIRED COMPONENTS thread system)
使其工作正常。
與第三方軟件包接口的正確CMake方法是使用find_package()
調用,該調用將使用find模塊(CMake附帶)或軟件包配置文件(由第三方軟件包提供)來定位必要的圖書館,目錄等
在Boost的情況下,CMake本身附帶了一個find模塊。 它提供了一個非常可調的界面,您可以閱讀其文檔以獲取詳細信息。 但是,這是您使用它的最基本方法:
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)
基於此消息,您收到:
導入的目標不適用於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})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.