简体   繁体   English

CMake COMMAND shell参数替换失败

[英]CMake COMMAND shell parameter substitution failing

I try to create a cmake custom target to merge several .a files into a single one. 我尝试创建一个cmake自定义目标,将几个.a文件合并为一个。 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...). 请注意,我不能使用OBJECTS lib模式,因为我有很多第三方库(我在复杂的环境中,conan,cmake等...)。

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: 但是当cmake运行combineall自定义目标时,我收到以下错误消息:

extract all .o files from all lib*.a file in the static folder /bin/sh: -c: line 1: syntax error: unexpected end of file 从静态文件夹/ bin / sh中的所有lib * .a文件中提取所有.o文件:-c:第1行:语法错误:意外的文件结尾

I suppose that it come from the $f . 我想它来自$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 ! 我知道我可以编写一个完全不同的cmake脚本,使用cmake for_each语法迭代其他.a文件,但这不是我的目标!

Regards, Alex 问候,亚历克斯

You have to backslash-escape your semicolons, and $-escape your $. 你必须反斜杠 - 逃避你的分号,并且$ -escape你的$。 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. CMake正在从命令中删除它们,因此bash for f in *.a do ar -x done看到for f in *.a do ar -x done ,它因你看到的错误而失败。

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
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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