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

George PF george.p.f at mail.com
Thu Aug 23 13:18:32 EDT 2018


> > However, whatever I write into 'set_property()' - no APPEND, single entry,
> > not quoted - does not end up
> > on the compiler command line. Is there a type mismatch which is silently
> > ignored?
> >
> 
> Or you are doing this in a directory which is not the one where the target
> is defined and from the doc:
> $ cmake --help-command set_property
> 
> ...
> ``SOURCE``
>   Scope may name zero or more source files.  Note that source
>   file properties are visible only to targets added in the same
>   directory (CMakeLists.txt).
> 
> ...

This is all in the same directory, full setup and test:

    % cmake --version
    cmake version 3.12.0   [..]
    % mkdir mylib && cd mylib
    mylib% touch file1.c file2.c
    mylib% cat > CMakeLists.txt

    cmake_minimum_required(VERSION 3.12)
    
    add_library(mylib SHARED file1.c file2.c)
    
    get_property(mysrcs TARGET mylib PROPERTY SOURCES)
    foreach(x IN LISTS mysrcs)
        message("at ${x}")
        set_property(SOURCE x APPEND PROPERTY COMPILE_DEFINITIONS "TEST1;TEST2;")
    endforeach(x)
    # ^D

    mylib% mkdir build && cd build && cmake ..
    [..]
    at file1.c
    at file2.c
    -- Configuring done
    [..]
    mylibs/build% grep -r TEST1 . || echo no TEST1
    no TEST1

and 'make VERBOSE=1' also shows no extra -DTEST1 compiler arguments.


More information about the CMake mailing list