如何在源中使用cmake生成头? [英] How to generate a header in source with cmake?

查看:180
本文介绍了如何在源中使用cmake生成头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个schema文件和实用程序,我写了生成一个头文件。
我使用cmake和源代码构建来构建应用程序。



目前,我必须手动重新生成头文件,然后构建应用程序。 / p>

然后我想出了这个CMakeLists.txt语句,但是它们在构建目录而不是源目录中生成头。

  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正在重新生成头文件...

是否可以在源目录中生成标题?



编辑(以反映答案)



您可以使用

  $ {CMAKE_CURRENT_SOURCE_DIR} 


$ b b

或:

  $ {CMAKE_CURRENT_BINARY_DIR} 

因此,为了在源代码目录中生成标题,CMakeLists.txt中的前面的摘录变为:

  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正在重新生成头文件...

这实际上更简单。
感谢



- 到

解决方案

生成的头文件放置在二进制目录中,因为你可能想创建一个目录,使用相同的源码和不同的配置生成不同的头文件。



想要将构建目录包含到您的项目中:

  include_directories($ {CMAKE_CURRENT_BINARY_DIR})


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

解决方案

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})

这篇关于如何在源中使用cmake生成头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆