简体   繁体   English

使用gcc使用-D选项编译多个文件

[英]compiling multiple files with -D option using gcc

I have a project with multiple .cpp and .h files. 我有一个包含多个.cpp和.h文件的项目。 I have a file called Globals.h which is included in all .cpp files. 我有一个名为Globals.h的文件,该文件包含在所有.cpp文件中。 Now when compiling this file I use some -D options. 现在,在编译此文件时,我使用一些-D选项。 These options affect all files not just Globals.h. 这些选项不仅影响Globals.h,还影响所有文件。

In my makefile I had to use the same -D options when compiling every file. 在我的makefile中,编译每个文件时必须使用相同的-D选项。 Of course when removing those options some "undefined" errors arise. 当然,删除这些选项时,会出现一些“未定义”的错误。

This means every time I change one of these options I have to recompile the whole project. 这意味着每次我更改这些选项之一时,都必须重新编译整个项目。 Is there a way that would let me compile ONLY Globals.h with these options? 有没有一种方法可以让我使用这些选项仅编译Globals.h?

This is a part from my makefile: lines that create Globals.o : 这是我的makefile的一部分:创建Globals.o的行:

Globals.o : $(GlobalsLib) $(GlobalsInc)
       g++ -c -Wall $(CodeDefined) $(UserDefined) $(GlobalsLib) -std=c++0x -o Globals.o

$(GlobalsLib) and $(GlobalsInc) are paths to files Globals.h and Globals.cpp $(CodeDefined) and $(UserDefined) are -U and -D options this code is for creating another .o file: $(GlobalsLib)$(GlobalsInc)是文件Globals.hGlobals.cpp路径。 $(CodeDefined)$(UserDefined)是-U和-D选项,此代码用于创建另一个.o文件:

NT_FFT_Decomp.o : $(GlobalsLib) $(GlobalsInc) $(NT_DecompLib) $(NT_DecompInc)
       g++ -c -Wall $(CodeDefined) $(UserDefined) $(NT_DecompLib) -std=c++0x -o NT_FFT_Decomp.o

(NT_DecompLib) $(NT_DecompInc) are also paths to files. (NT_DecompLib) $(NT_DecompInc)也是文件的路径。 Notice that I had to add the same options in the g++ command. 注意,我必须在g ++命令中添加相同的选项。 Is there anyway around that? 反正周围有吗?

No, that's not how headers work. 不,标题不是这样工作的。

Headers are not separate compilation units. 标头不是单独的编译单元。 Perhaps this will make more sense if you precompile all of your files, via the -E flag (I think). 如果通过-E标志预编译所有文件(我认为),也许这会更有意义。 This will leave you with a bunch of .i (I think) files that will actually be compiled. 这将给您留下一堆实际上将被编译的.i (我认为)文件。 Note that the headers are now gone, but their contents are spread across the different source files. 请注意,标头现已消失,但其内容分散在不同的源文件中。 That is what you actually have to recompile each time. 那就是您实际上每次必须重新编译的内容。

There are ways to minimize source recompilation. 有一些方法可以最大程度地减少源代码重新编译。 For instance this situation: 例如这种情况:

foo.h foo.h

int foo = 1;

Can be turned into this one: 可以变成这样一个:

foo.h foo.h

extern int foo;

foo.cpp foo.cpp

int foo = 1;

So you can now change the value of foo without recompiling. 因此,您现在可以更改foo的值而无需重新编译。

But unfortunately, it also sounds like your header is just too big. 但是不幸的是,听起来您的标头太大了。 If you throw everything in one header you're going to be doing a lot of recompilation. 如果将所有内容都放在一个标头中,则将需要大量重新编译。 Factor more specifically and this may be more avoidable. 更具体地讲因数,这可能是可以避免的。

Also (or alternatively?) use precompiled headers to reduce compilation time. 同样(或替代?)使用预编译头可以减少编译时间。 I've never programmed with them but they solve this problem I understand. 我从未与他们编程过,但是他们解决了我理解的问题。

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

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