C++ 如何生成 CMakeLists.txt?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6914524/
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
How to generate CMakeLists.txt?
提问by celavek
I need some pointers/advice on how to automatically generate CMakeLists.txt files for CMake. Does anyone know of any existing generators? I've checked the ones listed in the CMake Wikibut unfortunately they are not suitable for me.
我需要一些关于如何为 CMake 自动生成 CMakeLists.txt 文件的指针/建议。有谁知道任何现有的发电机?我已经检查了CMake Wiki 中列出的那些,但不幸的是它们不适合我。
I already have a basic Python script which traverses my project's directory structure and generates the required files but it's really "dumb" right now. I would like to augment it to take into account for example the different platforms I'm building for, the compiler\cross-compiler I'm using or different versions of the libraries dependencies I might have. I don't have much\expert experience with CMake and an example I could base my work or an already working generator could be of great help.
我已经有一个基本的 Python 脚本,它遍历我的项目的目录结构并生成所需的文件,但现在它真的很“愚蠢”。我想增加它以考虑例如我正在构建的不同平台,我正在使用的编译器\交叉编译器或我可能拥有的不同版本的库依赖项。我对 CMake 没有太多\专家经验,一个例子可以作为我的工作基础,或者一个已经工作的生成器可能会有很大帮助。
回答by Preeti Kaur
I am of the opinion that you need not use an automated script for generating CMakeLists.Txt as it is a very simple task to write one, after you have understood the basic procedure. Yeah I do agree that understanding the procedure to write one as given in CMake Wiki is also difficult as it is too much detailed.
我认为您不需要使用自动脚本来生成 CMakeLists.Txt,因为在您了解基本过程后,编写一个脚本是一项非常简单的任务。是的,我同意理解编写 CMake Wiki 中给出的程序也很困难,因为它太详细了。
A very basic example showing how to write CMakeLists.txtis shown here, which I think will be of use to everyone, even someone who is going to write CMakeLists.txt for the first time.
一个非常简单的例子显示了如何写的CMakeLists.txt显示在这里,我想这将是利用每一个人,甚至有人谁去写的CMakeLists.txt首次的。
回答by wOlVeRiNe
Well i dont have much of an experience in Cmake either, but to perform a cross platform make a lot of files need to be written and modified including the CMakeLists.txt file, i suggest that you use this new tool called the ProjectGenerator Tool, its pretty cool, it does all the extra work needed and makes it easy to generate such files for 3'rd party sources with little effort. Just read the README carefully before using it.
好吧,我在 Cmake 方面也没有太多经验,但是要执行跨平台,需要编写和修改大量文件,包括 CMakeLists.txt 文件,我建议您使用这个名为 ProjectGenerator Tool 的新工具,它的非常酷,它完成了所有需要的额外工作,并且可以轻松地为 3'rd 方源生成此类文件,而无需付出任何努力。使用前请仔细阅读自述文件。
Link: http://www.ogre3d.org/forums/viewtopic.php?f=1&t=54842
链接:http: //www.ogre3d.org/forums/viewtopic.php?f =1&t= 54842
回答by Lindydancer
I think that you are doing this upside down.
我认为你这样做是颠倒的。
When using CMake, you are supposed to write the CMakeLists.txt yourself. Typically, you don't need to handle different compilers as CMake has knowledge about them. However, if you must, you can add code in the CMakeFiles to do different things depending on the tool you are using.
使用 CMake 时,您应该自己编写 CMakeLists.txt。通常,您不需要处理不同的编译器,因为 CMake 了解它们。但是,如果必须,您可以在 CMakeFiles 中添加代码以根据您使用的工具执行不同的操作。
回答by nicolas-f
CLionis an Integrated development environment that is fully based on CMake project file.
CLion是一个完全基于 CMake 项目文件的集成开发环境。
It is able to generate itself the CMakeLists.txt file when using the import project from source
使用来自源的导入项目时,它能够自行生成 CMakeLists.txt 文件
However this is quite probable that you have to edit this file manually as your project grows and for adding external dependency.
但是,随着项目的增长和添加外部依赖项,您很可能必须手动编辑此文件。
回答by jpo38
I'm maintaining a C++ software environment that has more than 1000 modules (shared, static libraries, programs) and uses more than 20 third parties (boost, openCV, Qt, Qwt...). This software environment hosts many programs (~50), each one picking up some libraries, programs and third parties. I use CMake to generate the makefiles and that's really great.
我正在维护一个 C++ 软件环境,该环境拥有 1000 多个模块(共享、静态库、程序)并使用 20 多个第三方(boost、openCV、Qt、Qwt...)。这个软件环境承载了许多程序(约 50 个),每个程序都选择了一些库、程序和第三方。我使用 CMake 生成 makefile,这真的很棒。
However, if you write your CMakeLists.txt
as it is recommended to do (declare the module as being a library/program, importing source files, adding dependencies...). I agree with celavek: maintaining those CMakeLists.txt
files is a real pain:
但是,如果您CMakeLists.txt
按照建议的方式编写(将模块声明为库/程序、导入源文件、添加依赖项...)。我同意 celavek:维护这些CMakeLists.txt
文件真的很痛苦:
- When you add a new file to a module, you need to update its
CMakeLists.txt
- When you upgrade a third party, you need to update the
CMakeLists.txt
of all modules using it - When you add a new dependency (library
A
now needs libraryB
), you may need to update theCMakeLists.txt
of all programs usingA
- When you want a new global settings to be changed (compiler setting, predefined variable, C++ standard used), you need to update all your
CMakeLists.txt
- 向模块添加新文件时,需要更新其
CMakeLists.txt
- 当您升级第三方时,您需要更新
CMakeLists.txt
所有使用它的模块 - 当您添加新的依赖项(库
A
现在需要 libraryB
)时,您可能需要使用更新CMakeLists.txt
所有程序的A
- 当您想要更改新的全局设置(编译器设置、预定义变量、使用的 C++ 标准)时,您需要更新所有
CMakeLists.txt
Then, I see two strategies to adress those issues and likely the one mentioned by OP.
然后,我看到了两种解决这些问题的策略,可能是 OP 提到的一种。
1- Have CMakeLists.txt
be well written and be smart enough not to have a frozen behaviourto update themselves on the fly. That's what we have in our software environment. Each module has a standardized file organization (sources are in src
folder, includes are in inc
folder...) and have simple text files to specify their dependencies (with keywords we defined, like QT
to say the module needs to link with Qt
). Then, our CMakeLists.txt
is a two-line file and simply calls a cmake macro we wrote to automatically setup the module. As a MCVE that would be:
1-CMakeLists.txt
写得很好并且足够聪明,不会有僵硬的行为来即时更新自己。这就是我们在我们的软件环境中所拥有的。每个模块都有一个标准化的文件组织(源在src
文件夹中,包含在inc
文件夹中......)并有简单的文本文件来指定它们的依赖关系(使用我们定义的关键字,比如QT
模块需要链接Qt
)。然后,我们CMakeLists.txt
是一个两行文件,只需调用我们编写的 cmake 宏来自动设置模块。作为 MCVE,它将是:
CMakeLists.txt:
CMakeLists.txt:
include( utl.cmake )
add_module( "mylib", lib )
utl.cmake:
utl.cmake:
macro( add_module name what )
file(GLOB_RECURSE source_files "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
if ( what STREQUEL "lib" )
add_library( ${name} SHARED ${source_files} )
elseif ( what STREQUEL "prg" )
add_executable( ${name} ${source_files} )
endif()
# TODO: Parse the simple texts files to add target_link_libraries accordingly
endmacro()
Then, for all situations exposed above, you simply need to update utl.cmake, not the thousand of CMakeLists.txt
you have...
然后,对于上面暴露的所有情况,您只需要更新 utl.cmake,而不是CMakeLists.txt
您拥有的那一千个...
Honestly, we are very happy with this approach, the system becomes very easy to maintain and we can easily add new dependencies, upgrade third parties, change some build/dependency strategies...
老实说,我们对这种方法非常满意,系统变得非常易于维护,我们可以轻松添加新的依赖项、升级第三方、更改一些构建/依赖项策略……
However, there remains a lot of CMake
scripts to be written. And CMake
script language sucks...the tool's very powerful, right, but the script's variable scope, the cache, the painful and not so well documented syntax (just to check if a list is empty you must ask for it's size and store this in a variable!), the fact it's not object oriented...make it a real pain to maintain.
但是,还有很多CMake
脚本需要编写。而且CMake
脚本语言很烂……这个工具非常强大,对,但是脚本的变量范围、缓存、痛苦的和没有很好记录的语法(只是为了检查列表是否为空,你必须询问它的大小并将其存储在一个变量!),事实上它不是面向对象的......维护起来真的很痛苦。
So, I'm now convinced the real good approach may be to:
所以,我现在确信真正好的方法可能是:
2- completly generate the CMakeLists.txt
from a more powerful language like Python. The Python script would do things similar to what our utl.cmake does, instead it would generate a CMakeLists.txt
ready to be passed CMake tool (with a format as proposed in HelloWorld, no variable, no function....it would only call standard CMake function).
2-CMakeLists.txt
从更强大的语言(如 Python)完全生成。Python 脚本会做类似于我们的 utl.cmake 所做的事情,而是会生成一个CMakeLists.txt
准备传递的 CMake 工具(具有 HelloWorld 中建议的格式,没有变量,没有函数......它只会调用标准的 CMake功能)。
I doubt such generic tool exists, because it's hard to produce the CMakeLists.txt
files that will make everyone happy, you'll have to write it yourself. Note that gen-cmakedoes that (generates a CMakeLists.txt), but in a very primitive way and it apparently only supports Linux, but it may be a good start point.
我怀疑这样的通用工具是否存在,因为很难生成CMakeLists.txt
让每个人都满意的文件,您必须自己编写。请注意,gen-cmake 会这样做(生成 CMakeLists.txt),但是以一种非常原始的方式并且它显然只支持 Linux,但它可能是一个很好的起点。
This is likely to be the v2 of our software environment...one day.
这很可能是我们软件环境的 v2...有一天。
Note : Additionally, if you want to support both qmake and cmake for instance, a well written Python script could generate both CMakeLists and pro files on demand!
注意:另外,如果你想同时支持 qmake 和 cmake,一个写得很好的 Python 脚本可以按需生成 CMakeLists 和 pro 文件!
回答by Mekk
Not sure whether this is a problem original poster faced, but as I see plenty of ?just write CMakefile.txt” answers above, let me shortly explain why generating CMakefiles may make sense:
不确定这是否是原始海报面临的问题,但正如我看到的很多 ?just write CMakefile.txt” 上面的答案,让我简要解释为什么生成 CMakefiles 可能有意义:
a) I have another build system I am fairly happy with
a) 我有另一个我很满意的构建系统
(and which covers large multiplatform build of big collection of interconnected shared and static libraries, programs, scripting language extensions, and tools, with various internal and external dependencies, quirks and variants)
(并且涵盖了大量互连共享和静态库、程序、脚本语言扩展和工具的大型多平台构建,具有各种内部和外部依赖项、怪癖和变体)
b) Even if I were to replace it, I would not consider cmake.
b) 即使我要更换它,我也不会考虑 cmake。
I took a look at CMakefiles and I am not happy with the syntax and not happy with the semantics.
我看了一下 CMakefiles,我对语法不满意,对语义也不满意。
c) CLion uses CMakefiles, and Cmakefiles only (and seems somewhat interesting)
c) CLion 使用 CMakefiles,并且只使用 Cmakefiles(似乎有点有趣)
So, to give CLion a chance (I love PyCharm, so it's tempting), but to keep using my build system, I would gladly use some tool which would let me implement make generate_cmake and have all necessary CMakefiles generated on the fly according to the current info extracted from my build system. I can gladly feed the tool/script with information which sources and headers my app consists of, which libraries and programs it is expected to build, which -I, -L, -D, etc are expected to be set for which component, etc etc.
所以,为了给 CLion 一个机会(我喜欢 PyCharm,所以它很诱人),但为了继续使用我的构建系统,我很乐意使用一些工具来实现 make generate_cmake 并根据从我的构建系统中提取的当前信息。我可以很高兴地向工具/脚本提供我的应用程序包含哪些源和头文件的信息,它应该构建哪些库和程序,哪些 -I、-L、-D 等应该为哪个组件设置等等等等。
Well, of course I would be much happier if JetBrains would allow to provide some direct protocol of feeding the IDE with the information it needs (say, allowed me to provide my own command to compile, to run, and to emit whatever metadata they really need - I suppose they mainly need incdirs and defines to implement on the fly code analysis, and libpaths to setup LD_LIBRARY_PATH for the debugger), without referring to cmake. CMakefiles as protocol are somewhat complicated.
好吧,当然,如果 JetBrains 允许提供一些直接的协议来为 IDE 提供它需要的信息(比如,允许我提供我自己的命令来编译、运行和发出他们真正需要的任何元数据),我会更高兴需要 - 我想他们主要需要 incdirs 和定义来实现动态代码分析,以及 libpaths 来为调试器设置 LD_LIBRARY_PATH),而不是参考 cmake。CMakefiles 作为协议有点复杂。
回答by Alejandro Visiedo García
Maybe this could be helpful:
也许这可能会有所帮助:
The author has given some speeches about cmake and how to create modular projects using cmake into CPPCon. As far as I know, this tool require cmake, so that I suppose that generate it when you integrate new packages, or create new packages. Recently I read something about how to write a higher level description of the C/C++ project using a YAML file, but not sure if it is part of conan or not (what I read was from the author of conan). I have never used, and it is something pending for me, so that, please if you use it and fit your needs, comment your opinions about it and how it fit your scenario.
作者有一些关于cmake的演讲,以及如何在CPPCon中使用cmake创建模块化项目。据我所知,这个工具需要 cmake,所以我想当你集成新包或创建新包时会生成它。最近我读了一些关于如何使用 YAML 文件编写 C/C++ 项目的更高级别描述的内容,但不确定它是否是柯南的一部分(我读的是柯南的作者)。我从未使用过,它对我来说是待定的,因此,如果您使用它并满足您的需求,请评论您对它的看法以及它如何适合您的场景。