Moirae
Moirae

Reputation: 139

OpenCv Makefile Undefined symbols for architecture x86_64 error

I would appreciate some help with this error which I get when I run my Makefile. The code that I have was compiling fine before I separated it into different files.

I have main.c file in which is main function and "color.h" included. Other files are color.h and color.c . Color.c has color.h included aswell. This is my first Makefile and I've read many threads and topics about that so please correct me if I'm wrong.

This is the Makefile

CC = g++

CPPFLAGS = `pkg-config opencv --cflags`
LDFLAGS = `pkg-config opencv --libs`

ARCH = -arch x86_64

DEPENDENCIES = main.o color.o

# FINAL OUTPUTS
main: $(DEPENDENCIES)
    $(CC) $(ARCH) $(LDFLAGS) -o $(DEPENDENCIES) 

# MODULES
main.o: main.c color.h
    $(CC) $(ARCH) $(CPPFLAGS) -c main.c

colorOps.o: color.c
    $(CC) $(ARCH) $(CPPFLAGS) -c colorOps.c


# CLEAN
clean:
    -rm main $(DEPENDENCIES)

and this is the error that I get

Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [main] Error 1

I'm running OpenCv installed via Macports on Mac OS X 10.8.4, and as I said, everything was working fine until I separated the files.

Thank you very much!

Upvotes: 1

Views: 1897

Answers (1)

Ahmed Masud
Ahmed Masud

Reputation: 22374

You almost have it right:

You just need to add the output filename (program name) to the linking step:

main: $(DEPENDENCIES)
    $(CC) $(ARCH) $(LDFLAGS) -o $@ $(DEPENDENCIES) 
#                            ^^^^^

Hehe you just forgot that. If you had looked at the command-line output you'd have seen that it prints out:

 gcc -arch x86_64 /opencv-ld-flags/ -o main.o color.o

So all it's doing is trying to OUTPUT to main.o contents of color.o where as what you intend is:

 gcc -arch x86_64 /opencv-ld-flags/ -o main main.o color.o

The $@ will evaluate to the target file (in this case main) on the left side of the colon (:) in the build rule statement.

Cleaner make file

CC = g++

# CPPFLAGS are different from CFLAGS 
# keep CPPFLAGS name only for pre-processing flags 

CFLAGS = `pkg-config opencv --cflags`
LDFLAGS = `pkg-config opencv --libs`

ARCH = -arch x86_64

# DEPENDENCIES is a slightly confusing name
# You will see how I generate dependencies below
# So I replaced DEPENDENCIES with OBJ

SRC=main.c color.c
OBJ=$(SRC:.c=.o)

# INCLUDES flags should be of the form -Idir/path etc.
# Reason for separating this out is to help with 
# generating dependencies below

CPPFLAGS=
INCLUDES=

TARGET=main

default: $(TARGET)

$(TARGET): $(OBJ)
         $(CC) $(LDFLAGS) -o $@ $(OBJ) 

%.o: %.c
    $(CC)  $(CPPFLAGS) $(CFLAGS) $(INCLUDES) -c $< -o $@ 

# CLEAN
clean:
    -rm main $(OBJ)


# Create a rule to generate dependencies information for the makefile

.deps: $(SRC)
     $(CC) -o .deps $(CPPFLAGS) $(INCLUDES) -M -E $(SRC) 

#include dependencies
include .deps

Upvotes: 1

Related Questions