Kyle Strand
Kyle Strand

Reputation: 16509

Does CMake add_compile_options affect linker options when appropriate?

Some compiler options may actually require extra link options in order to actually be effective. For instance, to enable GCC's Link Time Optimization, the -flto option must be passed both in the compilation command and the link command.

There's a spiffy builtin cmake command for adding compile options but, as far as I know, no corresponding command for link options. Even if there were, needing to specify them explicitly when they're implied by the compile options I'm using would be pretty annoying.

So does add_compile_options(-flto) add -flto to the link command? If not, do I need to set(LINK_FLAGS .... directly?

Upvotes: 5

Views: 2604

Answers (2)

ComicSansMS
ComicSansMS

Reputation: 54737

add_compile_options and target_compile_options will not get passed to the linker.

Use add_link_options instead for this purpose.

On older CMake versions (older than 3.13), you can (ab)use target_link_libraries for this purpose:

Item names starting with -, but not -l or -framework, are treated as linker flags.

Upvotes: 6

Shoaib Ahmed
Shoaib Ahmed

Reputation: 434

This can be done in cmake versions 3.13 onwards using add_link_options().

Upvotes: 1

Related Questions