[CMake] ADD_CUSTOM_COMMAND and how to include generated files from other directories

Alexander Neundorf a.neundorf-work at gmx.net
Sun Feb 17 10:58:29 EST 2008


On Sunday 17 February 2008, blinkeye wrote:
> Hi guys
>
> I'm in the process of replacing Makefiles from two large projects with
> CMake files. So far it's working great, but now I hit a wall:
>
> I need to generate .cpp and .h files from input files (analogue to the
> .ui files of Qt).
...
> Imagine the following project layout:
>
> src
> -sub1
> --CMakeLists.txt
> --sub1.cpp
> --sub1.ui
> -sub2
> --CMakeLists.txt
> --sub2.cpp
>
> ${QT_UIC_EXECUTABLE} will read sub1.ui and generate a file like
> "ui_sub1.h". sub1.cpp and sub2.cpp both include the file "ui_sub1.h".
>
> While above macro works for sub1 it doesn't work for sub2:
> > make
> > [ 20%] Generating ui_sub1.h
> > [ 40%] Generating moc_sub1.cxx
> > Scanning dependencies of target sub1
> > [ 60%] Building CXX object src/sub1/CMakeFiles/sub1.dir/sub1.o
> > [ 80%] Building CXX object src/sub1/CMakeFiles/sub1.dir/moc_sub1.o
> > Linking CXX executable sub1
> > [ 80%] Built target sub1
> > Scanning dependencies of target sub2
> > [100%] Building CXX object src/sub2/CMakeFiles/sub2.dir/sub2.o
> > /home/cerberos/Desktop/mutacts/dan/src/sub2/sub2.cpp:4:21: error:
> > ui_sub1.h: No such file or directory make[2]: ***
> > [src/sub2/CMakeFiles/sub2.dir/sub2.o] Error 1
> > make[1]: *** [src/sub2/CMakeFiles/sub2.dir/all] Error 2
> > make: *** [all] Error 2
>
> I tried for hours but I can't figure out a proper (or at least working)
> way on how to achieve that. Changing above macro to include the path of
> the generated header files doesn't seem to make a difference:
>
> MACRO (QT4_WRAP_UI_CUSTOM outfiles )
>     FOREACH (it ${ARGN})
>        GET_FILENAME_COMPONENT(outfile ${it} NAME_WE)
>        GET_FILENAME_COMPONENT(infile ${it} ABSOLUTE)
>        SET(outfile ${CMAKE_CURRENT_BINARY_DIR}/ui_${outfile}.h)
>        include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
>        ADD_CUSTOM_COMMAND(OUTPUT ${outfile}
>        COMMAND ${QT_UIC_EXECUTABLE}
>        ARGS -o ${outfile} ${infile}
>        MAIN_DEPENDENCY ${infile})
>        SET(${outfiles} ${${outfiles}} ${outfile})
>     ENDFOREACH (it)
> ENDMACRO (QT4_WRAP_UI_CUSTOM)
>
> For some reason the included directory is lost in sub2 again (btw: is
> there a way to log the currently included directories besides analysing
> the make output?).

In that respect include directories work the same way as variables, they are 
per directory, i.e. a sub directory inherits all the include dirs of its 
parent directory, but the include directories of a subdir don't influence the 
parent dir.

Did you try to put something like the following into sub2/CMakeLists.txt ?
include_directories(${CMAKE_BINARY_DIR}/sub1) 

Alex


More information about the CMake mailing list