Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'd like to run a custom command with cmake. That sounds like an incredibly simple task/question, but it's frustrating how difficult it is to find an example.

Here's what I'm attempting to do:

$ cmake .
$ make
> Hello World! (Output)

In Gnu Make that's very easy:

bar: 
        echo Hello World!

But I'm trying to do this in cmake. Based on what I've been reading, I should be able to do that with the CMakeLists.txt file below:

cmake_minimum_required(VERSION 3.6)
project(foo)
add_custom_target(bar)
add_custom_command(
  TARGET   bar
  COMMAND  "echo Hello World!"
)

Currently there is no work to do if I just call make. I need to explicitly call make bar. How can I add bar to the all recipe?

I've tried adding add_dependency(foo bar), but foo is a non-existent target. If there is some super-target that I'm unaware of that would be perfect. Then I could just use that as the TARGET for my custom command and not bother with bar.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
471 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use ALL option for build the target by default:

add_custom_target(bar ALL)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...