Compilation CMake:连续编译一个程序两次

Compilation CMake:连续编译一个程序两次,compilation,cmake,g++,Compilation,Cmake,G++,为了能够进行许多自动优化,我希望能够首先使用标志-fprofile generate编译我的程序,然后运行它生成概要文件,然后使用-fprofile use重新编译程序 这意味着我要连续编译两次程序,每次使用两个不同的CMAKE\u CXX\u标志 如何使用CMake实现这一点?您可以通过使用客户目标和“添加依赖项”命令来构建一些内容,然后运行它,然后在执行后构建其他内容。对于您的gcov案例,您可以执行以下操作: profile.cxx #include <iostream> in

为了能够进行许多自动优化,我希望能够首先使用标志
-fprofile generate
编译我的程序,然后运行它生成概要文件,然后使用
-fprofile use
重新编译程序

这意味着我要连续编译两次程序,每次使用两个不同的
CMAKE\u CXX\u标志


如何使用CMake实现这一点?

您可以通过使用客户目标和“添加依赖项”命令来构建一些内容,然后运行它,然后在执行后构建其他内容。对于您的gcov案例,您可以执行以下操作:

profile.cxx

#include <iostream>
int main(void) {
    std::cout << "Hello from Generating Profile run" << std::endl;
    return 0;
}
显示生成->运行->生成的输出


谢谢你的回答。我没有想到要这么做:哦
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)