6

I want to use Linux if condition in CMakeLists.txt by add_custom_command(...) for i need run these if condition and do some judgement in makefile. Like this:

cmake_minimum_required(VERSION 2.8)
add_custom_target(temp_target ALL)
add_custom_command(TARGET temp_target
                   PRE_BUILD
                   COMMAND if ["a" != "b"]; then echo 1; fi;
                   VERBATIM )

What should i do if i want to use

if ["a" != "b"]; then echo 1; fi;

when make a makefile? Thanks a lot for you help!

1
  • if it doesnt work like that, why dont you try to add these in a function and then call the function in the command? Commented Apr 19, 2016 at 7:33

2 Answers 2

5

You may specify one-line shell code with using /bin/sh -c as COMMAND argument:

COMMAND /bin/sh -c "if [ 'a' != 'b' ]; then echo 1; fi;"

Note, that [ is an extension of the bash, it may be unknown for simple shells like "dash".

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for you reply, but what you give seem does not work in my computer.
DONOT forget to add VERBATIM within add_custom_command !!
as @Master Yoda says, preceed semicolons with backslash
@jackytse: Inside a string, escaping of semicolons is no longer needed.
1

You may need to preceed semicolons with backslash

COMMAND if [ 'a' != 'b' ] \; then echo 1\; fi\;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.