我有两个生成器,我希望链接依赖

时间:2016-09-23 23:03:34

标签: cmake

我正在尝试构建一个依赖于源(自然)以及由两个不同可执行文件动态生成的源的库。

首先,我将显示源,以便上下文清晰。应用程序是目录中的代码,而不是预先构建的。

add_library(app STATIC app.c)

App.c取决于生成的hdr1.h,它是生成的文件。所以我像这样添加hdr1.h的生成:

add_executable(hdr_maker1 src1.c)

Hdr_maker1 exe有自己的源src1.c,但它依赖于第二个文件hdr2.h而后者又是 另一个exe。

add_executable(hdr_maker2 src2.c)

然后我尝试指定这些文件的依赖关系和执行,因此hdr_maker2首先运行以生成hdr2.h,hdr_maker又被hdr_maker1用于生成hdr1.h,而hdr1.h又由目标库使用。 / p>

# pseudo target to make hdr2.h by executing hdr_maker2
add_custom_target(HDR_MAKER2 DEPENDS hdr2.h)
# for the command portion, hdr2.h is specifed a second time
# so that hdr_maker2 makes its output file as hdr2.h
add_custom_command(OUTPUT hdr2.h COMMAND $<TARGET_FILE:hdr_maker2> hdr2.h)

# this one depends upon its own src and the generated hdr2.h src
add_custom_target(HDR_MAKER1 DEPENDS hdr1.h hdr2.h)
add_custom_command(OUTPUT hdr1.h COMMAND $<TARGET_FILE:hdr_maker1> hdr1.h)

#
add_dependencies(app HDR_MAKER1)

结果是它生成了hdr_maker2,但它在运行hdr_maker2之前尝试生成hdr_maker1,这为hdr_maker2生成了必需的头。如果我只想制作一个伪目标,这个模式就有效。即。如果应用程序只依赖于一个.h那么它将在构建应用程序之前构建并运行制造商。

FWIW,我还试图通过这样做链接依赖关系:

# hdr_maker1 makes hdr1.h, but depends upon hdr2.h 
# which uses the existing dependncy.
add_custom_target(HDR_MAKER1 DEPENDS hdr1.h)
add_custom_command(OUTPUT hdr1.h COMMAND $<TARGET_FILE:hdr_maker1> hdr1.h DEPENDS hdr2.h)

我还试图将依赖关系链接起来:

#
add_dependencies(app HDR_MAKER1)
add_dependencies(HDR_MAKER1 HDR_MAKER2)

1 个答案:

答案 0 :(得分:0)

感谢Tsvarev,这是重写的解决方案。

# specifying hdr1.h as one of the sources will chain hdr1.h to
# be made by its generator.
add_library(app STATIC app.c hdr1.h)

# specifying hdr2.h as one of the source will chain hdr2.h to
# be made by its generator.
add_executable(hdr_maker1 src1.c hdr2.h)

# hdr2.h can be made without dependencies
add_executable(hdr_maker2 src2.c)

# These are used a custom targets since in real project,
# the making of the makers will be conditional.  In the
# first build the makes are built.  The second time
# the .exes will be found and used to build the source. 
# ie. the .exes are not built.
add_custom_target(HDR_MAKER2 DEPENDS hdr2.h)
add_custom_command(OUTPUT hdr2.h COMMAND $<TARGET_FILE:hdr_maker2> hdr2.h)

add_custom_target(HDR_MAKER1 DEPENDS hdr1.h)
add_custom_command(OUTPUT hdr1.h COMMAND $<TARGET_FILE:hdr_maker1> hdr1.h)