Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: Why doesn't POST_BUILD in add_custom_command(...) work?

Tags:

makefile

cmake

I have some trouble when I use PRE_BUILD | PRE_LINK | POST_BUILD in command add_custom_command(...). When I use POST_BUILD, I found the command will execute before the target has been built, like this:

[root@VM_33_35_centos build]# make 
Scanning dependencies of target main 
[100%] Building C object CMakeFiles/main.dir/main.c.o 
Linking C executable main 
This is pre build 
This is post build 
[100%] Built target main 

The contents of my CMakeLists.txt are:

cmake_minimum_required(VERSION 2.8) 
add_executable(main main.c) 
add_custom_command(TARGET main 
                     PRE_BUILD 
                    COMMAND echo "This is pre build " 
        ) 
add_custom_command(TARGET main 
                    POST_BUILD 
                   COMMAND echo "This is post build" 
        ) 

Why did the command echo "This is post build" on line 8 of CMakeLists.txt not execute after the [100%] Built target main message on line 7(Linux command)?

like image 622
Chao Zhang Avatar asked Dec 16 '25 17:12

Chao Zhang


1 Answers

Command added with add_custom_command(TARGET) signature becomes part of the target, that is target can be assumed to be built (Built target main) only after given command is executed.

Description of POST_BUILD keyword

run after the target has been built

means that command is executed after target file (in your case executable main) is created. This file is created as part of linking process, started after line Linking C executable main.

like image 200
Tsyvarev Avatar answered Dec 19 '25 13:12

Tsyvarev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!