[CMake] shell in Unix Makefiles

Michael Wild themiwi at gmail.com
Thu Apr 22 09:56:26 EDT 2010


On 22. Apr, 2010, at 15:29 , naryniecki wrote:

> 
> 
> 
> Dnia 22 kwietnia 2010 15:04 Magnus Therning <magnus at therning.org> napisał(a):
> 
>> On Thu, Apr 22, 2010 at 10:23, naryniecki <naryniecki at o2.pl> wrote:
>>> Hi,
>>> 
>>> I know there is such possibility, but I have also few directories with
>>> which are compiled and linked in "normal" way. In such situation I
>>> would have to create custom target for this big directory. But I had
>>> problem how to create target which say: compile all *.cpp files from
>>> directory to .o files.  I tried to use CMAKE_C_COMPILE_OBJECT somehow
>>> or CMAKE_C_FLAGS but it didn't work. I had "\" signs in CMAKE_C_FLAGS
>>> and couldn't remove. I wrote to this list some time ago but no answer.
>> 
>> I'm not quite sure I understand what you are trying to do.  AFAIU your
>> goal is to do the following:
>> 
>> - compile every C++ file in the directory
>> - link the resulting object files into a library
>> 
>> Is that correct?
>> 
> Yes, exactly.
> For each directory I have:
> FILE(GLOB_RECURSE FILES sources/Ald/*.cpp)
> add_library(Ald STATIC ${FILES})
> 
> but one directory has to much files and I can't change it. 
> 
> br
> Marek

Try to stay away from GLOB_RECURSE. It is evil (for one, CMake is not automatically rerun when you add/remove files). If there are too many files to list in the CMakeLists.txt, create e.g. files.cmake which set a variable (e.g. SRCS) to the list of source files.

A dirty trick to solve your problem would be something like this:

CMakeLists.txt:
###############
set(TOPIC_A_SRCS a1.cpp a2.cpp a3.cpp)
set(TOPIC_B_SRCS b1.cpp b2.cpp)
set(TOPIC_C_SRCS c1.cpp c3.cpp c4.cpp)
set(SRCS)
foreach(topic A B C)
  set(TXT "/* AUTOMATICALLY GENERATED! DO NOT EDIT! */\n\n")
  foreach(f ${TOPIC_${topic}_SRCS})
    get_filename_component(ff "${f}" ABSOLUTE)
    set(TXT "#include \"${ff}\"\n")
  endforeach()
  configure_file(wrapper.cpp.in
    "${CMAKE_CURRENT_BINARY_DIR}/wrapper_${topic}.cpp"
    @ONLY)
  list(APPEND SRCS "${CMAKE_CURRENT_BINARY_DIR}/wrapper_${topic}.cpp")
endforeach()
add_library(super_large ${SRCS})
###############


wrapper.cpp.in:
###############
@TXT@
###############

The big drawback of this is that if you change a file, the whole wrapper which includes this file will be recompiled. Also, dependency scanning and preprocessing will slightly (probably not noticeable) increase compilation times.

HTH

Michael


More information about the CMake mailing list