简体   繁体   English

C Makefile,使用不同的规则编译项目的各个部分

[英]C Makefile, Compile parts of the project with different rules

I have a project containing source and header files organized in N folders (let's call them folder1 , folder2 , ..., folderN ) 我有一个项目,其中包含组织在N文件夹中的源文件和头文件(我们称它们为folder1folder2 ,..., folderN

I want to compile folder1 with -Wall but the rest of them with -w . 我想用-Wall来编译folder1 ,而其余​​的要用-w来编译。 The problem is that the other folders (2 to N) contain only header files which are included in some of the sources in folder1. 问题在于其他文件夹(2到N)仅包含头文件,这些头文件包含在folder1的某些源中。

When I compile folder1 , -Wall will apply also to the headers in that folder and I do not want that. 当我编译folder1-Wall也将应用于该文件夹中的标头,但我不希望如此。

This is the makefile I used so far: 这是我到目前为止使用的makefile:

CC=gcc
LIBS=-iquotefolder2 ... -iquotefoldern

SOURCES=$(wildcard folder1/*.c) $(wildcard folder2/*.c) ... $(wildcard foldern/*.c)
OBJECTS=$(SOURCES:.c=.o)

folder1/%.o: folder1/%.c
    $(CC) -Wall $(LIBS) -c -o $@ $^

folder2/%.o: folder2/%.c
    @$(CC) -w $(LIBS) -c -o $@ $^
...

folderN/%.o: folderN/%.c
    @$(CC) -w $(LIBS) -c -o $@ $^

lib.dll: $(OBJECTS)
    @$(CC) -w -shared -Wl,-soname,$@ -o $@ $^

all: lib.dll

Is there any way to achieve this? 有什么办法可以做到这一点? I also tried to compile folder2 to folderN in a lib.a and link that library while compiling folder1 but gcc will (obviously) try to include the headers 我还尝试在lib.a中将folder2编译为folderN并在编译folder1时链接该库,但是gcc(显然)将尝试包含标头

You can't do that with Makefile. 您不能使用Makefile做到这一点。 Makefile processes only source files, so you can provide flags in Makefile on source file granularity only. Makefile仅处理源文件,因此您只能在Makefile中以源文件粒度提供标志。 In other words, gcc is invoked on source files, and headers are not mentioned in gcc invocations. 换句话说, gcc在源文件上被调用,并且在gcc调用中未提及标头。

However, you can instruct gcc to adjust warnings in different files: 但是,您可以指示gcc调整其他文件中的警告:

  1. Pass -isystem <dir> flag to gcc - this will make gcc treat headers located dir as system headers. -isystem <dir>标志传递-isystem <dir>这将使gcc将位于dir的标头视为系统标头。 gcc by default does not issue any warnings in system headers. 默认情况下, gcc在系统标题中不发出任何警告。

  2. Use #pragma GCC diagnostic to ignore warnings from source files: 使用#pragma GCC diagnostic可忽略源文件中的警告:

Source: 资源:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wall"
#include "folder2/bad-header.h"
#pragma GCC diagnostic pop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM