1
votes

I am new to CMake and am having a problem with how the files created by CMake are being organized.

First I will detail the file tree. I am trying to build a simple library with an example program from two source directories (Grid and examples) and a build directory. So, I have three CmakeLists.txt file. So, the top directory looks like this: CMakeLists.txt, /Grid, /examples, and /build.

This CMakeLists.txt contains:

project(talyfem)
cmake_minimum_required(VERSION 2.8.4)
set(CMAKE_CXX_FLAGS "-g")
add_subdirectory(examples Grid) 

Within the Grid folder CmakeLists.txt is:

add_library(${PROJECT_NAME} FEMElm.cpp Grid.cpp GridField.cpp SuperGrid.cpp SuperGrid3D.cpp)

Within the examples folder CmakeLists.txt is:

set(EXAMPLES example1)
foreach(example ${EXAMPLES})
   add_executable(${example} ${example}.cpp)
  target_link_libraries(${example} ${PROJECT_NAME})
endforeach(example)

I run CMake from the build directory as: Cmake ../ When I do this it creates a strange file structure that I can't figure out and doesn't seem correct. First, it creates a ../build/CMakeFiles folder, which is what I would expect. But, then it creates a ../build/Grid folder and within this folder is another CMakeFiles folder that then contains an example1.dir folder. If I switch the order of the sub-directories, which Grid first and examples second the opposite occurs with Grid.dir being inside a examples sub-directory.

This doesn't seem correct and I would appreciate any assistance in fixing this problem.

I would expect that ../build/CMakeFiles should contain a Grid.dir and an examples.dir folder.

1
I have a copy of Mastering CMake, so if the answer is in there I just need a page number.slaughter98

1 Answers

4
votes

The error is in the way you use add_subdirectory. From CMake Docs:

add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])

So when you give add_subdirectory two arguments the first is interpreted as a source directory, and the second as the directory where this source should be built.

What you want is to call add_subdirectory twice:

add_subdirectory(examples)
add_subdirectory(Grid)