0

I am using CMake to build a simple C++ project, hereafter named P. The structure of P is quite simple:

  • P/src/
  • P/src/package1
  • P/src/packege2
  • P/src/...
  • P/src/main-app

I would like to collect the libraries in package1, package2, ... in a variable called P_LIBS.

In a first attempt, I tried to collect the libraries available in package1, package2, ... in the variable called P_LIBS initially set in the src/CMakeLists.txt file. However, the updates to P_LIBS made in the CMakeLists.txt of the subfolders are not propagated to the parent folder.

I would rather not write a list of libraries in the main CMakeLists.txt file. I would rather modify such variable while moving in the directory tree.

After a search on the internet I could not find any valid suggestion. If I look at the various Find files, I only see long listings of libraries in their main CMakeLists.txt file.

Is there a way to do what (I hope) I explained above?


Thanks to sakra's link I was able to 'propagate' names up to the parent folder. However, the names I add to the P_LIBS variable are later interpreted as 'library' names, not as reference to CMake targets. In other words, if

P_LIBS = {a, b}

the 'a' and 'b' are interpreted as the library names, i.e. CMake generates:

gcc [...] -l a -o exe

instead of

gcc [...] /path/to/a.o -o exe 

(.o or other extensions)

Antonio Sesto
  • 2,868
  • 5
  • 33
  • 51
  • Could you share the relevant `CMakeLists.txt` files? (In particular, how do you include the other cmake files, is it via `include` or `add_subdirectory`? – Lindydancer Dec 11 '12 at 15:10
  • You should take a look at the answer provided here: http://stackoverflow.com/questions/9673326/cmakelists-txt-files-for-multiple-libraries-and-executables – Ryan Maloney Dec 11 '12 at 15:28
  • 1
    Also see http://stackoverflow.com/questions/6891447/cmake-variable-scope-add-subdirectory – sakra Dec 11 '12 at 15:50

1 Answers1

1

You are propably constructing the targets list as a string, try to make them a list instead. For example:

# in package1/CMakeLists.txt
set(P_LIBS ${P_LIBS} a b PARENT_SCOPE) 
ronkot
  • 5,915
  • 4
  • 27
  • 41