I want to use boost::thread in my project and I use CMake as a build tool. However, even a very simple setup results in two compiler errors:
main.cpp
#include <boost/thread.hpp>
int main()
{
boost::thread t;
return 0;
}
CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
project (ThreadTest)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.58.0 COMPONENTS random thread)
set(SOURCE_DIR src)
set(SOURCE_FILES
${SOURCE_DIR}/main.cpp
)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(test ${SOURCE_FILES})
target_link_libraries(test ${Boost_LIBRARIES})
endif()
I'm using Boost 1.68.0 which CMake can find and is able to build proper Visual Studio project files.
I tried using boost::random, and it worked.
However, compiling the above program results in two error messages:
- E2512: Argument for a feature-test-macro has to be a simple identifier (pointing out to an error in boost file error_code.hpp)
- LINK1104: File 'libboost_thread-vc141-mt-x64-1_68.lib' cannot be opened
This is the line in error_code.hpp which throws the error
I looked for the file 'libboost_thread-vc141-mt-x64-1_68.lib' in my boost installation but only found 'boost_1_68_0\lib64-msvc-14.0\boost_thread-vc140-mt-gd-x64-1_68.lib'
The linker settings contain the correct files:
So, my two questions:
- Why is there a compilation error in error_code.hpp, which is part of the boost::system module and what can I do about it?
- Why does VS want to link the file libboost_thread-vc141-mt-x64-1_68.lib, instead of the correct and available libboost_thread-vc140-mt-x64-1_68.lib?
#pragma comment(lib, ...)
directives. You should really build boost with the same version of your compiler you use for your code. – Miles Budnek#pragma comment(lib)
directives with something liketarget_compile_definitions(test PUBLIC BOOST_ALL_NO_LIB)
– Daniel Schepler