0

I have a Makefile that looks like this:

CFLAGS = `pkg-config opencv --cflags` -Wall -Wextra -Werror -pedantic -std=c++11
LIBS = `pkg-config opencv --libs`

% : src/%.cpp src/%.h
        $(CXX) $(CFLAGS) -g -o bin/$@ $< -O3 $(LIBS)

clean:
        rm -rf bin/*

When I run `make skin-detect'

I get this error:

make skin-detect
c++ `pkg-config opencv --cflags` -Wall -Wextra -Werror -pedantic -std=c++11 -g -o bin/skin-detect src/skin-detect.cpp -O3 `pkg-config opencv --libs`
ld: can't open output file for writing: bin/skin-detect, errno=2 for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [skin-detect] Error 1

I'm on Mac Catalina. This post:

ld: can't open output file for writing: bin/s, errno=2 for architecture x86_64

seems to address the issue, but I don't understand the recommendation. Do I need to instruct bin/skin-detect/ to be made manually? I'm very new to compilers...

connor449
  • 1,549
  • 2
  • 18
  • 49

2 Answers2

2

you can add a rule to your makefile to create the directory. something like:

BINDIR=bin
CFLAGS = `pkg-config opencv --cflags` -Wall -Wextra -Werror -pedantic -std=c++11
LIBS = `pkg-config opencv --libs`

$(BINDIR):
        mkdir -p $(BINDIR)

% : src/%.cpp src/%.h $(BINDIR)
        $(CXX) $(CFLAGS) -g -o bin/$@ $< -O3 $(LIBS)
SHR
  • 7,940
  • 9
  • 38
  • 57
  • You need an order-only dependency for the directory. See https://stackoverflow.com/a/41464506/412080 – Maxim Egorushkin Dec 08 '20 at 17:01
  • 1
    Yes you should never use basic prerequisites to depend on directories. Even simpler is to just put the `mkdir` in the recipe, before you invoke the link step. – MadScientist Dec 08 '20 at 17:16
  • I just replaced my Makefile with your suggestion and I still get this error: `ld: can't open output file for writing: bin/skin-detect, errno=2 for architecture x86_64 ` – connor449 Dec 08 '20 at 17:18
  • @MadScientist how do I do this? – connor449 Dec 08 '20 at 17:18
1

Can't add formatting in comments: you should be able to do something like this:

% : src/%.cpp src/%.h
        mkdir -p bin
        $(CXX) $(CFLAGS) -g -o bin/$@ $< -O3 $(LIBS)

if the problem is really that the bin directory doesn't exist. However of course there could be other issues.

One thing you said in your question mentioned bin/skin-detect/ with a trailing slash. Was that a typo? Or does bin/skin-detect already exist as a directory? Because if it already exists as a directory then the linker can't write a file with that name.

What does ls -l bin/skin-detect show? If it already exists as a directory, or even as a file that you don't have write permissions for, you should rename that directory or remove it if you don't want it, then try your build.

MadScientist
  • 92,819
  • 9
  • 109
  • 136