C++ 将 Cmake 与 Qt Creator 结合使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9415761/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:47:58  来源:igfitidea点击:

Using Cmake with Qt Creator

c++idecmakeqt-creator

提问by themean

I would like to use Qt creatorand Cmaketogether (please, don't ask me about my motivation, accept this as a given.)

我想Qt creator和和Cmake一起使用(请不要问我的动机,接受这是给定的。)

I successfully set up Qt creator to use cmake "Cmake": see this, thisand thisdocuments regarding how I did that.

我成功地建立了Qt的创建者使用cmake的“CMake的”:看到这个这个这个关于我是怎么做的文件。

I successfully create hello worldproject, but I can't create files in project, only add existing files to project tree and after that adding it to cmake list. Standard operation of Qt creator "Add New..." doesn't work and I can't find why.

我成功创建了hello world项目,但无法在项目中创建文件,只能将现有文件添加到项目树中,然后将其添加到 cmake 列表中。Qt 创建者“添加新...”的标准操作不起作用,我找不到原因。

Is there anybody who uses Qt creator and "Cmake" together? Is the combination actually possible?

有没有人同时使用Qt creator和“Cmake”?这种组合真的可能吗?

Note: I'm using Qt creator v2.4.1.

注意:我使用的是 Qt creator v2.4.1。

采纳答案by Evgeny Lazin

You can add files using glob expression in your CMakeLists.txt, like this:

您可以在 CMakeLists.txt 中使用 glob 表达式添加文件,如下所示:

file(GLOB SRC . *.cpp)
add_executable (your_exe_name ${SRC})

Cmake will pick your new cpp files next time you run it and QtCreator will show them in the project browser.

Cmake 将在您下次运行时选取您的新 cpp 文件,QtCreator 将在项目浏览器中显示它们。

Update

更新

This solution may be useful but as noted in comments - this is not a good practice. Every time somebody add new source file and commit changes, you need to rerun cmake to build all the sources. Usually I just touchone of the CMakeLists.txtfiles if my build is broken after I pool recent changes from repository. After that makewill run cmakeautomatically and I didn't need to run it by hands. Despite of that I think that explicit source lists in CMakeLists.txtis a good thing, they called thing CMake Listsfor a reason.

此解决方案可能有用,但如评论中所述 - 这不是一个好习惯。每次有人添加新的源文件并提交更改时,您都需要重新运行 cmake 来构建所有源。如果在我从存储库中汇集最近的更改后我的构建被破坏,通常我只是CMakeLists.txt文件touch之一。之后会自动运行,我不需要手动运行它。尽管如此,我认为CMakeLists.txt中的显式源列表是一件好事,他们将事物称为 CMake列表是有原因的。makecmake

回答by alanxz

When you add new files in QtCreator using the "New File or Project..." dialog it only creates the files on disk, it doesn't automatically add the files to the CMakeLists.txt. You need to do this by hand by editing the CMakeLists.txt file.

当您使用“新建文件或项目...”对话框在 QtCreator 中添加新文件时,它只会在磁盘上创建文件,不会自动将文件添加到 CMakeLists.txt。您需要通过编辑 CMakeLists.txt 文件来手动执行此操作。

The next time you build the project, CMake will be re-run, and QtCreator will pick up the new files and show them in the project browser.

下次构建项目时,CMake 将重新运行,QtCreator 将选取新文件并在项目浏览器中显示它们。

回答by chao

I solve this problem that I added new files in standard way (CTRL+N), then added needed files in CMakeLists. After that, right click on project in project tree view and choose option Run CMake. After this, files showed in project list tree. Only build was not enough.

我解决了这个问题,我以标准方式(CTRL+N)添加了新文件,然后在 CMakeLists.txt 中添加了所需的文件。之后,在项目树视图中右键单击项目并选择选项 Run CMake。之后,文件显示在项目列表树中。只有构建是不够的。

回答by para.cavalcante

I tested here and happened the same behavior because those options you were asking were really disabled.

我在这里测试并发生了相同的行为,因为你问的那些选项真的被禁用了。

Use File -> "New File or Project..." or CTRL+N to add new files and after that add to CMakeLists.txt

使用 File -> "New File or Project..." 或 CTRL+N 添加新文件,然后添加到 CMakeLists.txt

回答by aTom

I'm adding an updated answer for newer versions of QtCreator (4.x). In the Tools > Options...menu, choose the Build & Runsection and then the CMaketab. You will see the Adding Filessettings, and you can set it to Copy file paths.

我正在为较新版本的 QtCreator (4.x) 添加更新的答案。在Tools > Options...菜单中,选择Build & Run部分,然后选择CMake选项卡。您将看到添加文件设置,您可以将其设置为复制文件路径

This way, when you want to add a new file to your project, in the Projectview, right click on the desired CMake Executable/Library's name and select Add New..., go through the Add dialog, and when you'll validate the dialog, QtCreator will open CMakeLists.txtin the Editorview. Finally, paste the content of the clipboard at the end of the corresponding source file list and save CMakeLists.txt. The CMake project will be parsed, and your new file will show up in the Projectview.

这样,当您想向项目添加新文件时,在Project视图中,右键单击所需的 CMake Executable/Library 名称并选择Add New...,通过Add dialog,然后验证在对话框中,QtCreator 将在编辑器视图中打开CMakeLists.txt。最后,将剪贴板的内容粘贴到对应源文件列表的末尾,并保存CMakeLists.txt。CMake 项目将被解析,您的新文件将显示在项目视图中。