[CMake] Generated Source Files
Michael Hertling
mhertling at online.de
Sun May 1 06:43:55 EDT 2011
On 05/01/2011 11:16 AM, Toralf Niebuhr wrote:
> Hi.
>
> I've got the following problem:
>
> I've got a CMake Project with the following directory structure:
>
> project/src
> project/include
> project/resources
>
> In the directory project/resources/ is a file res.gen.
> I have a command that generates res.cpp and res.h files from it.
>
> I want the .cpp files to be compiled and linked to the library generated by CMake with the other source files.
> I want the header files to be available such that my project can include them (so they need to be generated before compilation).
> I also need a target that installs the header files into the proper install directory.
> It should be possible to do this as out of source build, such that the generated files are not copied to the project/src and project/include directory.
> If the res.cpp file changes, the generation command should be invoked again.
>
> Basically before building the project, the script should generate the resource files if needed and than build the project as if the generated files are in project/src, project/include.
>
> What would be the correct approach for this?
>
>
> Thanks ahead
>
> Toralf Niebuhr
For an example, look at the following CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(GENRES CXX)
FILE(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/include ${CMAKE_BINARY_DIR}/src)
ADD_CUSTOM_COMMAND(OUTPUT
${CMAKE_BINARY_DIR}/include/res.h ${CMAKE_BINARY_DIR}/src/res.cpp
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/include/res.h
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/src/res.cpp
DEPENDS ${CMAKE_SOURCE_DIR}/resources/res.gen)
INCLUDE_DIRECTORIES(
${CMAKE_BINARY_DIR}/include
${CMAKE_SOURCE_DIR}/include)
ADD_LIBRARY(f SHARED
src/f.cpp include/f.h
${CMAKE_BINARY_DIR}/src/res.cpp ${CMAKE_BINARY_DIR}/include/res.h)
INSTALL(TARGETS f
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
INSTALL(FILES
include/f.h ${CMAKE_BINARY_DIR}/include/res.h
DESTINATION include)
with
// src/f.cpp:
#include "f.h"
#include "res.h"
void f(){}
and empty include/f.h and resources/res.gen. See [1,2] and - when your
project's complexity is growing - especially [3] for more information.
Regards,
Michael
> [1] http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_generate_a_source_file_during_the_build.3F
> [2] http://www.cmake.org/Wiki/CMake_FAQ#How_do_I_generate_an_executable.2C_then_use_the_executable_to_generate_a_file.3F
> [3] http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_add_a_dependency_to_a_source_file_which_is_generated_in_a_subdirectory.3F
More information about the CMake
mailing list