Search code examples
c++windowsboostcmakecmake-modules

Cmake FindBoost.cmake MinGW-W64: searching for library with incorrect name


I have built Boost 1.68 (using instructions from https://gist.github.com/sim642/29caef3cc8afaa273ce6, and adding link=static,shared to the b2 command line to also build shared libraries.)

The libraries appear to build correctly, and I have set the BOOST_INCLUDEDIR and BOOST_LIBRARYDIR environment variables correctly.

However, when I add the following to a CMakeLists.txt:

find_package(Boost REQUIRED COMPONENTS system context coroutine thread random REQUIRED)

and generate with MinGW Makefiles, I get the following error:

CMake Error at C:/Users/pbelanger/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/182.4129.15/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:2044 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.68.0

  Boost include path: C:/boost/install/include/boost-1_68

  Could not find the following static Boost libraries:

          boost_system
          boost_context
          boost_coroutine
          boost_thread
          boost_random

  Some (but not all) of the required Boost libraries were found.  You may
  need to install these additional Boost libraries.  Alternatively, set
  BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT
  to the location of Boost.

I have placed the output of adding set(Boost_DEBUG ON) before the find_package line here: https://pastebin.com/yRd5DPt4

According to the debug output, the find script is searching in the correct directory (c:\boost\install\lib), but is not finding the boost libraries since they have a different naming scheme. For example, the system library is named libboost_system-mgw81-mt-x64-1_68.dll, but the find script is passing he library name boost_system-mgw81-mt-1_68 to CMake's find_library. Notice that the addressing model (-x64)is not listed in the latter name.

My question is whether this is an issue with Boost, or the findCMake script? Can this be fixed by setting a specific cmake variable before the findCMake script?


Solution

  • Looking at the source of FindBoost.cmake, line 1478, the script looks at the value of CMAKE_CXX_COMPILER_ARCHITECTURE_ID in order to build the correct architecture tag. However, on my compiler (MinGW-W64 8.1 64-bit), this string is empty. Therefore, the architecture tag is omitted.

    I have to set the value of this variable manually by putting the following before my find_package line:

    if(WIN32 AND "x${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}" STREQUAL "x")
        message(WARNING "WIN32 compiler does not specify CMAKE_CXX_COMPILER_ARCHITECTURE_ID -- filling in manually")
        if(CMAKE_SIZEOF_VOID_P EQUAL 8)
            set(CMAKE_CXX_COMPILER_ARCHITECTURE_ID "x64")
        else()
            set(CMAKE_CXX_COMPILER_ARCHITECTURE_ID "x86")
        endif()
        message(STATUS "Compiler architecture: ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
    endif()
    
    # now we should be able to find boost correctly. 
    find_package(Boost REQUIRED COMPONENTS system context coroutine thread random REQUIRED)
    

    This makes the find_package work correctly.