[CMake] add_executable and extension of source file

Kris Thielemans kris at asc.uk.com
Fri Feb 24 12:16:25 EST 2012


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)

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

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

Any suggestions?

Thanks

Kris



More information about the CMake mailing list