gcc 如何忽略包含的第三方库上的“-W”消息?

z9ju0rcb  于 2023-11-19  发布在  其他
关注(0)|答案(1)|浏览(140)

我经常在我的C++项目中包含外部/第三方库,几乎每次编译代码时它们都包含-W警告消息。
它们位于lib/*文件夹中。特别是Makefile,这就是我所包含的:

# Add .h and .hpp to the preprocessor build
HEADERS += $(wildcard lib/*.h)
HEADERS += $(wildcard lib/*.hpp)
HEADERS += $(wildcard lib/**/*.h)
HEADERS += $(wildcard lib/**/*.hpp)
HEADERS += $(wildcard lib/**/**/*.h)
HEADERS += $(wildcard lib/**/**/*.hpp)
HEADERS += $(wildcard src/*.h)
HEADERS += $(wildcard src/*.hpp)
HEADERS += $(wildcard src/**/*.h)
HEADERS += $(wildcard src/**/*.hpp)

# Add .c and .cpp files to the build
SOURCES += $(wildcard lib/*.c)
SOURCES += $(wildcard lib/*.cpp)
SOURCES += $(wildcard lib/**/*.c)
SOURCES += $(wildcard lib/**/*.cpp)
SOURCES += $(wildcard lib/**/**/*.c)
SOURCES += $(wildcard lib/**/**/*.cpp)
SOURCES += $(wildcard src/*.c)
SOURCES += $(wildcard src/*.cpp)
SOURCES += $(wildcard src/**/*.c)
SOURCES += $(wildcard src/**/*.cpp)

# FLAGS will be passed to both the C and C++ compiler
FLAGS +=
CFLAGS +=
CXXFLAGS += $(sort $(foreach HEADERS, $(HEADERS), -I$(dir $(HEADERS))))

字符串
有没有可能指定(我在Windows上使用MingGW,所以“gcc”),对于那个lib/*文件夹,忽略警告消息?保持我的代码/编译干净?它非常无聊的“混乱”与其他库警告为什么编译和检查状态。

wbgh16ku

wbgh16ku1#

您应该能够通过将编译划分为多个部分并应用不同的标志来抑制特定文件的警告。
例如

# Your sources and headers
SRC_HEADERS := $(wildcard src/*.h) $(wildcard src/*.hpp) $(wildcard src/**/*.h) $(wildcard src/**/*.hpp)
SRC_SOURCES := $(wildcard src/*.c) $(wildcard src/*.cpp) $(wildcard src/**/*.c) $(wildcard src/**/*.cpp)

# Third-party libraries' sources and headers
LIB_HEADERS := $(wildcard lib/*.h) $(wildcard lib/*.hpp) $(wildcard lib/**/*.h) $(wildcard lib/**/*.hpp) $(wildcard lib/**/**/*.h) $(wildcard lib/**/**/*.hpp)
LIB_SOURCES := $(wildcard lib/*.c) $(wildcard lib/*.cpp) $(wildcard lib/**/*.c) $(wildcard lib/**/*.cpp) $(wildcard lib/**/**/*.c) $(wildcard lib/**/**/*.cpp)

# FLAGS for your sources
SRC_FLAGS := 
SRC_CFLAGS :=
SRC_CXXFLAGS := $(sort $(foreach HEADERS, $(SRC_HEADERS), -I$(dir $(SRC_HEADERS))))

# FLAGS for third-party libraries (suppress all warnings using -w)
LIB_FLAGS := -w
LIB_CFLAGS := -w
LIB_CXXFLAGS := $(sort $(foreach HEADERS, $(LIB_HEADERS), -I$(dir $(LIB_HEADERS))))

# Targets and rules
...

# Rule for compiling your sources
%.o: %.cpp
    $(CXX) $(SRC_FLAGS) $(SRC_CXXFLAGS) -o $@ -c $<

# Rule for compiling third-party libraries
lib/%.o: lib/%.cpp
    $(CXX) $(LIB_FLAGS) $(LIB_CXXFLAGS) -o $@ -c $<

字符串
注意-w将禁止所有警告。如果你想有选择地禁用特定的警告,你可以使用特定的标志,如-Wno-<specific-warning>
对于包含在源代码中的库头文件,事情就有点棘手了。
一种方法是使用#pragma s来禁用包含头的警告。
例如

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wall"
#pragma GCC diagnostic ignored "-Wextra"
#include "lib/whatever.hpp"
#pragma GCC diagnostic pop


这可能有点乏味,但希望比解决包含的标题中的每个警告更乏味=)

相关问题