使用 CMake 编译 32 位与 64 位项目
如何指定 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你按照这些思路做一些事情
You do something along these lines
我知道这是一个很老的问题。但当你用 Google 搜索“cmake 32 64”时,它仍然位于顶部。
我的答案类似于 user434507 的答案,但在我看来更具可读性(我不喜欢 cmake 中的 if-else 构造,它看起来很难看):
这会将
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):
This will point
BOOST_LIBRARY
path to /boost/win32/lib or /boost/win64/lib, depending on your architecture.特别是对于 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.
基于 rominf 我找到了以下解决方案(适用于 Windows)。
我将 boost 库安装到: C:\Boost_32 和 C:\Boost_64
在 CMakeLists.txt 中
说明:
CMAKE_SIZEOF_VOID_P
在 32 位平台上等于 4,在 64 位平台上等于 8。8*${CMAKE_SIZEOF_VOID_P}
将计算为 32 或 64,分别。
C:/Boost_${BITS}
自动变成C:/Boost_32
或C:/Boost_64
优点:
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
Explanation:
CMAKE_SIZEOF_VOID_P
is equal to 4 on 32bit platform, and 8 on 64bit platform.8*${CMAKE_SIZEOF_VOID_P}
will evaluate to 32 or 64,respectively.
C:/Boost_${BITS}
turns intoC:/Boost_32
orC:/Boost_64
automagicallyAdvantages: