Search code examples
c++macosmakefileclang

Makefile confusion ' errno=2 for architecture x86_64'


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...


Solution

  • 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.