[CMake] add_executable and extension of source file

Kris Thielemans kris at asc.uk.com
Mon Feb 27 05:12:09 EST 2012


Thanks Michael

The behaviour I was asking about is resolved. At some point in my
CMakeFiles.txt, it didn't work for my user on MacOSX, but apparently it does
work now, so I (or he) must have had another mistake in the files earlier
on. But you have made some helpful suggestions, so my comments are below

> -----Original Message-----
> From: cmake-bounces at cmake.org [mailto:cmake-bounces at cmake.org] On
> Behalf Of Michael Hertling
> Sent: 24 February 2012 22:15
> To: cmake at cmake.org
> Subject: Re: [CMake] add_executable and extension of source file
> 
> 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.
> 

Seems that I didn't explain myself properly. My files do have an extension,
and I want this to be passed on to the compiler. It seems that CMake
actually does this for add_executable (i.e. you can do "add_executable
my_file my_file" and it will check if there's a my_file.c etc). (As I
mentioned above, this does after all seem to work on MacOSX as well). 

However, as it isn't documented clearly, and not entirely safe anyway, I
think I'll replace it with your suggestion below.


> > 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()
> 

This sounds good to me! Thanks

> > 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.
> 

Agreed. I suppose if I don't add the extension, something like the above is
effectively what cmake is doing. But passing extensions to specify the
complete filename seems a better way of doing things.

Thanks again!

Kris




More information about the CMake mailing list