简体   繁体   English

在 Makefile 中调用 GNU ar 脚本

[英]Calling a GNU ar script in Makefile

I'm having some trouble using GNU ar script from a Makefile.我在使用来自 Makefile 的 GNU ar脚本时遇到了一些麻烦。 Specifically, I'm trying to follow an answer to How can I combine several C/C++ libraries into one?具体来说,我正在尝试遵循如何将多个 C/C++ 库合并为一个的答案? , but ar scripting isn't well supported by make , as most Makefiles use the ar command-line interface and not a script. ,但是make不能很好地支持 ar 脚本,因为大多数 Makefile 使用ar命令行界面而不是脚本。 ar needs each line to be terminated by a new line ( \n ). ar需要每行以新行( \n )终止。 This is not the same as multiple shell commands which can use a semicolon as a separator.这与可以使用分号作为分隔符的多个 shell 命令不同。

The ar commands are: ar 命令是:

ar -M <<CREATE libab.a
    ADDLIB liba.a
    ADDLIB libb.a
    SAVE
    END
ranlib libab.a

I'm trying to merge the libraries because I'm creating a static library in a Makefile which depends on another static library, but the second one is created with CMake. I'm trying to merge the libraries because I'm creating a static library in a Makefile which depends on another static library, but the second one is created with CMake.

foo.a: foo.o
    $(AR) $(ARFLAGS) $@ $^

final_lib.c: foo.a the_cmake_library.a
    $(AR) -M <<CREATE $@
        ADDLIB foo.a
        ADDLIB the_cmake_library.a
        SAVE
        END
    ranlib $@

The above doesn't work because make is interpreting the ar commands as its own, so I'm getting a以上不起作用,因为makear命令解释为它自己的命令,所以我得到了

make: CREATE: Command not found

If these were bash commands, I could use something like this answer , but that doesn't work:如果这些是 bash 命令,我可以使用类似这个答案的东西,但这不起作用:

ar -M <<CREATE libab.a; ADDLIB liba.a; ADDLIB libb.a; SAVE; END

ar doesn't have a command-line version of the ADDLIB command. ar没有ADDLIB命令的命令行版本。

My current solution is:我目前的解决方案是:

final_lib.c: foo.a the_cmake_library.a
    $(shell printf "EOM\nCREATE $@\nADDLIB foo.a\nADDLIB the_cmake_library.a\nSAVE\nEND\nEOM\n" > ARCMDS.txt)
    $(AR) -M < ARCMDS.txt
    ranlib $@

I find that very clumsy.我觉得这很笨拙。 Does anyone know of a better way to handle ar scripts in a Makefile?有谁知道在 Makefile 中处理ar脚本的更好方法? Thanks!谢谢!

You don't need to use the shell function and you don't need to write it to a file.您不需要使用shell function 也不需要将其写入文件。 ar is taking from stdin anyway so why not just use a pipe? ar无论如何都是从标准输入中获取的,那么为什么不只使用 pipe 呢?

Either:任何一个:

final_lib.c: foo.a the_cmake_library.a
        printf "EOM\nCREATE $@\nADDLIB foo.a\nADDLIB the_cmake_library.a\nSAVE\nEND\nEOM\n" | $(AR) -M
        ranlib $@

Or something like:或类似的东西:

final_lib.c: foo.a the_cmake_library.a
        (echo EOM; \
         echo "CREATE $@"; \
         echo ADDLIB foo.a; \
         echo ADDLIB the_cmake_library.a; \
         echo SAVE; \
         echo END; \
         echo EOM) \
            | $(AR) -M
        ranlib $@

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

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