Compiling 32-bit vs 64-bit project using CMake

后端 未结 4 1145
日久生厌
日久生厌 2020-12-24 11:54

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

相关标签:
4条回答
  • 2020-12-24 12:38

    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.

    0 讨论(0)
  • 2020-12-24 12:39

    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} )
    
    0 讨论(0)
  • 2020-12-24 12:44

    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 into C:/Boost_32 or C:/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.
    0 讨论(0)
  • 2020-12-24 12:48

    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.

    0 讨论(0)
提交回复
热议问题