在 Qt Creator 中哪里可以将参数传递给编译器?

发布于 2024-12-13 15:53:52 字数 50 浏览 2 评论 0原文

在 Qt Creator 中哪里可以将参数传递给编译器?
其实并不是那么明显。

Where in Qt Creator do I pass arguments to a compiler?
It isn't really that obvious.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

回首观望 2024-12-20 15:53:52

根据您的构建系统,它位于您的 qmake 项目文件< /a>(.pro,新项目的标准)或 CMake 文件(CMakeLists.txt,由 KDE 和其他几个项目使用)。

使用 .pro:

QMAKE_CXXFLAGS += -O2

使用 CMake:

set( CMAKE_CXX_FLAGS "-g -Wall")

Depending on your build system it's either in your qmake project file(.pro, standard for new projects) or in one of the CMake files (CMakeLists.txt, used by KDE and several other projects).

Using .pro:

QMAKE_CXXFLAGS += -O2

Using CMake:

set( CMAKE_CXX_FLAGS "-g -Wall")
剑心龙吟 2024-12-20 15:53:52

要添加编译器标志,请打开 .pro 文件并添加如下行:

QMAKE_CXXFLAGS += -std=c++0x

对于标准标志(如调试与发布等),为了平台和编译器独立性,您应该尝试使用预定义的 qmake 选项(请参阅 QMake 文档) ,因为 QMake 会将它们映射到编译器特定的标志。

To add compiler flags, open your .pro file and add a line like this:

QMAKE_CXXFLAGS += -std=c++0x

For standard flags like debug vs. release etc. you should try to use the predefined qmake options (see QMake documentation) for the sake of platform and compiler-independency, as QMake will map them to the compiler-specific flags.

你好,陌生人 2024-12-20 15:53:52

如果您的目的是预编译一些源代码,您可以这样做:

/A/ 在您的 .pro 文件中,您可以添加如下行:

DEFINES += HOPLA

/B/ 在您的 .cpp 或 .h 文件中,您可以像这样使用它

#ifdef HOPLA
// Do something
#else
// Do something different
#endif

If your intention is to precompile some source code you can do like this:

/A/ In your .pro file you can add a line like this:

DEFINES += HOPLA

/B/ In you .cpp or .h file you can use it like this

#ifdef HOPLA
// Do something
#else
// Do something different
#endif
清风不识月 2024-12-20 15:53:52

对于 C 项目,在 .pro 文件中添加以下行

QMAKE_CFLAGS += -std=c99

for C projects, add the following line in .pro file

QMAKE_CFLAGS += -std=c99
或十年 2024-12-20 15:53:52

作为选定的答案点,对于基于 CMake 的项目,您可以编辑 CMakeLists.txt 并设置编译器的标志,对于这种情况,我有一个关于如何在 QtCreator 上添加标志的图片演示/CMake.

我想添加“-pedantic”标志,它会警告扩展,而不会在执行程序时抛出错误,下面是如何在使用 QtCreator 时在 CMake 上启用编译器标志的图解示例:

输入图像描述此处

了解更多背景信息:

在下面的示例中,我在运行时设置静态数组的大小,这只能通过可变长度数组功能实现,该功能在 C99 中可用,但从 C11 开始定义为可选功能。如果编译器没有使用 -pedantic 标志,编译代码后将不会显示警告。

As the elected answer points, for CMake based projects, you can edit the CMakeLists.txt and set the flags for the compiler, and for this case, I have a pictorial demonstration on how to add flags on QtCreator/CMake.

I wanted to add the '-pedantic' flag, which warns about extensions, without throwing errors while executing the program, and here's pictorial example of how to enable compiler flags on CMake while using QtCreator:

enter image description here

For more context:

On the example below, I'm setting the size of a Static Array at Runtime, something that is only possible with Variable-length Array feature, which is available at C99, but defined as optional feature starting from C11. Without -pedantic flag being available for the compiler, the warning would't be displayed after compiling the code.

脸赞 2024-12-20 15:53:52

在 .pro 文件中,您可以添加修改 make 行为的变量,例如,如果您尝试执行以下命令:

g++ -Wall -I/usr/include/cppconn -o exe main.cpp -L/usr/lib -lmysqlcppconn

您必须在 .pro 文件中添加以下行

INCLUDEPATH += /usr/include/cppconn
LIBS += -L/usr/lib -lmysqlcppconn

检查下图。
输入图片此处描述

有关 QT IDE 使用的可用变量的更多信息,您可以访问以下链接,其中对每个变量进行了更详细的解释。 Qt 文档:变量

in the .pro file you can add variables which modify the make behavior for example, if you try to execute the following command:

g++ -Wall -I/usr/include/cppconn -o exe main.cpp -L/usr/lib -lmysqlcppconn

you must add the following lines in the .pro file

INCLUDEPATH += /usr/include/cppconn
LIBS += -L/usr/lib -lmysqlcppconn

Check the image below.
enter image description here

For more information on the available variables that QT IDE uses, you can visit the following link where they explain in more detail each one. Qt Documentation: Variables

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文