Linux 从现有的 Makefile 创建 CMakeLists 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9985839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating CMakeLists file from existing Makefile
提问by Pradeep Jawahar
I want to use cmake
to generate my build files for a C++ project. I have an existing Makefile
.
I am having problems generating this Makefile
using the standard cmake
syntax.
我想用来cmake
为 C++ 项目生成我的构建文件。我有一个现有的Makefile
. 我在Makefile
使用标准cmake
语法生成它时遇到问题。
How do I include standard C++ libraries like -lstdc++ -lpthread -lboost_thread-mt
in the TARGET_LINK_LIBRARIES
section of cmake
? Or should these files be included in the ADD_DEPENDENCIES
section.
我如何包含标准 C++ 库,如 -lstdc++ -lpthread -lboost_thread-mt
的TARGET_LINK_LIBRARIES
部分cmake
?或者应该将这些文件包含在该ADD_DEPENDENCIES
部分中。
(OR)
Is there a simple tool which generates a CMakeList.txt
file from a Makefile
(或)是否有一个简单的工具可以CMakeList.txt
从Makefile
采纳答案by ComicSansMS
Unfortunately, there is no straightforward 1:1 conversion from Makefiles to CMakeLists. Since CMake is supposed to run on all platforms, it can not rely on platform specific assumptions like GNU make does, which complicates things in certain places.
不幸的是,没有从 Makefile 到 CMakeLists 的直接 1:1 转换。由于 CMake 应该在所有平台上运行,因此它不能像 GNU make 那样依赖特定于平台的假设,这会使某些地方的事情变得复杂。
In particular, CMake offers a very powerful and rather complex mechanism for using libraries: You call find_packagewith the name of your library, which will invoke a library search script from your cmake module path. This script (which is also written in CMake) will attempt to detect the location of the library's header and lib files and store them in a couple of CMake variables that can then be passed to the according CMake commands like include_directories and target_link_libraries.
特别是,CMake 为使用库提供了一种非常强大且相当复杂的机制:您使用库的名称调用find_package,它将从您的 cmake 模块路径调用库搜索脚本。这个脚本(也是用 CMake 编写的)将尝试检测库的头文件和 lib 文件的位置,并将它们存储在几个 CMake 变量中,然后可以将这些变量传递给相应的 CMake 命令,如 include_directories 和 target_link_libraries。
There are two problems with this approach: First, you need a search script. Fortunately, CMake ships with search scripts for Pthreads, Boost and a couple of others, but if you are using a more exotic library, you might have to write the search script yourself, which is kind of an arcane experience at first...
这种方法有两个问题:首先,您需要一个搜索脚本。幸运的是,CMake 附带了 Pthreads、Boost 和其他一些搜索脚本,但是如果您使用的是更奇特的库,您可能必须自己编写搜索脚本,这起初是一种神秘的体验......
The second major problem is that there is no standard way for a search script to return its results. While there are naming conventions for the used variables, those often don't apply. In practice that means you will have to check out a search script's source to know how to use it. Fortunately, the scripts that come with CMake are mostly very well documented.
第二个主要问题是搜索脚本没有标准的方法返回其结果。虽然使用的变量有命名约定,但这些约定通常不适用。实际上,这意味着您必须查看搜索脚本的源代码才能知道如何使用它。幸运的是,CMake 附带的脚本大多都有很好的文档记录。
The builtin scripts are located somewhere like <cmake-install-prefix>/share/cmake-2.8/Modules
. For your question, look at the FindBoost.cmake and FindThreads.cmake files (CMake should automatically link with the standard library). Anycorn already gave some sample code for using the Boost script, everything else you need to know is in the CMake documentation or directly in the search script files.
内置脚本位于类似<cmake-install-prefix>/share/cmake-2.8/Modules
. 对于您的问题,请查看 FindBoost.cmake 和 FindThreads.cmake 文件(CMake 应自动与标准库链接)。Anycorn 已经提供了一些使用 Boost 脚本的示例代码,您需要知道的其他一切都在 CMake 文档中或直接在搜索脚本文件中。
回答by John Zwinck
Like this:
像这样:
target_link_libraries(your-target-name pthread boost_thread-mt etc)
You should not use add_dependencies when you want to link libraries. Linking implies a dependency, but the dependency alone will not be sufficient when you need to link.
当您想要链接库时,不应使用 add_dependencies。链接意味着依赖,但当您需要链接时,仅依赖依赖是不够的。
回答by Anycorn
With Boost you really need to use package finder
使用 Boost 你真的需要使用包查找器
set(Boost_ADDITIONAL_VERSIONS "1.46" "1.46.0" "1.46.1")
set(Boost_USE_MULTITHREADED ON) # for -mt
find_package(Boost COMPONENTS thread)
if(Boost_FOUND)
MESSAGE(STATUS "Found Boost: ${Boost_LIBRARY_DIRS}")
MESSAGE(STATUS "Found Boost libraries: ${Boost_LIBRARIES}")
set(LIBRARIES "${LIBRARIES};${Boost_LIBRARIES}")
else()
MESSAGE(FATAL_ERROR "Boost Thread NOT FOUND")
endif()
target_link_libraries(executable ${LIBRARIES})