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

Mike Jackson imikejackson at gmail.com
Sun Feb 17 10:51:27 EST 2008


Lets assume for a second that your layout is something like:

Project
   src
    -sub1
      --CMakeLists.txt
      --sub1.cpp
      --sub1.ui
    -sub2
      --CMakeLists.txt
      --sub2.cpp

What you would want quite possible is a top level CMakeLists.txt file  
Like this:

PROJECT (MyGreatProject)

SET (LIBRARY_OUTPUT_PATH "${PROJECT_BINARY_DIR}/Bin" CACHE INTERNAL  
"For libraries.")
SET (EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/Bin" CACHE  
INTERNAL "For executables.")

#--- All the generated .h and .cpp files end up in $ 
{CMAKE_CURRENT_BINARY_DIR}
#---  so include that directory.
INCLUDE_DIRECTORIES(  "${CMAKE_CURRENT_BINARY_DIR}" )

ADD_SUBDIRECTORY( src/sub1)
ADD_SUBDIRECTORY( src/sub2)

#--- End CMakeLists.txt file

The above _should_ work although I just did it from memory.

Mike Jackson

On Feb 17, 2008, at 10:36 AM, 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).
>
> Bill Hofmann wrote the following QT4_WRAP_UI macro:
>
>> MACRO (QT4_WRAP_UI 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)
>> 		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)
>
> I'm using just a slightly adapted form of this macro. The problem  
> is that our project includes the generated header files from  
> different subdirectories and I can't figure out on how to include  
> them.
>
> 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?).
>
> I've attached above mentioned mini project.
>
> Thanks for any help.
>
>
>
> <demo.zip>_______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake

-- 
Mike Jackson   Senior Research Engineer
Innovative Management & Technology Services




More information about the CMake mailing list