如何在cmake中构建规则以预处理生成.h和.cpp文件的惰性C ++ .lzz文件? [英] How do I make build rules in cmake to preprocess lazy C++ .lzz files that generate .h and .cpp files?

查看:1121
本文介绍了如何在cmake中构建规则以预处理生成.h和.cpp文件的惰性C ++ .lzz文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是编写只是Lazy C ++ .lzz文件,然后在构建之前运行lzz运行生成.cpp和.h文件,将被内置到最终的应用程序,类似于moc的工作原理Qt。

What I'd like to do is write just Lazy C++ .lzz files and then have lzz run before a build to generate .cpp and .h files that will be built into the final application, sort of like how moc works with Qt.

有什么办法吗?

推荐答案

一个例子如何做...首先你需要找到 lzz 程序,因为使用 find_program 命令:

Here is an example of how to do this... First you need to find the lzz program, for that use the find_program command:

find_program(LZZ_COMMAND lzz)

这会将 LZZ_COMMAND 设置为编译器的路径。然后使用CMake自定义命令将LZZ文件编译到其C ++头文件/实现文件:

This sets LZZ_COMMAND to the path of the compiler. Then use a CMake custom command to compile the LZZ file to their C++ header/implementation files:

add_custom_command(
    OUTPUT ${output}
    COMMAND ${LZZ_COMMAND} -o ${CMAKE_CURRENT_BINARY_DIR} ${filename})

生成当前构建目录中的文件,以防您进行源代码构建。您还需要指定输出是生成的文件:

That generates the files in the current build directory, in case you do out-of-source builds. You will also need to specify that the outputs are generated files:

set_source_files_properties(${output} PROPERTIES GENERATED TRUE)

把所有的组合在一起,你会得到一个类似这样的CMakeLists.txt文件:

Put that all together and you get a CMakeLists.txt file something like this:

cmake_minimum_required(VERSION 2.8)
project(lazy_test)
find_program(LZZ_COMMAND lzz)
function(lazy_compile filename)
    get_filename_component(base ${filename} NAME_WE)
    set(base_abs ${CMAKE_CURRENT_BINARY_DIR}/${base})
    set(output ${base_abs}.cpp ${base_abs}.h)
    add_custom_command(
        OUTPUT ${output}
        COMMAND ${LZZ_COMMAND} -o ${CMAKE_CURRENT_BINARY_DIR} ${filename})
    set_source_files_properties(${output} PROPERTIES GENERATED TRUE)
endfunction()
lazy_compile(${CMAKE_CURRENT_SOURCE_DIR}/example.lzz)
add_executable(test example.cpp example.h)

您可能还想要向lzz添加包含路径和其他选项。如果你把所有的Lazy C ++东西放入一个模块文件,并包括从CMakeLists.txt它会有点清洁。但这是基本的想法。

You would probably also want to add include path and other options to lzz eventually. If you placed all the Lazy C++ stuff into a module file and included that from the CMakeLists.txt it would be a bit cleaner. But this is the basic idea.

这篇关于如何在cmake中构建规则以预处理生成.h和.cpp文件的惰性C ++ .lzz文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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