[CMake] Compiling two versions of file/program

Eric Noulard eric.noulard at gmail.com
Mon Jan 20 10:35:47 EST 2014


If you need two versions of the same executable you'll have to do
something like;

add_executable(exe1 same_source.cpp)
add_executable(exe2 same_source.cpp)
set_target_properties(exe2 PROPERTIES COMPILE_FLAGS "-DFOO=1")

then exe2 will be compiled with -DFOO.

you can put a specific property on a specific source file using
set_source_files_properties([file1 [file2 [...]]]
                             PROPERTIES prop1 value1
                             [prop2 value2 [...]])

but AFAIK you cannot set that for a specific target.

if you have several source file in your add_executable statement and
only one is concerned with the
specific -D flag then may be you can use two  "object" library
see add_library(<name> OBJECT <src>...)

and use set_target_properties on this OBJECT library

2014/1/20 Rob McDonald <rob.a.mcdonald at gmail.com>:
> My project can be compiled with support for graphics, or in a completely
> headless mode with no dependencies on any graphics libraries.  By and large,
> this is accomplished with the same source code and a very few #define 's.
>
> We also have an API/Library mode with no main(), but that can have graphics
> or not.
>
> I'd like to use CMake to compile both versions of these in one go.  However,
> I don't see any way to make a -DFOO for just a single file, or to compile
> two versions of a single object to then be used later.
>
> I think the workaround will be to have two CMakeLists.txt a level deeper
> than the file with main().
>
>
> \maindir
>   CMakeLists.txt_1
>   main.cpp
> \maindir\graphics
>   CMakeLists.txt_2
> \maindir\headless
>   CMakeLists.txt_3
>
> Where...
> CMakeLists.txt_1 is basically:
> add_directory( graphics )
> add_directory( headless )
>
> And then _2 and _3 set the -DFOO at their level and include the source with
> a ../main.cpp
>
> Is there another/better way?
>
> Rob
>
>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake



-- 
Erk
L'élection n'est pas la démocratie -- http://www.le-message.org


More information about the CMake mailing list