0

I have a directory tree like this:

libs
   support
       db
          csv
       patterns
   support_qt
       helpers
       dialogs

etc.

Now when I do the add_subdirectory in the support level, I can add db and patterns and the files are collected. However in db I added another add_subdirectory referencing the csv, but somehow this is ignored.

In support

set(SUPPORT_SOURCE
    ${CMAKE_CURRENT_SOURCE_DIR}/support_defs.h
    ${CMAKE_CURRENT_SOURCE_DIR}/support_dll_api.h
    ${CMAKE_CURRENT_SOURCE_DIR}/supportlib_namespace.h
    ${CMAKE_CURRENT_SOURCE_DIR}/dll_main.cpp
)

add_subdirectory (db)
add_subdirectory (patterns)

In db

set(SUPPORT_SOURCE ${SUPPORT_SOURCE}
    ${CMAKE_CURRENT_SOURCE_DIR}/column_types.h
    ${CMAKE_CURRENT_SOURCE_DIR}/dbcolumn.h
    ${CMAKE_CURRENT_SOURCE_DIR}/database_login.h
    ${CMAKE_CURRENT_SOURCE_DIR}/database_login.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/type_helper.h
    ${CMAKE_CURRENT_SOURCE_DIR}/type_helper.cpp

    PARENT_SCOPE
)

add_subdirectory(csv)

The above works fine but in csv

set(SUPPORT_SOURCE ${SUPPORT_SOURCE}
    ${CMAKE_CURRENT_SOURCE_DIR}/csv.h
    ${CMAKE_CURRENT_SOURCE_DIR}/csv.cpp

    PARENT_SCOPE
)

But these files are not included in the build. So do I have to put the add_subdirectory calls all into the root file?

Devolus
  • 21,661
  • 13
  • 66
  • 113
  • 1
    The PARENT_SCOPE goes only one level up. I think you are better off with a CACHE INTERNAL, global variable, like this `set(SUPPORT_SOURCE ${SUPPORT_SOURCE} ${CMAKE_CURRENT_SOURCE_DIR}/dbcolumn.h [...] CACHE INTERNAL "")` But I am not sure how it works with a variable set multiple times. – Antonio Jun 04 '15 at 12:04

1 Answers1

0

Just found the solution. I have to put the add_subdirectory before the set command.

add_subdirectory(csv)
set(SUPPORT_SOURCE ${SUPPORT_SOURCE}
    ${CMAKE_CURRENT_SOURCE_DIR}/column_types.h
    ${CMAKE_CURRENT_SOURCE_DIR}/dbcolumn.h
    ${CMAKE_CURRENT_SOURCE_DIR}/database_login.h
    ${CMAKE_CURRENT_SOURCE_DIR}/database_login.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/type_helper.h
    ${CMAKE_CURRENT_SOURCE_DIR}/type_helper.cpp

    PARENT_SCOPE
)
Devolus
  • 21,661
  • 13
  • 66
  • 113
  • For big projects, it's usually fine to use globbing expressions. – Antonio Jun 04 '15 at 12:09
  • But with globbing it would also find generated files, right? Also I have some files which are not included in the lib, but are still in that directory tree. – Devolus Jun 04 '15 at 12:12
  • 1
    @Antonio I personally avoid globbing, see http://stackoverflow.com/a/18538444/678093 – m.s. Jun 04 '15 at 12:14
  • @m.s. Globbing saves a lot of tedious and uninformative cmake code. It has to be done carefully though, on this I agree. For example, we use git hooks to trigger cmake to run at the next build after pulling from the repository. Let's link also [the other, more popular, answer](http://stackoverflow.com/a/1060061/2436175) :) – Antonio Jun 04 '15 at 12:47
  • @Devolus Sure, it will find anything your globbing expression matches. You can decide to have a very wide globbing expression, and exclude the rare extra files (there are plenty of Q/A in this sense: http://stackoverflow.com/q/16449676/2436175 http://stackoverflow.com/q/15550777/2436175 http://stackoverflow.com/q/13114680/2436175). Or you can use a more specific globbing function to get only get the files/directories you need. – Antonio Jun 04 '15 at 12:52