简体   繁体   中英

CMake COMMAND shell parameter substitution failing

I try to create a cmake custom target to merge several .a files into a single one. Note that I cannot use the OBJECTS lib mode because I have a lot of 3rd party library (I'm in a complex environment, conan, cmake, etc...).

So I wrote the following

add_custom_target(combineall
    COMMAND echo "extract all .o files from all lib*.a file in the static folder"
    COMMAND for f in *.a ; do ar -x $f ; done
    COMMAND echo "merge all .o files in the static folder"
    COMMAND for f in *.o ; do ar -rs ${CMAKE_BINARY_DIR}/libmerged.a $f ; done
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/static
    DEPENDS MyLib
    )

but when cmake run the combineall custom target I get the following error message:

extract all .o files from all lib*.a file in the static folder /bin/sh: -c: line 1: syntax error: unexpected end of file

I suppose that it come from the $f . I know that I could wrote a completely different cmake script iterating others the .a files using the cmake for_each syntax but It's not my goal here !

Regards, Alex

You have to backslash-escape your semicolons, and $-escape your $. CMake is removing them from the command and so bash sees for f in *.a do ar -x done , which fails with the error you're seeing.

So your target should be:

add_custom_target(combineall
    COMMAND echo "extract all .o files from all lib*.a file in the static folder"
    COMMAND for f in *.a \; do ar -x $$f \; done
    COMMAND echo "merge all .o files in the static folder"
    COMMAND for f in *.o \; do ar -rs ${CMAKE_BINARY_DIR}/libmerged.a $$f \; done
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/static
    DEPENDS MyLib
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM