我试图构建一个依赖于源(自然)的库,以及由两个不同的可执行程序动态生成的源。
首先,我将显示源代码,以便上下文是明确的。App是目录中的代码,而不是预构建的。
add_library(app STATIC app.c)
C依赖于生成的hdr1.h,它是生成的文件。因此,我添加hdr1.h的生成,如下所示:
add_executable(hdr_maker1 src1.c)
Hdr_maker1 exe有自己的源src1.c,但它依赖于第二个文件hdr2.h,而hdr2.h又由另一个exe生成。
add_executable(hdr_maker2 src2.c)
然后,我尝试指定这些文件的依赖项和执行情况,因此hdr_maker2首先运行以生成hdr2.h,而hdr_maker1在构建hdr1.h生成hdr1.h时使用hdr2.h,而hdr1.h又由目标库使用。
# 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)
发布于 2016-09-24 12:02:31
多亏了茨瓦列夫,这是重写的解决方案。
# 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)
https://stackoverflow.com/questions/39670732
复制相似问题