使用qmake / gcc有条件地禁用警告?

时间:2022-09-06 08:07:55

I am involved with a software project written in Qt and built with qmake and gcc on Linux. We have to link to a third-party library that is of fairly low quality and spews tons of warnings. I would like to use -W -Wall on our source code, but pass -w to the nasty third-party library to keep the console free of noise and clutter so we can focus on our code quality.

我参与了用Qt编写的软件项目,并在Linux上使用qmake和gcc构建。我们必须链接到质量相当低并且发出大量警告的第三方库。我想在我们的源代码上使用-W -Wall,但是将-w传递给讨厌的第三方库以保持控制台没有噪音和杂乱,因此我们可以专注于我们的代码质量。

In qmake, is there a way to conditionally add CFLAGS/CXXFLAGS to certain files and libraries?

在qmake中,有没有办法有条件地将CFLAGS / CXXFLAGS添加到某些文件和库中?

5 个解决方案

#1


Jonathan, I think the problem is where your source files are including header files from 3rd party libraries, and you want to switch off the warnings for the latter.

Jonathan,我认为问题在于您的源文件包含来自第三方库的头文件,并且您想要关闭后者的警告。

Kevin, i think you can use pragmas to control warnings : gcc diagnostic pragmas

Kevin,我认为你可以使用pragma来控制警告:gcc诊断pragma

You could add these before and after any #includes for 3rd party libs.

您可以在第三方库的任何#includes之前和之后添加这些。

#2


What if you include your library using -isystem.

如果使用-isystem包含库,该怎么办?

In the project file e.g.:

在项目文件中例如:

QMAKE_CXXFLAGS += -isystem /usr/local/boost_1_44_0

#3


Normally, you'd build the third-party library in a separate directory from your own code, so you would have a different makefile for it, so you could put a different set of flags for that compilation.

通常,您将在与您自己的代码不同的目录中构建第三方库,因此您将拥有不同的makefile,因此您可以为该编译添加一组不同的标志。

If you've mixed the third-party library code with your own code, you have set yourself up for a maintenance nightmare.

如果您已将第三方库代码与您自己的代码混合在一起,那么您已经为维护噩梦做好了准备。

#4


Kevin,

qmake CONFIG+=debug QMAKE_CXXFLAGS_WARN_ON=-w QMAKE_CFLAGS_WARN_ON=-w

should do (use CONFIG+=release if you wish...)

应该这样做(如果你愿意,可以使用CONFIG + = release ...)

#5


As Martin wrote adding the include directory via

正如Martin写道添加include目录

QMAKE_CXXFLAGS += -isystem ...

suppresses warnings just in the respective headers. No need to disable warnings for any source files of your project (or even project-wide) or mess with #pragmas or wrappers files.

仅在相应的标题中抑制警告。无需为项目的任何源文件(甚至项目范围)禁用警告,也不需要使用#pragmas或包装文件。

Note that if you're using QtCreator you'll still (i.e. additionally) want add the directory to INCLUDEPATH so the indexer picks up the headers.

请注意,如果您正在使用QtCreator,您仍然(即另外)想要将目录添加到INCLUDEPATH,以便索引器选择标头。

#1


Jonathan, I think the problem is where your source files are including header files from 3rd party libraries, and you want to switch off the warnings for the latter.

Jonathan,我认为问题在于您的源文件包含来自第三方库的头文件,并且您想要关闭后者的警告。

Kevin, i think you can use pragmas to control warnings : gcc diagnostic pragmas

Kevin,我认为你可以使用pragma来控制警告:gcc诊断pragma

You could add these before and after any #includes for 3rd party libs.

您可以在第三方库的任何#includes之前和之后添加这些。

#2


What if you include your library using -isystem.

如果使用-isystem包含库,该怎么办?

In the project file e.g.:

在项目文件中例如:

QMAKE_CXXFLAGS += -isystem /usr/local/boost_1_44_0

#3


Normally, you'd build the third-party library in a separate directory from your own code, so you would have a different makefile for it, so you could put a different set of flags for that compilation.

通常,您将在与您自己的代码不同的目录中构建第三方库,因此您将拥有不同的makefile,因此您可以为该编译添加一组不同的标志。

If you've mixed the third-party library code with your own code, you have set yourself up for a maintenance nightmare.

如果您已将第三方库代码与您自己的代码混合在一起,那么您已经为维护噩梦做好了准备。

#4


Kevin,

qmake CONFIG+=debug QMAKE_CXXFLAGS_WARN_ON=-w QMAKE_CFLAGS_WARN_ON=-w

should do (use CONFIG+=release if you wish...)

应该这样做(如果你愿意,可以使用CONFIG + = release ...)

#5


As Martin wrote adding the include directory via

正如Martin写道添加include目录

QMAKE_CXXFLAGS += -isystem ...

suppresses warnings just in the respective headers. No need to disable warnings for any source files of your project (or even project-wide) or mess with #pragmas or wrappers files.

仅在相应的标题中抑制警告。无需为项目的任何源文件(甚至项目范围)禁用警告,也不需要使用#pragmas或包装文件。

Note that if you're using QtCreator you'll still (i.e. additionally) want add the directory to INCLUDEPATH so the indexer picks up the headers.

请注意,如果您正在使用QtCreator,您仍然(即另外)想要将目录添加到INCLUDEPATH,以便索引器选择标头。