CMake:在CMake脚本执行期间生成源 [英] CMake: generate sources during CMake script execution

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

问题描述

我试图将我的Visual Studio解决方案迁移到CMake。

I'm trying to migrate my Visual Studio solution to CMake.

我有两个项目 - generator(generator.exe从文本文件生成C ++源代码) (包括一些需要由generator.exe处理的文本文件)。

我想要的是:

I have two projects - generator (generator.exe generates C++ sources from text files) and myProj (includes some text files that have to be processed by generator.exe).
What I want is:


  1. 构建生成器项目

  2. 从project1使用generator.exe从项目myProj中的文本文件生成C ++源文件

  3. 将生成的源文件放入生成的VS项目for myProj

那么,如何做呢?

EDIT
我到目前为止所尝试的:

EDIT what I've tried so far:

###################################################################
# in root/generator/CMakeLists.txt:
set(SRC_LIST .... )
set(HDR_LIST .... )
add_executable(generator ${SRC_LIST} ${HDR_LIST})    

###################################################################
# in root/my_proj/CMakeLists.txt:

add_subdirectory(../generator/  ../generator/cmake_out)
add_subdirectory(src)

###################################################################
# in root/my_proj/src/CMakeLists.txt:    

ADD_CUSTOM_COMMAND(
   OUTPUT ../include/SomeSource.h SomeSource.cpp
   COMMAND generator ../definitions/SomeDefs1.txt
   DEPENDS generator ../definitions/SomeDefs1.txt
   )

set(SRC_LIST SomeSource.cpp .... )
set(HDR_LIST SomeSource.h .... )

add_library(myProj STATIC ${SRC_LIST} ${HDR_LIST} )

###################################################################
###################################################################

CMake的输出:

.....
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Visual Studio 12 2013
-- Check for working CXX compiler using: Visual Studio 12 2013 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
CMake Error at src/CMakeLists.txt:145 (add_library):
  Cannot find source file:

    ../include/SomeSource.h

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx


推荐答案

从CMake的 add_custom_command( ) ,并从中取出:

Start with CMake's add_custom_command() and take it from there:

add_executable( generator ${generator_SOURCES} )
add_custom_command( OUTPUT source1.cpp source2.cpp
    COMMAND generator args1 args2
    COMMENT "Generating sources..." )

是否将myProj集成到您的其他项目中,或将其保存在外部,取决于您的个人需求,并影响最终设置的外观。

Whether or not to integrate myProj into your other project, or keep it external is up to your individual needs, and influences how the final setup would look like.

回复您的编辑

,在源列表中包含标头文件是一种非常的好主意。 CMake与编译器合作,应该找出源文件对它们包含的头文件的依赖性,并且它们比大多数人做的更好。 C ++的 #include 告诉编译器处理头!

First off, it is an extremely bad idea to include header files in the list of sources. CMake, in cooperation with the compiler, is supposed to figure out the dependencies of source files on the header files they include, and they do a much better job at it than most humans. C++'s #include already tells the compiler to process the header!

你显示的CMake代码不是你正在执行的代码。 add_library()列出 SomeSource.h ,但CMake抱怨 ../ include / SomeSource.h 。目录名也不工作。

Second, the CMake code you are showing is not the one you are executing. Your add_library() lists SomeSource.h, but CMake complains about ../include/SomeSource.h. The directory names also do not work out. This makes it hard to say what exactly is wrong.

当熟悉一个新工具时,我的一般建议:立即尝试它在您最终希望它在(myProj,在这种情况下)工作的上下文中跳舞。相反,设置一个简单的测试环境(使用目录X和Y,源文件foo.cpp和bar.cpp,你的想法),并测试的概念(像,有 echo 创建一个文件而不是 generator ,所以你不需要创建 generator

My generic suggestion when familiarizing yourself with a new tool: Do not immediately try to make it dance in the context you eventually want it to work in (myProj, in this case). Instead, set up a simple test environment (with directories X and Y, source files foo.cpp and bar.cpp, you get the idea), and test the concepts (like, having echo create a file instead of generator, so you do not need to build generator first -- that's the next step).

这样,如果弹出任何问题,您可以要求帮助 ,您可以提供完整示例代码,供其他人找出,而无需摆动您的项目设置。

That way, if any problems pop up, you can ask for help in the abstract, and you can provide complete example code for others to figure out without having to wiggle through your project's setup.

此外,你避免了由于错误的先入为主的问题。很多时候,你的第一次尝试将导致一个尴尬的设置,真正的优雅只产生于经验教训。

Also, you avoid problems that arise from faulty preconceptions. Quite often, your first attempt will result in an awkward setup, and real elegance only arises from the lessons learned. Invest the time to learn the tool properly.

如果您还不知道该工具, source_group() 允许CMake创建一个MSVC项目文件,其中给定的源文件分组在一起,因此您可以让您的项目在CMake控制下,但仍然舒适地在MSVC内工作。

If you didn't know about it already, source_group() allows CMake to create a MSVC project file with the given source files grouped together, so you can have your project under CMake control but still work comfortably inside MSVC.

我已经设置了一个框架CMake设置我在各种项目中工作,也许你对它感兴趣(为灵感或实际使用): JAWS

I've set up a "framework" CMake setup I am working from in various projects, perhaps you're interested in it (for inspiration or actual use): JAWS.

这篇关于CMake:在CMake脚本执行期间生成源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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