[CMake] include_directories() and duplicates

Michael Hertling mhertling at online.de
Wed Nov 2 15:51:10 EDT 2011


On 11/02/2011 06:54 PM, Robert Dailey wrote:
> Does the include_directories() command strip out duplicate paths added?

Yes, it does; see the following exemplary project:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(INCLUDE C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR})
INCLUDE_DIRECTORIES(BEFORE ${CMAKE_SOURCE_DIR})

Although CMAKE_SOURCE_DIR is added twice, it appears only once in the
compilation command. Note that the BEFORE flag additionally changes
the include directories' order. For more information, refer to
cmMakefile::AddIncludeDirectory() in Source/cmMakefile.cxx:

"// Don't add an include directory that is already present."

However, with "cmake --build" and VS2008, I can see the following:

Z:\work\include>type CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(INCLUDE C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR})
INCLUDE_DIRECTORIES(BEFORE ${CMAKE_SOURCE_DIR})

Z:\work\include>mkdir obj

Z:\work\include>cd obj

Z:\work\include\obj>cmake ..
-- Building for: Visual Studio 9 2008
-- Check for working C compiler using: Visual Studio 9 2008
-- Check for working C compiler using: Visual Studio 9 2008 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: Z:/work/include/obj

Z:\work\include\obj>cmake --build .

Microsoft (R) Visual Studio Version 9.0.21022.8.
[...]
1>cl /Od /I "Z:\work\include" /I "Z:\work\include\obj" [...]

REM Everything works as expected, but now
REM comment out CMakeLists.txt's last line:

Z:\work\include\obj>type ..\CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(INCLUDE C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR})
#INCLUDE_DIRECTORIES(BEFORE ${CMAKE_SOURCE_DIR})

Z:\work\include\obj>cmake --build .

Microsoft (R) Visual Studio Version 9.0.21022.8.
[...]
1>Checking Build System
1>CMake is re-running because [...]
[...]
1>-- Configuring done
1>-- Generating done
1>-- Build files have been written to: Z:/work/include/obj
[...]
2>cl /Od /I "Z:\work\include" /I "Z:\work\include\obj" [...]

REM CMAKE_SOURCE_DIR is still before CMAKE_BINARY_DIR
REM although CMake has been re-run in the meantime.

REM However:

Z:\work\include\obj>cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: Z:/work/include/obj

Z:\work\include\obj>cmake --build .

Microsoft (R) Visual Studio Version 9.0.21022.8.
[...]
1>cl /Od /I "Z:\work\include\obj" /I "Z:\work\include" [...]

REM Now, the include directories' order is correct.

It seems as if a reconfiguration triggered by "cmake --build" due to
a changed CMakeLists.txt file has not the same effect as an explicit
reconfiguration and a subsequent rebuild. Can anybody confirm this
observation, and might it be considered as buggy behavior?

Regards,

Michael


More information about the CMake mailing list