4

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

enter image description here

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:

enter image description here

So, my two questions:

  1. 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?
  2. 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?
WolfgangP
  • 3,195
  • 27
  • 37
  • It looks like you're compiling your code with a different version of your compiler than you used to build boost. That's throwing off boost's `#pragma comment(lib, ...)` directives. You should really build boost with the same version of your compiler you use for your code. – Miles Budnek Oct 03 '18 at 23:41
  • Since 14.0 Boost should be compatible with 14.1 toolchain, you could also disable the `#pragma comment(lib)` directives with something like `target_compile_definitions(test PUBLIC BOOST_ALL_NO_LIB)` – Daniel Schepler Oct 04 '18 at 00:22

3 Answers3

7

I get the same error message in Visual Studio 2017 version 15.9 and Boost 1.69.0; I think that the trouble arises from this VS version introducing some version of __has_cpp_attribute which the boost authors expected only to be present in clang.

I had to change boost\system\detail\config.hpp line 50 from

#if defined(__has_cpp_attribute)

to

#if defined(__clang__) && defined(__has_cpp_attribute)

as the remaining preprocessor define is only relevant to clang anyways.

Changing boost headers is quite messy, but I didn't find a clean solution yet :(. I was hoping /Zc had some switch to deactive this new "feature" (feature testing macro)

Oliver Zendel
  • 2,695
  • 34
  • 29
  • Had the same issue with 15.9.4 and Boost 1.69.0. Your answer solves it, but I'd still love to see a solution that doesn't involve changing Boost headers. – Mikkel K. Jan 04 '19 at 12:55
  • Me too, I suspect that this must be fixed by MS or this fix has to be pushed to the boost trunk – Oliver Zendel Jan 11 '19 at 15:05
  • 4
    Addenum: This is now fixed in the current develop branch; v1.70 release still has the breaking code ( see https://github.com/boostorg/system/commit/bfbc5ec42ff35f4dcbd1547244504d4e6fc7737d ) – Oliver Zendel May 13 '19 at 08:05
1

1) If you use

set(Boost_USE_STATIC_LIBS OFF)

and dont give a version number for boost

find_package(Boost COMPONENTS random thread) 

since you are using 1.68 and not 1.58, right? It should work. I can reproduce your error messages with your settings and it works using the mentioned settings.

2) The boost CMake package file searches for a boost version depending on your selected compiler, I guess the default for vs 2017 is 14.1. See also here.

To solve this problem, download and install the proper prebuilt binaries.

Gombat
  • 1,994
  • 16
  • 21
0

Thank you for your hints!

The following steps resolved the problem:

1) I installed boost for MSVC 14.1 (prebuild binaries)

2) I added the following to the CMakeLists.txt:

add_definitions(-DBOOST_ALL_NO_LIB)
WolfgangP
  • 3,195
  • 27
  • 37
  • for me, the compile error did not go away with this "add_definitions"... :( What else could it be? – Zordid Mar 07 '19 at 12:56
  • You have to take extra care that your Boost library exaclty matches your Visual Studio version. You should use the prebuild libraries. – WolfgangP Mar 08 '19 at 15:32
  • @WolfgangP: The error is in a Boost header file. "Prebuilt boost libraries" affect only the .cpp files in Boost, which for prebuilt libraries will have been compiled into .obj and then linked in .lib format. – MSalters Jun 04 '19 at 09:30