How to generate a header in source with cmake?

雨燕双飞 提交于 2019-12-01 02:43:58

问题


In my project I have a "schema" file and utility that I wrote to generate a header file. I'm using cmake and out of source build to build the application.

Currently I have to regenerate the header file by hand then build the application.

Then I came up with this CMakeLists.txt statements, but they generate the header in the build directory instead of in the source directory.

configure_file( generator.pl generator COPYONLY )
configure_file( schema.txt.in schema.txt COPYONLY )
add_custom_command(
    OUTPUT generated.h
    COMMAND ./generator schema.txt generated.h
    DEPENDS mib_schema.txt.in generator.pl
    COMMENT "Regenerating header file..."
)

Is it possible to generate the header in the source directory?

edit (to reflect the answer):

The file can be reached directly by fully qualifying its path with either

${CMAKE_CURRENT_SOURCE_DIR}

or:

${CMAKE_CURRENT_BINARY_DIR}

So, to generate the header in my source dir, the previous excerpt from CMakeLists.txt becomes:

add_custom_command(
    OUTPUT generated.h
    COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/generator.pl ${CMAKE_CURRENT_SOURCE_DIR}/schema.txt.in ${CMAKE_CURRENT_SOURCE_DIR}/generated.h
    DEPENDS mib_schema.txt.in generator.pl
    COMMENT "Regenerating header file..."
)

which is actually simpler. Thanks

--to


回答1:


I think that generated header are well placed in the binary directory, since you might want to create to build directories with the same source and different configurations resulting in different header generated.

You might want to include the build directory to your project:

include_directories(${CMAKE_CURRENT_BINARY_DIR})


来源:https://stackoverflow.com/questions/4225005/how-to-generate-a-header-in-source-with-cmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!