如何在 cmake 中制定构建规则来预处理惰性 C++生成 .h 和 .cpp 文件的 .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.
Is there any way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是如何执行此操作的示例...首先,您需要找到
lzz
程序,为此请使用find_program
命令:这会设置
LZZ_COMMAND
code> 到编译器的路径。然后使用 CMake 自定义命令将 LZZ 文件编译为其 C++ 头文件/实现文件:这会在当前构建目录中生成文件,以防您进行源外构建。您还需要指定输出是生成的文件:
将它们放在一起,您将得到一个 CMakeLists.txt 文件,如下所示:
您可能还希望最终向 lzz 添加包含路径和其他选项。如果您将所有 Lazy C++ 内容放入一个模块文件中并包含 CMakeLists.txt 中的内容,那么它会更干净一些。但这是基本思想。
Here is an example of how to do this... First you need to find the
lzz
program, for that use thefind_program
command: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: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:
Put that all together and you get a CMakeLists.txt file something like this:
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.
我只是想分享我的 CMakeLists.txt,它基于 richq 的脚本构建。 *.cpp 和 *.hpp 文件现在正确依赖于 *.lzz 文件。 *.lzz 文件被添加到项目中(这回答了上面absense 的问题),但使用source_group 命令与生成的文件分开。
对我来说唯一剩下的问题是无法编译 *.lzz 文件的当前文件。
I just wanted to share my CMakeLists.txt, which builds upon richq's script. The *.cpp and *.hpp files now properly depend on the *.lzz files. The *.lzz files are added to the project (which answers absense's question above) but kept separate from the generated files using the source_group command.
The only remaining dealbreaker for me is the inability to compile the current file for *.lzz files.
对于 make:
使用正确的值填写 sourcecode.h、sourcecode.cpp 和lazy-cpp。我不认识他们。
For make:
fill in sourcecode.h, sourcecode.cpp, and lazy-cpp with the correct values. I don't know them.