使用 CMake 编译 32 位与 64 位项目

发布于 2024-09-29 10:15:59 字数 135 浏览 0 评论 0原文

如何指定 CMake 应根据目标是 32 位还是 64 位使用不同的 link_directories 值?例如,32 位二进制文​​件需要与 32 位 Boost 链接,64 位二进制文​​件需要与 64 位 Boost 链接。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

驱逐舰岛风号 2024-10-06 10:15:59

你按照这些思路做一些事情

  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} )

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} )
独夜无伴 2024-10-06 10:15:59

我知道这是一个很老的问题。但当你用 Google 搜索“cmake 32 64”时,它仍然位于顶部。
我的答案类似于 user434507 的答案,但在我看来更具可读性(我不喜欢 cmake 中的 if-else 构造,它看起来很难看):

math(EXPR BITS "8*${CMAKE_SIZEOF_VOID_P}")
set(BOOST_LIBRARY "/boost/win${BITS}/lib")
set(CMAKE_EXE_LINKER_FLAGS ${BOOST_LIBRARY})

这会将 BOOST_LIBRARY 路径指向 /boost/ win32/lib 或 /boost/win64/lib,具体取决于您的架构。

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.

迷爱 2024-10-06 10:15:59

特别是对于 Boost,您应该使用

FIND_LIBRARY(Boost 1.44 COMPONENTS ...)

然后 CMake 变量 Boost_LIBRARY_DIRS 将包含正确的库路径,该路径必须使用 LINK_DIRECTORIES 设置,例如

LINK_DIRECTORIES($ {Boost_LIBRARY_DIRS})

user434507 的答案中正确描述了更一般的情况。

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.

江湖彼岸 2024-10-06 10:15:59

基于 rominf 我找到了以下解决方案(适用于 Windows)。
我将 boost 库安装到: C:\Boost_32 和 C:\Boost_64

在 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})

说明:

  • CMAKE_SIZEOF_VOID_P 在 32 位平台上等于 4,在 64 位平台上等于 8。
  • 表达式 8*${CMAKE_SIZEOF_VOID_P} 将计算为 32 或 64,
    分别。
  • C:/Boost_${BITS} 自动变成 C:/Boost_32C:/Boost_64

优点:

  • 你不需要条件 (在我的 CMakeLists 中已经有太多了),
  • 90% 你“应该”将 Boost 包含在 CMake 中。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文