2017-04-12 1 views
3

Ich verwende Mac OS 10.11 und klingle als mein C/C++ - Compiler. Ich installierte Schub mit dem einfachste Weg brew install boostWarum kann ich nicht auf die Thread-Bibliotheken von Boost zugreifen?

Zuerst habe ich versucht, diese Code

#include <iostream> 
#include <boost/array.hpp> 

int main() { 
    boost::array<int, 4> arr = {1, 2, 3, 4}; 
    std::cout << arr[3]; 
    return 0; 
} 

Es könnte gut funktionieren. So scheint es, dass Boost richtig eingerichtet ist.

Aber wenn versuchen #include<boost/thread.hpp> und den Beispielcode zu verwenden, wie unten dargestellt:

#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(); 
} 

Dann habe ich diese

/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 

ich eine Zeit lang dieses Problem googeln. Ich habe versucht, Bibliotheksverzeichnis set(Boost_LIBRARY_DIRS /usr/local/lib) link_directories(${Boost_LIBRARY_DIRS}) zu setzen.

Mein CMakeLists.txt ist hier:

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}) 

Ich habe auch versucht clang++ main.cpp -lboost_thread-mt. Und bekam dann diese:

[email protected]:~/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) 

Kann mir jemand etwas Hilfe geben? Vielen Dank!


UPDATE: mit dem find_package Befehl als @Angew sagte, habe ich einige neue Fehler.

/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

habe ich versucht, eine ältere Version von boost aber die gleichen Schweller. Und wenn ich target_include_directories(untitled PRIVATE ${Boost_INCLUDE_DIRS}) und 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 die wahre Antwort gefunden.

Ich habe die Antwort erneut aktualisiert. Es scheint, Boost.Thread erfordert, dass Sie auch Boost.System verwenden. Dies würde von den importierten Zielen gehandhabt werden, aber da Sie diese nicht verwenden können, müssen Sie sie offensichtlich explizit anfordern.

find_package(Boost REQUIRED COMPONENTS thread system) macht es gut.

+0

Es scheint, dass Sie CMake verwenden. Können Sie den relevanten Teil Ihrer 'CMakeLists.txt' bereitstellen? (Wie finden Sie Boost und wie Sie Ihre ausführbare Datei einrichten, um sie zu verwenden). – Angew

+0

@Angew Ja sicher, aktualisiert. –

Antwort

1

Die korrekte CMake-Methode zur Verbindung mit Paketen von Drittanbietern ist die Verwendung eines Aufrufs find_package(), der entweder ein Suchmodul (im Lieferumfang von CMake enthalten) oder eine Paketkonfigurationsdatei (vom Drittanbieterpaket) verwendet. um die notwendigen Bibliotheken, Verzeichnisse usw. zu finden.

Im Falle von Boost gibt es ein Suchmodul, das mit CMake selbst ausgeliefert wird. Es bietet eine sehr abstimmbare Schnittstelle, Sie können seine documentation für Details lesen.Aber hier ist die einfachste Art und Weise Sie es verwenden könnte:

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) 

Basierend auf dieser Nachricht, die Sie empfangen:

Importierte Ziele nicht für Boost-Version 106300

Es scheint, dass der Import-Ziel-Ansatz nicht mit Ihrer Boost-Version verwendet werden kann. Sie müssen daher auf die vom Suchmodul bereitgestellten Variablen zurückgreifen:

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}) 
+0

Vielen Dank! Ich habe Fehler wie 'CMake Fehler bei CMakeLists.txt: 7 (add_executable): Target" unbenannt "Links zu Ziel" Boost :: Thread ", aber das Ziel war nicht gefunden." Vielleicht sollte ich CMake sagen, wo Boost ist? –

+0

@AmorFati Haben Sie tatsächlich die entsprechende Version von Boost.Thread gebaut? Übrigens, wenn es nicht mit etwas wie "Can not find Boost" in der Zeile "find_package" fehlgeschlagen ist, dann wurde Boost erfolgreich gefunden (beachten Sie die Verwendung von 'REQUIRED'). – Angew

+0

@AmorFati Bearbeitet – Angew

Verwandte Themen