[CMake] Adding an individual define to each source file of a library

Eric Noulard eric.noulard at gmail.com
Thu Aug 23 08:39:41 EDT 2018


Le jeu. 23 août 2018 à 13:02, George PF <george.p.f at mail.com> a écrit :

> Hello,
>
> following "modern cmake" conventions I want to create a library where
> every single file is compiled with an individual define (-D_fileX_,
> required for a macro which integrates code into every translation unit).
>
> So following this
>
>     add_library(mylib SHARED file1.c file2.c)
>     target_link_libraries(mylib PUBLIC otherlib)
>
> I'd like to iterate over all "mylib" source file targets and modify them
> so that the compiler is called like this:
>
>     cc -c -o file1.o file1.cc -D_file1_define_
>     cc -c -o file1.o file2.cc -D_file2_define_
>     etc.
>
> Is this possible?
>

You can retrieve the sources associated with a target using SOURCES
properties.
https://cmake.org/cmake/help/latest/prop_tgt/SOURCES.html

then
https://cmake.org/cmake/help/latest/command/foreach.html
with
https://cmake.org/cmake/help/latest/command/set_property.html
or
https://cmake.org/cmake/help/latest/command/set_source_files_properties.html

for setting property:
https://cmake.org/cmake/help/latest/prop_sf/COMPILE_DEFINITIONS.html

on each source file.

not sure why you want to do that you know that compiler already defined
many "standard" macro for you, like __FILE__
https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html#Standard-Predefined-Macros
?

-- 
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180823/f580671f/attachment.html>


More information about the CMake mailing list