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

房东的猫 提交于 2021-02-07 17:29:02

问题


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?


回答1:


After hours of research, the answer given by Paul Belanger saved my day.

Digging a bit more in the codebase, they added a new option to manage exactly this case, so with the latest version of CMAKE you can add the following option:

set (Boost_ARCHITECTURE "-x64")

Source: https://github.com/Kitware/CMake/commit/1e08b625c291e0bb57d253b6656e812dc8848bd8#diff-555801259d7df67368f7deab1f9deacd




回答2:


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.



来源:https://stackoverflow.com/questions/51900290/cmake-findboost-cmake-mingw-w64-searching-for-library-with-incorrect-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!