lblenner
lblenner

Reputation: 452

What are the differences between running a command in bash and running a command in a Makefile?

I have this command that compiles my program.

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

I created the following 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)

The command will succeed if ran from terminal but make will fail.

It fails with the error

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

This file is supposed to be imported with $(python3 -m pybind11 --includes) in the command.

I thought that commands in Makefile were executed much like a bash script.

What are the differences between running a command in bash and running a command in a Makefile ?

Upvotes: 0

Views: 110

Answers (3)

asio_guy
asio_guy

Reputation: 3767

Another variant $(shell bash_command)

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

Upvotes: 2

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38539

The more portable way of inserting a command result as a part of the current command in shell is using `` instead of $(python3 -m pybind11 --includes). Make does not interpret such quotes.

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

Upvotes: 1

MadScientist
MadScientist

Reputation: 100856

Makefiles run commands in /bin/sh (by default). That can sometimes make a difference, if you're using bash features; but you're not so that doesn't matter.

The issue here is that the $ character is special to make: it introduces make variables. If you want to pass a $ to the shell you have to double it:

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

Upvotes: 2

Related Questions