20

I am very new to CMake. In fact, I am trying it through Kdevelop4 widh C++.

I have the habit of creating subdirs for every namespace I create, even if all the sources must be compiled and linked into a single executable. Well, when i create a directory under kdevelop, it updates CMakeLists.txt with a add_subdirectory command and creates a new CMakeLists.txt under it, but that alone does not add the sources under it to the compilation list.

I have the root CMakeLists.txt as follows:


project(gear2d)

add_executable(gear2d object.cc main.cc)

add_subdirectory(component)

Under component/ I have the sources I want to be compiled and linked to produce the gear2d executables. How can I accomplish that?

CMake FAQ have this entry but if thats the answer I'd rather stay with plain Makefiles.

Is there a way of doing this?

Leonardo
  • 1,834
  • 1
  • 17
  • 23

1 Answers1

24

Adding a subdirectory does not do much more than specify to CMake that it should enter the directory and look for another CMakeLists.txt there. You still need to create a library with the source files with add_library and link it to your executable with target_link_libraries. Something like the following:

In the subdir CMakeLists.txt

set( component_SOURCES ... ) # Add the source-files for the component here
# Optionally you can use file glob (uncomment the next line)
# file( GLOB component_SOURCES *.cpp )below

add_library( component ${component_SOURCES} )

Top-dir CMakeLists.txt

project( gear2d )
add_subdirectory( component )
add_executable( gear2d object.cc main.cc )
target_link_libraries( gear2d component )
André
  • 18,348
  • 6
  • 60
  • 74
  • That did it! Thanks very much. I had other concept for what a library means, in fact what I was expecting just to add the sources in the subdir CMakeListrs.txt just like it was done in the root one, with add_executable. Thanks again. – Leonardo Apr 13 '11 at 07:26
  • This basically adds a new (library) target. Even though this target doesn't get linked itself, problems might arise later on when setting comiler flags or other stuff... Better answer is here: http://stackoverflow.com/questions/8934295/add-source-in-a-subdirectory-to-a-cmake-project – kralyk Apr 28 '12 at 11:48
  • @kralyk: I agree on the new library target. For convencience, I skimmed over "linked into a single executable" and simply wanted to hint on another method of distributing source files over directories and libraries. – André May 02 '12 at 06:53
  • You sir, put an end to banging my head against the wall. Thank you. – winduptoy Oct 06 '15 at 23:31
  • 2
    I would like to add the suggestion to rename the subdir 'component' in the example to 'componentdir'. Just to make clear that `target_link_libraries` expects the target name 'component' from *within* the subdir's CMakeLists.txt. Since this is at least the 3rd place in the web where I found examples with equal names for subdir and link-item, I got a little confused and had to try it out myself... (if perhaps the subdir name would also work for `target_link_libraries` - but it doesn't; by the way you also cannot use a ${...} alias defined in the subdir...) – yau Jan 31 '16 at 00:23
  • Thanks for easy solution !! – Kamble Tanaji Dec 03 '19 at 11:22
  • Thanks @yau - your comment saved me! Was very confused for a bit – Jon Nordby Jan 02 '22 at 23:16