提问者:小点点

在bash中运行命令和在makefile中运行命令有什么区别?


我有这个命令来编译我的程序。

c++ -O3 -Wall -shared -pthread -std=c++11 -fPIC $(python3 -m pybind11 --includes) f1.cpp f2.cpp -o exemple$(python3-config --extension-suffix)

我创建了以下makefile。

test:
    c++ -O3 -Wall -shared -pthread -std=c++11 -fPIC $(python3 -m pybind11 --includes) f1.cpp f2.cpp -o exemple$(python3-config --extension-suffix)

如果从terminal运行该命令将成功,但make将失败。

它将失败,并出现错误

pybind11/pybind11.h: No such file or directory
    1 | #include <pybind11/pybind11.h>

该文件应该在命令中使用$(python3-m pybind11--includes)导入。

我认为Makefile中的命令执行起来很像bash脚本。

在bash中运行命令和在Makefile中运行命令有什么区别?


共2个答案

匿名用户

生成文件在/bin/sh中运行命令(默认情况下)。如果您正在使用bash特性,这有时会起到很大的作用;但你不是所以那无关紧要。

这里的问题是$字符对于make是特殊的:它引入了make变量。如果要将$传递给shell,则必须将其加倍:

test:
        c++ -O3 -Wall -shared -pthread -std=c++11 -fPIC $$(python3 -m pybind11 --includes) f1.cpp f2.cpp -o exemple$$(python3-config --extension-suffix)

匿名用户

将命令结果作为当前命令的一部分插入shell中的更可移植的方法是使用``而不是$(python3-m pybind11--includes)。Make不解释此类引用。

test:
    c++ -O3 -Wall -shared -pthread -std=c++11 -fPIC `python3 -m pybind11 --includes` f1.cpp f2.cpp -o exemple`python3-config --extension-suffix`