2

In a project using cmake, I build two versions of a library, one statically and one dynamically linked. For a single source file, I want to pass a different compile definition (i.e. -Dfoo=bar) when compiling for the shared library only.

I know about set_target_properties where I can use the COMPILE_DEFINITIONS for a single source, but I don't know how to add that definition only for the shared library.

How can this be done?

Edit

To clarify how this question is different, I am already making two versions of the same library.

add_library(static_lib STATIC foo.cpp bar.cpp)
add_library(dyn_lib SHARED foo.cpp bar.cpp)

What I would like to do is to add the target property that foo.cpp is compiled with -Dbaz=True only when compiling foo.cpp for dyn_lib.

pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110
  • 1
    The common pattern I've seen in many projects is to have 2 targets - for static library and for shared one. With 2 targets you can set different flags for them. – arrowd Sep 25 '17 at 13:31
  • @arrowd I'm rather ignorant of cmake. I only know how to add the source targets to the library as `add_library(${lib_name} (STATIC|SHARED) ${list of sources})`. Do you mean do some level of indirection between the name of source file and adding in the `add_library` command? – pythonic metaphor Sep 25 '17 at 13:43
  • 2
    I meant, you should make 2 calls to `add_library()` - one for static lib and another for shared. – arrowd Sep 25 '17 at 13:48
  • 1
    Possible duplicate of [Is it possible to get CMake to build both a static and shared version of the same library?](https://stackoverflow.com/questions/2152077/is-it-possible-to-get-cmake-to-build-both-a-static-and-shared-version-of-the-sam) – Florian Sep 25 '17 at 14:10

1 Answers1

3

The simplest way add the definition -Dbaz=True for objects compiled for the library target dyn_lib is to use target_compile_definition().

target_compile_definition(dyn_lib PRIVATE -Dbaz=True)

This effectively is a shorter version of setting COMPILE_DEFINITIONS property for dyn_lib target.

set_target_properties(dyn_lib PROPERTIES COMPILE_DEFINITIONS -Dbaz=True)

To compile a single source file with the definition -Dbaz=True use set_source_files_properties().

set_source_files_properties(file.cpp PROPERTIES COMPILE_DEFINITIONS -Dbaz=True)
Paweł Bylica
  • 3,780
  • 1
  • 31
  • 44
  • This defines `baz` for all sources being compiled into `dyn_lib`. How would I define `baz` only for a single source? – pythonic metaphor Sep 26 '17 at 21:43
  • 1
    I am a bit confused of your comment. Why would you expect symbols being valid only for specific source files, while source files generally are dependent on your target of the specific library you add? Symbols can be evaluated via prprocessor directives and they are just there (globally for a project) or not. How you use them in sources is a matter of forks in those preprocessor directives in the different sources. – woodz Feb 04 '21 at 18:39
  • @pythonicmetaphor, I added answer to your question. – Paweł Bylica Feb 05 '21 at 12:46