[CMake] add_executable and extension of source file

Michael Hertling mhertling at online.de
Fri Feb 24 17:15:01 EST 2012


On 02/24/2012 06:16 PM, Kris Thielemans wrote:
> Hi
> 
> I have a project where I have C++ and C source files. I'm adding executables
> for this (via macros) like this
> 
> foreach(executable ${SOURCES})
>    add_executable(${executable} ${executable} )
>    target_link_libraries(${executable} ${STIR_LIBRARIES})
> endforeach()
> 
> where ${SOURCES} is a list of sources WITHOUT extension, e.g.
> 
> 	set( SOURCES abs_image  src2)  
> 
> This relies on the fact that cmake should find .cxx and .c etc source files
> for add_executable. At least, I think it should (I found this some tutorial,
> e.g.
> http://www-flc.desy.de/ldcoptimization/documents/talks/CMake_Tutorial.pdf),
> but the doc for add_executable does not seem to mention this behaviour. 
> 
> My current CMake files work fine on Windows and Linux, but I now have a
> MacOSX user who says that it fails. He's running cmake 2.8.7 and when I
> inspect that linking command, it looks like (slightly edited for brevity)
> 
> 	/usr/bin/c++   -O3 -DNDEBUG -ffast-math -Wl,-search_paths_first
> -Wl,-headerpad_max_install_names   
>              -o abs_image  a ../buildblock/libbuildblock.a
> 
> That is, clearly the abs_image.o file is missing on this command line.
> 
> Maybe this "adding a list of known extensions" feature is no longer
> supported? Or is the list of known extensions platform specific? (that would
> be bad)

The gcc manpage states:

<cite>
For any given input file, the file name suffix determines what kind of
compilation is done:

file.c
    C source code which must be preprocessed.
...
other
    An object file to be fed straight into linking.  Any file name with
    no recognized suffix is treated this way.
...
-c  Compile or assemble the source files, but do not link. [...]

    Unrecognized input files, not requiring compilation or assembly,
    are ignored.
</cite>

Thus, AFAICS, CMake handles the extension-less sources correctly, but
gcc freaks out: No extension --> ignore when compiling --> no object
file. IMO, it's a quite bad idea to provide source files without a
proper suffix. However, see gcc's -x switch.

> I guess I will have to set my SOURCE files with the extension, and then
> strip the extension for the executable-name. maybe with something like
> 
> foreach(src ${SOURCES})
>   STRING(REPLACE \(.*\)\..* \1 executable ${src})
>  add_executable(${executable} ${src} )
>  ...
> endforeach()

SET(SOURCES abs_image.cxx src2.c)
...
FOREACH(i IN LISTS SOURCES)
    GET_FILENAME_COMPONENT(j ${i} NAME_WE)
    ADD_EXECUTABLE(${j} ${i})
ENDFOREACH()

> or alternatively find the source file
> 
> foreach(executable ${SOURCES})
>    FILE(GLOB src "*.cxx" "*.c")
>   add_executable(${executable} ${src} )
>    target_link_libraries(${executable} ${STIR_LIBRARIES})
> endforeach()

Do not use FILE(GLOB ...) in a CMakeLists.txt since this makes the
latter unaware of additions/removals/renamings among your sources.
You will most certainly miss a necessary reconfiguration one day.

Regards,

Michael


More information about the CMake mailing list