关于生成文件的CMake [英] CMake with regarding generated files

查看:109
本文介绍了关于生成文件的CMake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我有以下情况:我有一个CMake文件,该文件应该用来编译我的应用程序,该文件包括:

I have the following situation: I have a CMake file, which is supposed to compile my application, which consists of:

  1. 一个或多个cpp文件
  2. 一些模板文件(ecpp),这些文件又被生成为cpp文件,这些文件被编译到应用程序中(它们在WEB_COMPONENTS中列出,因此对于每个组件,都有关联的文件以及将从中生成的.cpp.
  1. one or more cpp files
  2. some template files (ecpp), which on their turn are generated into cpp files, which are compiled into the application (they are listed below in the WEB_COMPONENTS so for each component there is the associated .ecpp file and the .cpp that will be generated from it).

这是CMakeLists.txt(简体)

And here is the CMakeLists.txt (simplified)

cmake_minimum_required (VERSION 2.6)
set (PROJECT sinfonifry)
set (ECPPC /usr/local/bin/ecppc)
set (WEB_COMPONENTS 
     images 
     menu
     css
)

set(${PROJECT}_SOURCES
  ""
  CACHE INTERNAL ${PROJECT}_SOURCES
)

foreach(comp ${WEB_COMPONENTS})
  list(APPEND ${PROJECT}_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${comp}.cpp )

  execute_process(COMMAND ${ECPPC} -o ${CMAKE_CURRENT_BINARY_DIR}/${comp}.cpp -v
                  ${CMAKE_CURRENT_SOURCE_DIR}/${comp}.ecpp 
                  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} OUTPUT_QUIET
                 )
endforeach()

list(APPEND ${PROJECT}_SOURCES main.cpp )
add_executable(${PROJECT}_exe ${${PROJECT}_SOURCES})
target_link_libraries(${PROJECT}_exe cxxtools dl tntnet tntdb)

现在,发生了什么:第一次(例如:生成构建目录,运行cmake-gui,选择Web组件,配置,生成,制作),CMake很好地执行了${ECPPC}命令.它在二进制目录中生成所需的CPP文件,并将它们链接在一起.

Now, what happens: for the very first time (ie: make the build directory, run cmake-gui, select web component, configure, generate, make) the CMake nicely executes the ${ECPPC} command, ie. it generates the required CPP files in the binary directory, and links them together.

过一会儿,很明显,在我工作的同时,我修改了一个组件文件(例如images.ecpp)并在生成目录中再次运行make.但是现在,CMake不会拾取ecpp文件的更改.我必须去cmake-gui,删除缓存,从零开始重新启动所有内容.这很累又很慢.

After a while, obviously while I work, I modify one of the component files (such as images.ecpp) and run make again in the build directory. But now, CMake does not pick up the changes of the ecpp files. I have to go to cmake-gui, delete cache, restart everything from zero. This is very tiresome and slow.

因此,有两个问题:

  1. 我告诉CMake跟踪images.ecpp的更改,并在${ECPPC}编译器发生更改时调用它吗?

  1. Cand I tell CMake to track the changes of the images.ecpp and call the ${ECPPC} compiler on it if it changed?

如何make clean以便它也删除生成的cpp文件.

How can I make clean so that it also removes the generated cpp files.

谢谢你的时间,f.

推荐答案

您要使用add_custom_command()而不是execute_process().看到这里: https://stackoverflow.com/a/2362222/4323

Instead of execute_process() you want to use add_custom_command(). See here: https://stackoverflow.com/a/2362222/4323

基本上,您告诉CMake OUTPUT(生成的文件名),COMMANDDEPENDS(.ecpp文件名).这使它了解如何将源代码转换为必要的C ++生成的文件.然后,将生成的文件添加到某个目标,例如add_executable()add_custom_command()依赖项(如果不需要编译,则更可能需要).

Basically you tell CMake the OUTPUT (the generated filename), COMMAND, and DEPENDS (the .ecpp filename). This makes it understand how to turn the source into the necessary C++ generated file. Then, add the generated file to some target, e.g. add_executable(), or to an add_custom_command() dependency (if it didn't need to be compiled you'd more likely need that).

这篇关于关于生成文件的CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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