0

I'm trying to build my project using Makefile:

source := $(wildcard **/*.cpp)
headers := $(wildcard include/*.h)
objects := ${source:.cpp=.o}

run: $(objects)
        g++ -o run $(objects)
        diff -u <(cat $(source)) <(clang-format $(source))
%.o: %.cpp tests/%.cpp $(headers)
        g++ $@ -o $<

And execution throws error:

/bin/sh: 1: Syntax error: "(" unexpected 
make: *** [Makefile:8: run] Error 2

Then I tried to add #!/bin/bash at the beginning of the makefile but got the same result.

What could be the problem and how can I fix it?

Potatmen
  • 13
  • 3
  • 1
    make will use the shell specified in the `SHELL` environment variable (probably falling back on `/bin/sh`) to execute each command for a target. In your case, that does not seem to be bash, so you can't use bash features. Either adjust the diff line to be something like `bash -c 'diff -u ....'`, or ensure `SHELL` is set to the bash shell's location before running make. – Zastai Jun 21 '22 at 09:04
  • 2
    `g++ $@ -o $<` looks wrong. I think you want `g++ $< -c -o $@` – Ted Lyngmo Jun 21 '22 at 09:39
  • 1
    I would also separate the `diff`ing and make a separate targets for that. [Example](https://pastebin.com/KzQZztMp) – Ted Lyngmo Jun 21 '22 at 09:48
  • Just to be clear, `make` **NEVER** uses the value of the `SHELL` environment variable. That would be a disaster for portability. Make always uses `/bin/sh` as its shell, unless you explicitly set `SHELL` in your makefile. – MadScientist Jun 21 '22 at 17:44

0 Answers0