Gr-Disarray
Gr-Disarray

Reputation: 534

Having target_compile_definitions apply to static library that is linked in

As part of the build process we produce two executables from our source exe1 and exe2. These executables link in a static library which has to have some extra code run in the case of exe2 so the static library has the following code (changed for brevity).

#ifdef _EXE2_

   Do certain stuff

#endif

now our CMakeLists.txt defines two targets exe1 and exe2, I tried the following change so that __EXE2_ would get defined only for exe2

target_compile_definitions(exe2 PUBLIC _EXE2_)

however it seems to me that the above line just adds this definition to exe2 and the static lib does not get that definition. Is there a way around this? or do i have to solve this with an exe configuration file. This is c++ code on linux if that helps in any way.

Upvotes: 2

Views: 1060

Answers (1)

rustyx
rustyx

Reputation: 85341

Why would it? The exe depends on the library, not the other way around. Generally, a library has no relation to the modules that include it.

A library is also built once regardless of how many applications include it. That's the whole point of having a library.

If you want you can just add the definitions globally in your main CMakeLists.txt:

add_compile_definitions(_EXE2_)

Note that identifiers beginning with an underscore followed by an uppercase letter are reserved and should not be used.

Upvotes: 3

Related Questions