简体   繁体   中英

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?

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.

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. I presume that all this is managed by make , so it's not like you actually need to type out the options every time.

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

-Wall is primarily a preprocessor option, but there is also a reference for the libraries. See here

-Wextra appears to be strictly a c++ preprocessor option. See here

-O2 is a compiler optimization level setting. See here

So to answer your precise question, only -Wall would possibly help during your link step. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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