CMake使用说明

 

How can I add a dependency to a source file which is generated in a subdirectory?

Rules created with ADD_CUSTOM_COMMAND as above have scope only in the directory in which they are specified. If the generated file is needed in another directory, a target-level dependency needs to be added. Create a target in the subdirectory with the custom rule in order to drive it:

 # subdir/CMakeLists.txt
 ADD_CUSTOM_COMMAND(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/foo.c
    COMMAND ${CMAKE_COMMAND} copy ${CMAKE_CURRENT_SOURCE_DIR}/bar.c ${CMAKE_CURRENT_BINARY_DIR}/foo.c
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/bar.c
    )
 ADD_CUSTOM_TARGET(generate_foo DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/foo.c)

Now other targets can depend on the target from the subdirectory:

 # CMakeLists.txt
 ADD_SUBDIRECTORY(subdir)
 # Create the executable.
 ADD_EXECUTABLE(generated ${CMAKE_CURRENT_BINARY_DIR}/subdir/foo.c)
 # Tell CMake the source won't be available until build time.
 SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/subdir/foo.c PROPERTIES GENERATED 1)
 # Make sure the source is generated before the executable builds.
 ADD_DEPENDENCIES(generated generate_foo)

How do I make my shared and static libraries have the same root name, but different suffixes?

Set the OUTPUT_NAME of your shared and static libraries to the same thing.

 ADD_LIBRARY(foo SHARED ${foo_sources})
 ADD_LIBRARY(foo-static STATIC ${foo_sources})
 # The library target "foo" already has a default OUTPUT_NAME of "foo", so we don't need to change it.
 # The library target "foo-static" has a default OUTPUT_NAME of "foo-static", so change it.
 SET_TARGET_PROPERTIES(foo-static PROPERTIES OUTPUT_NAME "foo")
 # Now the library target "foo-static" will be named "foo.lib" with MS tools.
 # This conflicts with the "foo.lib" import library corresponding to "foo.dll",
 # so we add a "lib" prefix (which is default on other platforms anyway):
 SET_TARGET_PROPERTIES(foo-static PROPERTIES PREFIX "lib")

How can I generate a source file during the build?

The ADD_CUSTOM_COMMAND command lets you generate a source file that you can then include in another target. For example:

 ADD_CUSTOM_COMMAND(
   OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/foo.c
   COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/bar.c ${CMAKE_CURRENT_BINARY_DIR}/foo.c
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/bar.c
   )
 ADD_EXECUTABLE(foo foo.c)

This will create an executable by copying bar.c to foo.c and then compiling foo.c to produce foo. CMake allows you to put generated source files in the current source or binary directory, so we were careful to output foo.c to the current binary directory. When we add foo.c to foo, CMake will look in either directory for it. Even if foo.c does not yet exist, CMake is smart enough to notice that a custom command creates it. (For the file named as the OUTPUT, CMake has its GENERATED source file property set to true.)

You can also use ADD_CUSTOM_COMMAND when the generator command is another executable in the same project.

Sometimes, the program doing the generation may generate multiple output files that each need to be part of the build. CMake 2.4 or higher supports having multiple files listed in the OUTPUT section. For example, suppose you had a program that read input.txt and generated three files output1.cpp, output2.h, and output3.cpp, and that those three files needed to be compiled into an executable program. The cmake list file for that would look like this:

 PROJECT(FOO)
 # make sure cmake addes the binary directory for the project to the include path
 INCLUDE_DIRECTORIES(${FOO_BINARY_DIR})
 # add the executable that will do the generation
 ADD_EXECUTABLE(my_generator my_generator.cxx)
 GET_TARGET_PROPERTY(MY_GENERATOR_EXE my_generator LOCATION)
 # add the custom command that will generate all three files
 ADD_CUSTOM_COMMAND(
   OUTPUT ${FOO_BINARY_DIR}/output1.cpp ${FOO_BINARY_DIR}/output2.h ${FOO_BINARY_DIR}/output3.cpp
   COMMAND ${MY_GENERATOR_EXE} ${FOO_BINARY_DIR} ${FOO_SOURCE_DIR}/input.txt
   DEPENDS my_generator
   MAIN_DEPENDENCY ${FOO_SOURCE_DIR}/input.txt
   )
 # now create an executable using the generated files
 ADD_EXECUTABLE(generated
                ${FOO_BINARY_DIR}/output1.cpp
                ${FOO_BINARY_DIR}/output2.h
                ${FOO_BINARY_DIR}/output3.cpp)

CMake 2.4 allows you to generate a header file. Because generated headers often cause unnecessary rebuilds, you should try to avoid them; consider using the CONFIGURE_FILE command to prepare the header at CMake time. If you must generate a header file, use code like this:

 ADD_CUSTOM_COMMAND(
   OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/foo.h
   COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/bar.h ${CMAKE_CURRENT_BINARY_DIR}/foo.h
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/bar.h
   )
 ADD_EXECUTABLE(foo foo.c ${CMAKE_CURRENT_BINARY_DIR}/foo.h)

This is like the first example above, except that it generates a header instead of a C file. The header might not exist when the build system scans foo.c's dependencies, so there is no way for CMake to know that this target requires foo.h unless we can tell it that foo.h may exist in the future. We give CMake this knowledge by listing the generated header file in the set of source files for the target. (This requires CMake 2.4. Previous versions of CMake required use of the OBJECT_DEPENDS source file property.)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值