2
votes

Boost compiled libraries have the same names for x86 and x64. My project uses Boost and I want it to automatically link correct Boost libraries when compiling with CMake for x64 or x86 target

My CMakeFiles.txt uses simple code

find_package(Boost REQUIRED
    COMPONENTS
    coroutine context thread filesystem program_options system
)

My Boost is built with (MSVC2015)

b2 address-model=32 --build-type=minimal stage --stagedir stage
b2 address-model=64 --build-type=minimal stage --stagedir stage64

I have also tried "install" target and putting boost builds into separate folders

I am building my project with (Windows)

md build32
cd build32
cmake .. -G"Visual Studio 14 2015"
cmake --build .
cd ..

and

md build
cb build
cmake .. -G"Visual Studio 14 2015 Win64"
cmake --build .
cd ..

x86 target is build successfull because its libraries (boost) are laying in "stage" folder that is well-known to CMake's FindBoost module

But x64 target cant be built because FindBoost uses x86 libraries of Boost for building process and does not try to use libraries from "stage64" with this error:

D:\lib\boost_1_61_0\stage\lib\libboost_coroutine-vc140-mt-gd-1_61.lib : warning LNK4272: library machine type 'X86' conflicts with target machine type 'x64'

My goal is to exlude any additional params at "Cmake" calling to build my project and I want it to automatically find correct boost libs for x86 or x64 depending on which -G param of CMAKE I am using

How can I change my CMakeFiles.txt to make it automatically find correct boost libraries?

Boost version - 1.61, MSVC 2015, CMAKE - 3.6.2 (latest and has knowledge about boost 1.61), Windows 7 x64

1
You may set CMake variable BOOST_ROOT to approprite directory (D:\lib\boost_1_61_0\stage64) when build project for x86_64.Tsyvarev
@Tsyvarev, unfortunately, boost stage dir does not contain includes - only libs. So I need to set BOOST_ROOT to d:\lib\boost_1_61_0. But even if I add -DBOOST_LIBRARYDIR to d:\lib\boost_1_61_0\stage64\lib - it still uses incorrect d:\lib\boost_1_61_0\stage (x86) for libs when I am building x64. It somehow hard-coded inside CMake's findboost moduleEvgeniy
Can you try manually putting the x86 libs inside BOOST_ROOT/lib32-msvc-14.0/ and the x64 ones in BOOST_ROOT/lib64-msvc-14.0/ and check again?llonesmiz
@jv_, looks like you are correct. I need remove "stage" dir and put x86 libraries somewhere else. If my boost folder contains "stage" - it uses it and does not look into BOOST_LIBRARY_DIR at allEvgeniy
wasthishelpful's answer seems to be better, but you may need to use stage32 instead of just stage. The problem seems to be that BOOST_ROOT/stage/lib seems to be unconditionally added to the list of directories where CMake looks for libraries.llonesmiz

1 Answers

4
votes

I guess you have BOOST_ROOT set somewhere in your poject, in the cache, or in an environment variable.

Looking in the sources, you may short-circuit find process by setting Boost_LIBRARY_DIR. Using CMAKE_SIZEOF_VOID_P to detect architecture:

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(Boost_LIBRARY_DIR ${BOOST_ROOT}/stage64/lib
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
    set(Boost_LIBRARY_DIR ${BOOST_ROOT}/stage/lib
endif()

find_package(...)

${BOOST_ROOT} may be replaced with $ENV{BOOST_ROOT}.