CMake 自定义构建步骤 PRE_LINK/PRE_BUILD 不起作用

标签 cmake

PRE_LINK 和 PRE_BUILD 似乎在 Linux 上的 CMake 中不起作用(生成 MakeFiles )。文档说仅不支持 PRE_BUILD。但是,PRE_LINK 根本不起作用。

我不确定是否理解错误或者我是否遗漏了某些内容。

( 目前,作为解决方法,我添加了一个假目标并对其进行 POST_BUILD 步骤。然后创建依赖项起作用了。但是,它有自己的问题。对于我的设置,它在并行 make (-j) 完成时会产生问题。

示例代码:

cmake_minimum_required(VERSION 2.8.5)
project(custom_command_test)

add_custom_target(my_actual_target
    COMMAND echo " I am the actual taget "
    COMMENT "Running actual target"
    )
add_custom_command(
    TARGET my_actual_target
    PRE_LINK
    COMMAND echo "I am prelinked to actual target"
    COMMENT " Running PRELINK action "
    )
add_custom_command(
    TARGET my_actual_target
    PRE_BUILD
    COMMAND echo " I am prebuilt to actual target"
    COMMENT " Running PRE_BUILD action"
    )
add_custom_command(
    TARGET my_actual_target
    POST_BUILD
    COMMAND echo " I postbuild to actual target"
    COMMENT " Running POST BUILD action "
    )

输出:

> cmake .
-- The C compiler identification is GNU 4.4.2
-- The CXX compiler identification is GNU 4.4.2
-- Check for working C compiler: XXXX/gcc/4.4.2/bin/gcc
-- Check for working C compiler: XXXX/gcc/4.4.2/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: XXXX/gcc/4.4.2/bin/g++
-- Check for working CXX compiler: XXXX/gcc/4.4.2/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: XXXX

 > make my_actual_target
Scanning dependencies of target my_actual_target
[100%] Running actual target
 I am the actual taget 
 Running PRE_BUILD action
 I am prebuilt to actual target
 Running POST BUILD action 
 I postbuild to actual target
[100%] Built target my_actual_target

> cmake --version
cmake version 2.8.10.2

有什么解决办法吗?

最佳答案

看起来 PRE_LINK 仅适用于“真实”目标,例如库或可执行文件:

add_library(my_actual_target foo.cpp)

add_custom_command(
    TARGET my_actual_target
    PRE_LINK
    COMMAND echo "I am prelinked to actual target"
    COMMENT " Running PRE_LINK action "
    )
add_custom_command(
    TARGET my_actual_target
    PRE_BUILD
    COMMAND echo " I am prebuilt to actual target"
    COMMENT " Running PRE_BUILD action"
    )
add_custom_command(
    TARGET my_actual_target
    POST_BUILD
    COMMAND echo " I postbuild to actual target"
    COMMENT " Running POST_BUILD action "
    )

结果:

Running PRE_BUILD action
...
Running PRE_LINK action
...
Running POST_BUILD action

恕我直言,这是文档问题

关于CMake 自定义构建步骤 PRE_LINK/PRE_BUILD 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25029895/

相关文章:

c++ - 在 CMake 中包含 Visual Studio 解决方案

c++ - CMakeExternalProject_Add : How to build multiple msbuild targets?

c++ - CMake:外部项目的依赖

c++ - tr1/normal_distribution : No such file or directory

c++ - 我可以从一组较小的文件构建 CMakeLists.txt 吗(以提高 CMakeLists.txt 的可读性和可维护性)

list - CMake - 我应该如何删除以空格分隔的列表中的重复项?

gcc - 无法接受正确的GNU编译器

android - 导入的库不会在 Android Studio 2.2 中构建

c++ - 构建一个可移植的可执行文件及其依赖项

C++ 项目 : Artifact-Name dependant "include" directory population using CMake (build-time)