cmake - How to use Qt Creator as a smart editor?
问题描述
I am looking to use Qt Creator 4.7 as a smart editor that helps navigate/refactor code and catch errors early. I do not want to set it up to build the project—I only want to edit it.
Is this possible? If yes, how?
More specifically, I need to communicate the following information to Qt Creator and not deal with anything build-related:
- Which are my source files, and what is their language (C, C++98, C++11, etc.)
- The location of other includes files (like the
-I
compiler option) - Any special
#define
s to be assumed by the preprocessor
What I tried:
I can create a generic project using File -> New File Or Project -> Import Existing Project. This allows me to specify 2. and 3. above, but not 1. Specifically, it 4.7.0 considers all files to be C++98 and won't let me use a newer standard. The code model configuration no longer accepts the
-std=c++11
option. My impression is that the generic project support got little attention recently, and is no longer recommended.I attempted to set up a qmake project, but this seems to require a full Qt installation. I wish to avoid this, as I do not use Qt.
I looked at how to use a cmake-based project, but this seems to require setting up a specific build target. Is this really necessary, or is there a way to use a cmake-based project to communicate only the required information to Qt Creator, and not set up a build? Assume editing a header-only library, without also including any
.cpp
files. Again, it seems that a cmake-based project is not a good option, as the very purpose of cmake is to automate building, while all I need is to communicate essential information to my IDE.
Motivation: I often work on a library which is used to interface with Mathematica. It has its own Mathematica-based build system. Some required .cpp
sources are auto-generated by Mathematica. Thus I can't fully switch to something like cmake, and doing so would give me no benefit (but it would take a considerable amount of learning time). I simply want to be able to use Qt Creator as a smart editor.
Note: In the past, it was possible to add -std=c++
to the code model configuration and work around this issue. The latest version of Creator rejects this option.
解决方案
Qbs项目是通用项目的一个相当容易使用的替代方案,并有助于解决这个问题。
Qbs 相对于 qmake/cmake 的优势:
它带有 Qt Creator。不需要安装其他任何东西(比如所有的Qt)
它旨在与 Creator 配合使用(与 cmake 不同,cmake 是构建系统,而不是 IDE 项目格式)
没有必要向 Qbs 项目添加显式构建信息。指定哪些文件是项目的一部分就足够了。
入门:
在项目目录下运行
qbs-create-project
,创建一个barebones qbs文件在文件顶部的下方添加以下内容
type
,并根据需要进行调整:Depends { name: "cpp" } cpp.cxxLanguageVersion: "c++11" cpp.includePaths: base.concat([ "/your/include/path1", "/your/include/path2", ]) cpp.defines: base.concat([ "NDEBUG", "SOME_SYMBOL", "SYMBOL_WITH_VALUE=123", ])
而不是添加的文件qbs-creator-project
,您可能想要使用
files: [ "**/*.cpp" ]
添加所有文件,类似于通用项目的外观。
感谢 Nikolai Kosjar 帮助设置了这一切!