CMake编译时的源代码

CMake编译时的源代码

本文介绍了CMake编译时的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况与CMake:


  1. 它必须构建两个应用程序:



    a。 生成器



    b。 something_else


  2. 生成器 / li>

  3. something_else 是:



    a。几个正常CPP文件



    b。这些CPP文件是由生成器


生成器在配置阶段配置有一些选项,根据这些选项,生成的文件的内容不同。



这里我得到一个错误:如果我在 something_else add_application 中指定所有文件因为它不能找到生成的文件...显然因为它们还没有生成,因为生成器没有被构建和执行。



所以问题:这是可能使用CMake吗?如果是,如何?

解决方案

是的,这是可能的。您可以通过提供自定义命令来生成文件,以便CMake学习如何生成它们。这里有一个例子:

  add_executable(generator gen1.cpp gen2.cpp)

add_custom_command $ b OUTPUT generated_file1.cpp generated_file2.cpp
COMMAND generator -options --go here
COMMENT正在运行的生成器
VERBATIM


add_executable something_else
fixed1.cpp
fixed2.cpp
generated_file1.cpp
generated_file2.cpp

这样,CMake就会知道文件是否生成,并且会引入适当的依赖关系 - 在a中键入 make something_else clean build dir将构建 generator ,然后运行它,然后构建 something_else


I have the following situation with CMake:

  1. it has to build two applications:

    a. generator

    b. something_else

  2. The generator is nothing fancy, a few CPP files linked together

  3. The something_else is:

    a. a few "normal" CPP files

    b. generated CPP/h files which I have to link in. These CPP files are generated by the generator

The generator is configured in the configure phase with some choices, depending on these options the content of the generated files is different.

And here I get an error: If I specify all the files (generated and not) in the add_application of something_else the configure phase chokes on it since it cannot find the generated files... obviously because they were not generated yet, since the generator was not built and executed yet.

So the question: Is this possible using CMake? If yes, how?

解决方案

Yes, it is possible. You do that by providing a custom command to generate the files, so that CMake learns how to generate them. Here's an example:

add_executable(generator gen1.cpp gen2.cpp)

add_custom_command(
  OUTPUT generated_file1.cpp generated_file2.cpp
  COMMAND generator -options --go here
  COMMENT "Running generator"
  VERBATIM
)

add_executable(something_else
  fixed1.cpp
  fixed2.cpp
  generated_file1.cpp
  generated_file2.cpp
)

This way, CMake will know the files are generated, and will introduce proper dependencies as well - typing make something_else in a clean build dir will build generator, then run it, then build something_else.

这篇关于CMake编译时的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:05