Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

macos

gcc

I'm trying to compile my code on OSX El Capitan. This is my Makefile

TARGET   = proj_name

CC       = gcc
# compiling flags 
CFLAGS   = -std=c99 -Wall -I.

LINKER   = gcc -o
# linking flags 
LFLAGS   = -Wall -I. -lm

SRCDIR   = src
OBJDIR   = obj
BINDIR   = bin

SOURCES  := $(wildcard $(SRCDIR)/*.c)
INCLUDES := $(wildcard $(SRCDIR)/*.h)
OBJECTS  := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
rm       = rm -f


$(BINDIR)/$(TARGET): $(OBJECTS)
    @$(LINKER) $@ $(LFLAGS) $(OBJECTS)
    @echo "Linking complete!"

$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
    @$(CC) $(CFLAGS) -c $< -o $@
    @echo "Compiled "$<" successfully!"

.PHONEY: clean
clean:
    @$(rm) $(OBJECTS)
    @echo "Cleanup complete!"

.PHONEY: remove
remove: clean
    @$(rm) $(BINDIR)/$(TARGET)
    @echo "Executable removed!"

I keep getting the following error while compiling on El Capitan

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

I understand that its a linker issue, but if someone could help me amending the Makefile, it would really help.

like image 454
Thomas G Avatar asked Oct 07 '15 04:10

Thomas G


1 Answers

Errno 2 means (google for something like errno list):

#define ENOENT       2  /* No such file or directory */

bin/proj is relative path.

Looking at the Makefile, the most likely cause seems to be, bin directory simply does not exist. ld will not try create it if it is not there. To fix, add

mkdir -p $(BINDIR)

before $(LINKER) line (-p switch allows creating a path if it does not exist, which in this case prevents error if bin already exists).

A side note: Another common cause with relative paths is, that working directory is not what you think it is, when ld is run. Adding command like pwd to before $(LINKER) command would help troubleshooting this. But looking at Makefile, this probably is not the reason here.

like image 176
hyde Avatar answered Oct 13 '22 18:10

hyde