Why can't I connect to the Stream Libraries Extension?

I am using Mac OS 10.11 and clang as my C / C ++ compiler. I installed boost with the simplest waybrew install boost

I tried this code first

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

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

      

This might work fine. So the boost seems to be set up correctly.

But when try to use #include<boost/thread.hpp>

and sample code like below:

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

      

Then I got these

/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

      

I've tried this problem for a long time. I tried to install dir library set(Boost_LIBRARY_DIRS /usr/local/lib) link_directories(${Boost_LIBRARY_DIRS})

.

My CMakeLists.txt is located here:

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

      

I have also tried clang++ main.cpp -lboost_thread-mt

. And then we got:

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)

      

Can anyone help me? Thank you very much!


UPDATE: with find_package command as @Angew said I got a new error.

/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

I tried some older version of boost, but the same worked. And if I use target_include_directories(untitled PRIVATE ${Boost_INCLUDE_DIRS})

andtarget_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 found the correct answer.

I have updated the answer again. Boost.Thread seems to require you to use Boost.System. Imported targets will be processed, but since you cannot use them, you need to explicitly request it explicitly.

find_package(Boost REQUIRED COMPONENTS thread system)

makes it work fine.

+3


source to share


1 answer


The correct CMake way to interact with third party packages is to use a call find_package()

that will use either the find module (comes with CMake) or the package config file (comes with a third party package) to find the required libraries, directories, etc.

In the case of Boost, there is a find module that comes with CMake. It offers a very customizable interface, you can read its documentation . However, here's the simplest way to use it:

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)

      




Based on this post you are receiving:

Imported targets are not available for Boost version 106300

it seems like the import target approach cannot be used with your Boost version. Therefore, you will have to revert to the variables provided by the find module:

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

      

+1


source







All Articles