How do I specify that CMake should use a different link_directories
value depending on whether the target is 32-bit or 64-bit? For example, 32-bit binaries need to link with 32-bit Boost, 64-bit binaries need to link with 64-bit Boost.
4 Answers
You do something along these lines
if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
set( BOOST_LIBRARY "/boost/win64/lib" )
else( CMAKE_SIZEOF_VOID_P EQUAL 8 )
set( BOOST_LIBRARY "/boost/win32/lib" )
endif( CMAKE_SIZEOF_VOID_P EQUAL 8 )
set( CMAKE_EXE_LINKER_FLAGS ${BOOST_LIBRARY} )

- 9,126
- 6
- 36
- 40
-
As specified by Martin, this shouldn't be needed for boost, but it is a good to know method anyway. – tibur Oct 29 '10 at 21:39
-
10What were the cmake developers thinking with the else ( ... ) part of the syntax. That's not an else if BTW. confused me for a moment until I remembered this. New cmake syntax doesn't need the else, endif stuff in brackets. – hookenz Oct 04 '12 at 00:06
I know it's quite old question. But it's still on top when you search with Google "cmake 32 64". I have answer similar to user434507's answer but a little bit more readable in my opinion (I don't like if-else construction in cmake, it looks ugly):
math(EXPR BITS "8*${CMAKE_SIZEOF_VOID_P}")
set(BOOST_LIBRARY "/boost/win${BITS}/lib")
set(CMAKE_EXE_LINKER_FLAGS ${BOOST_LIBRARY})
This will point BOOST_LIBRARY
path to /boost/win32/lib or /boost/win64/lib, depending on your architecture.
-
Error: CMake Error at build.cmake:27 (math): math cannot parse the expression: "8*": syntax error, unexpected $end, expecting exp_OPENPARENT or exp_NUMBER (2) – Abhishek Jain May 17 '16 at 12:41
-
there is TINY error in second line. Not {$BITS} but ${BITS}. $ sign before brackets. – Nick Sep 01 '17 at 14:57
For Boost specifically, you should use
FIND_LIBRARY(Boost 1.44 COMPONENTS ...)
Then the CMake variable Boost_LIBRARY_DIRS will contain the correct library path, which has to be set using LINK_DIRECTORIES, e.g.
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
The more general case is correctly described in user434507's answer.

- 1,261
- 7
- 11
Based on rominf I turned up following solution (for Windows). I install boost libraries into: C:\Boost_32 and C:\Boost_64
In CMakeLists.txt
math(EXPR BITS "8*${CMAKE_SIZEOF_VOID_P}")
set(BOOST_ROOT C:/Boost_${BITS})
find_package(Boost 1.64.0 COMPONENTS ... )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
LINK_DIRECTORIES(${Boost_LIBRARY_DIR})
Explanation:
CMAKE_SIZEOF_VOID_P
is equal to 4 on 32bit platform, and 8 on 64bit platform.- Expression
8*${CMAKE_SIZEOF_VOID_P}
will evaluate to 32 or 64, respectively. C:/Boost_${BITS}
turns intoC:/Boost_32
orC:/Boost_64
automagically
Advantages:
- You don't need conditionals (and in my CMakeLists there are too many already),
- It is 90% how you 'should' include Boost with CMake.

- 1,248
- 12
- 18
-
CMAKE_SIZEOF_VOID_P is always blank for me :( I believe you need to run try_compile before it has a valid value. – David Ledger Sep 04 '20 at 09:29
-
try make CMakeLists.txt with these 2 lines:"project(check_for_sizeof_void_p) message( ${CMAKE_SIZEOF_VOID_P} )" And run "cmake ." – Nick Sep 07 '20 at 06:24
-
1I fixed the issue. I think my antivirus was preventing various things occuring. I moved it out of a protected directory and into a path without spaces (maybe it was that too) and all good. Strange... – David Ledger Sep 07 '20 at 07:12