1

cmake 3.5.
I have tried the following code in my CMake project. Let's say SUBDIRS contains a list of directories:

1.

foreach(subdir ${SUBDIRS})
    if(IS_DIRECTORY ${subdir})
        MESSAGE( STATUS "Including dir " ${subdir} )
        include_directories(${subdir})
    endif()
endforeach()

2.

set(include_dirs "")
foreach(subdir ${SUBDIRS})
    if(IS_DIRECTORY ${subdir})
        list(APPEND include_dirs ${subdir})
    endif()
endforeach()
include_directories(${include_dirs})

3.

set(include_dirs "")
foreach(subdir ${SUBDIRS})
    if(IS_DIRECTORY ${subdir})
        set(include_dirs "${include_dirs} ${subdir}")
    endif()
endforeach()
include_directories(${include_dirs})

None of the source code above works.
Looks like I must declare specifically each path:

include_directories(path/to/dir1
                    path/to/dir2
                    ...)

I have noticed that

include_directories("path/to/dir1" "path/to/dir2")

works as well, in contrast to

include_directories("path/to/dir1 path/to/dir2")

which doesn't work. I think it reflects the foreach() problem.

Any suggestions? Thanks.

----------- edit -----------
The message from 1.: MESSAGE( STATUS "Including dir " ${subdir} ) provides the output:

including dir path/to/dir1
including dir path/to/dir2
hudac
  • 2,584
  • 6
  • 34
  • 57
  • What do you mean by "does not work"? What do you expect, what happens. It's obvious that `include_directories("path/to/dir1 path/to/dir2")` is wrong, as you pass only one argument and it is not a valid path. – usr1234567 May 03 '16 at 06:34
  • I except all directories to be included after the `include_directories()` directive. – hudac May 03 '16 at 06:43
  • Give the actual paths you want to add. Do they contain spaces? What's the output you get? – usr1234567 May 03 '16 at 06:56
  • How do you test if the `include_directories` command effectively works? Also, somebody mentioned you should give full paths, e.g. `"${CMAKE_SOURCE_DIR}/path/to/dir1"`. Last thing to try, in your first sample, is to add quotes, like this `include_directories("${subdir}")` – Antonio May 03 '16 at 12:15

1 Answers1

2

Your first code snippet looks good to me, I checked some CMake code in a project that I wrote a while ago and it looks almost identical.

foreach(subdir ${SUBDIRS})
    if(IS_DIRECTORY ${subdir})
        MESSAGE( STATUS "Including dir " ${subdir} )
        include_directories(${subdir})
    endif()
endforeach()

My best guess would be that you're setting the SUBDIRS variable incorrectly.

Try doing something like:

set(SUBDIRS
    path/to/dir1
    path/to/dir2
)

And see if the loop works then. If so, it's probably an issue with the variable's value. Be sure not to put quotes around the list of values that you set SUBDIRS to, although you can quote the individual entries in the list.

Justin Buchanan
  • 404
  • 3
  • 9
  • You can try adding another `message()` call that's inside the loop, but outside of the `if(IS_DIRECTORY)` part. This will let you know if the loop is working correctly or not. My best guess is that your directory paths in `SUBDIRS` aren't correct. You may want to specify them relative to the current cmake directory. For example, rather than `path/to/dir1`, do `${CMAKE_CURRENT_SOURCE_DIR}/path/to/dir1` – Justin Buchanan May 03 '16 at 07:05
  • @hudac I agree with Justin Buchanan that this should work. You have to add `${CMAKE_CURRENT_SOURCE_DIR}` where you are filling the `SUBDIRS` list. See also [What is the difference between “${CMAKE_CURRENT_SOURCE_DIR}” and “.” in INCLUDE_DIRECTORIES?](http://stackoverflow.com/questions/30705284/what-is-the-difference-between-cmake-current-source-dir-and-in-include) You can check with `get_directory_property(_inc_dirs INCLUDE_DIRECTORIES)` | `message("_inc_dirs: ${_inc_dirs}")` what is actually set. – Florian May 03 '16 at 10:59
  • 1
    If you are not sure if the variables have been set correctly before the for_each try to print the values with the command message(STATUS ${VAR_NAME}). – Francesco Argese May 03 '16 at 19:14