0
votes

I am getting the following output from my makefile. My main goal is to compile everything in a directory. I expect to have many more cpp files in future. Any tips or advice is greatly appreciated!

Output:

g++ `pkg-config --libs --cflags opencv libexif` main.cpp -o main.o
g++ `pkg-config --libs --cflags opencv libexif` Image.cpp -o Image.o
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Image.o] Error 1

Current list of files: main.cpp, Image.h, Image.cpp

Here's the makefile:

CC=g++
CFLAGS= `pkg-config --libs --cflags opencv libexif`
LDFLAGS= `pkg-config --libs --cflags opencv libexif`
SOURCES=main.cpp Image.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=CvHelloWorld

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

clean:
    rm *.o
1
Shouldn't you compile with the -c option? - 5gon12eder
As a side note, you can use a glob to get all of the filenames in a directory without explicitly listing them. - Catalyst

1 Answers

0
votes

As suggested by @5gon12eder, you should use '-c'.

.cpp.o:
    $(CC) -c $(CFLAGS) -o $@ $<

Debug is easier if you interpret pkg-config results earlier:

CFLAGS  := $(shell pkg-config --cflags opencv libexif)
LDFLAGS := $(shell pkg-config --libs opencv libexif)
SOURCES := $(wildcard *.cpp)

[...]

.PHONY: clean