CMake:连续两次编译程序

时间:2017-07-23 16:27:23

标签: compilation cmake g++

为了能够进行许多自动优化,我希望能够首先使用标志-fprofile-generate编译我的程序,然后运行它以生成配置文件,然后使用{{1}重新编译程序而不是。

这意味着我想连续两次编译我的程序,每次只有两个-fprofile-use

我怎么能用CMake做到这一点?

1 个答案:

答案 0 :(得分:2)

您可以构建一些东西,然后运行它,然后通过使用客户目标和“add_dependencies”命令在执行后构建其他东西。对于您的gcov案例,您可能会执行以下操作:

profile.cxx

#include <iostream>
int main(void) {
    std::cout << "Hello from Generating Profile run" << std::endl;
    return 0;
}

的CMakeLists.txt

cmake_minimum_required(VERSION 3.1 FATAL_ERROR)

project(profileExample C CXX)

# compile initial program
add_executable(profileGenerate profile.cxx)
set_target_properties(profileGenerate PROPERTIES COMPILE_FLAGS "-fprofile-
generate")
target_link_libraries(profileGenerate gcov)

add_executable(profileUse profile.cxx)
set_target_properties(profileUse PROPERTIES COMPILE_FLAGS "-fprofile-use")
target_link_libraries(profileUse gcov)

# custom target to run program
add_custom_target(profileGenerate_run
    COMMAND profileGenerate
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "run Profile Generate"
    SOURCES profile.cxx
    )

#create depency for profileUse on profileGenerate_run
add_dependencies(profileUse profileGenerate_run)

显示构建的输出 - &gt;跑 - &gt;构建

Build Output