从现有的Makefile创建CMakeLists文件 [英] Creating CMakeLists file from existing Makefile

查看:900
本文介绍了从现有的Makefile创建CMakeLists文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用cmake为C ++项目生成我的构建文件.我有一个现有的Makefile. 我在使用标准cmake语法生成此Makefile时遇到问题.

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.

如何在cmakeTARGET_LINK_LIBRARIES部分中包含标准的C ++库,例如-lstdc++ -lpthread -lboost_thread-mt?还是应将这些文件包含在ADD_DEPENDENCIES部分中.

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.

(或) 是否有一个简单的工具可以从Makefile

(OR) Is there a simple tool which generates a CMakeList.txt file from a Makefile

推荐答案

不幸的是,没有简单的从Makefiles到CMakeLists的1:1转换.由于CMake应该在所有平台上运行,因此它不能像GNU make那样依赖平台特定的假设,这会使某些地方的事情复杂化.

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.

尤其是CMake提供了一种非常强大且相当复杂的库使用机制:您调用

In particular, CMake offers a very powerful and rather complex mechanism for using libraries: You call find_package with 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附带了针对Pthreads,Boost和其他几个搜索脚本,但是,如果您使用的是更具异国情调的库,则可能必须自己编写搜索脚本,起初是一种不可思议的体验...

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附带的脚本大部分都记录得很好.

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-install-prefix>/share/cmake-2.8/Modules之类的位置.对于您的问题,请查看FindBoost.cmake和FindThreads.cmake文件(CMake应自动与标准库链接). Anycorn已经提供了一些使用Boost脚本的示例代码,您需要了解的所有其他信息都在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.

这篇关于从现有的Makefile创建CMakeLists文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆