简体   繁体   English

gcc 使用警告/优化标志链接目标文件

[英]gcc linking object files with warning/optimization flags

We are compiling a piece of software using generics where files are first made into object files, they are built like so:我们正在使用泛型编译一个软件,其中文件首先被制作成目标文件,它们的构建方式如下:

arm-unknown-linux-gnu-gcc -c -O2 -Wstrict-prototypes -Wdeclaration-after-statement -fsigned-char -I/opt/tm-sdk/include  -mlittle-endian -Wno-trigraphs -fno-strict-aliasing -fno-omit-frame-pointer -march=armv4 -mtune=arm9tdmi -Wall -Wextra -o src/flex.o src/flex.c
...
arm-unknown-linux-gnu-gcc -c -O2 -Wstrict-prototypes -Wdeclaration-after-statement -fsigned-char -I/opt/tm-sdk/include  -mlittle-endian -Wno-trigraphs -fno-strict-aliasing -fno-omit-frame-pointer -march=armv4 -mtune=arm9tdmi -Wall -Wextra -o src/flexdb.o src/flexdb.c

Then they are linked with:然后它们与:

arm-unknown-linux-gnu-gcc -o flex src/flex.o src/flexdb.o src/flexio.o src/flexprotocol.o src/flexsettings.o src/flexstate.o -L/opt/tm-sdk/lib -ltag  -lrt -ltmreader -lsqlite3 -lsha1

My questions is: Do we need to include optimization and warning flags during linking?我的问题是:我们是否需要在链接期间包含优化和警告标志? Would it do anything if -Wall , -Wextra , and -O2 were included when creating the flex binary from the object files?如果在从目标文件创建 flex 二进制文件时包含-Wall-Wextra-O2 ,它会做什么吗?

Edit: Clarifying meaning based on feedback.编辑:根据反馈澄清含义。

Do we need to include optimization and warning flags during this final stage of compilation?我们是否需要在编译的最后阶段包含优化和警告标志?

Of course you don't need to include them for the link stage.当然,您不需要将它们包含在链接阶段。 You already know that, because you don't include them.你已经知道了,因为你包括它们。 But I think what you really want to know is ...但我认为你真正想知道的是......

Would it do anything if -Wall, -Wextra, and -O2 were included when building the flex binary from object files.如果在从目标文件构建 flex 二进制文件时包含 -Wall、-Wextra 和 -O2,它会做任何事情吗?

All or almost all warnings are generated during the compilation stage.所有或几乎所有警告都是在编译阶段生成的。 I don't off-hand know any exceptions, but it's conceivable that there are some.我不知道有任何例外,但可以想象有一些例外。 Thus, it's possible that passing warning-related flags during linking would trigger warnings that otherwise you would not receive.因此,在链接期间传递与警告相关的标志可能会触发警告,否则您将不会收到这些警告。 But that shouldn't affect the compiled binary in any way.但这不应该以任何方式影响编译的二进制文件。

Optimization is different.优化是不同的。 There are optimizations that can be performed at link time, but that might not be performed at the default optimization level.有些优化可以在链接时执行,但可能无法在默认优化级别执行。 Omitting optimization flags from the link command should not break your build, but including them may result in a binary that is faster and / or smaller.省略链接命令中的优化标志不应破坏您的构建,但包含它们可能会导致二进制文件更快和/或更小。

Overall, I see no good reason to avoid passing the same warning and optimization flags during the link step that you do during the compilation steps.总的来说,我认为没有充分的理由避免在链接步骤中传递与编译步骤中相同的警告和优化标志。 If you wish, it's not harmful to pass preprocessor-specific flags (eg -D ) as well, as they'll just be ignored during linking.如果您愿意,传递特定于预处理器的标志(例如-D )也无害,因为它们只会在链接期间被忽略。 I presume that all this is managed by make , so it's not like you actually need to type out the options every time.我认为所有这些都是由make管理的,因此您实际上并不需要每次都输入选项。

否 您只是通过对 gcc 的最终调用来调用链接器,并且 -W 和 -O 标志用于编译器。

-Wall is primarily a preprocessor option, but there is also a reference for the libraries. -Wall 主要是一个预处理器选项,但也有库的参考。 See here这里

-Wextra appears to be strictly a c++ preprocessor option. -Wextra 似乎严格来说是一个 C++ 预处理器选项。 See here这里

-O2 is a compiler optimization level setting. -O2 是编译器优化级别设置。 See here这里

So to answer your precise question, only -Wall would possibly help during your link step.因此,要回答您的确切问题,只有 -Wall 可能会在您的链接步骤中有所帮助。 The other two would not.另外两个不会。 You could test this by building with and without these options, seeing if any additional output is created in the case of warnings and if the code size or execution time differed between builds.您可以通过使用和不使用这些选项进行构建来测试这一点,看看是否在出现警告的情况下创建了任何额外的输出,以及构建之间的代码大小或执行时间是否不同。

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

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