6

I recently inherited some OpenCV code. I installed openCV on my mac, built in in XCode, and then compiled and successfully ran my first openCV "hello world"-ish program.

Now I'm trying to run the code I was given, but I get errors that lead me to believe it's an issue with the original code being run on a 32-bit Windows system and mine being on a 64-bit Mac.

When I run the Makefile by entering "make"

CC = g++
CFLAGS =
LDFLAGS = -I/usr/local/include/opencv -lm -lopencv_core -lopencv_highgui -lopencv_video
ALL = vision

all: $(ALL)

vision: vision.o
    $(CC) $(LDFLAGS) -o $@ $^

vision.o: vision.cpp
    $(CC) $(LDFLAGS) -c $<

.PHONY: clean

clean:
    rm -rf *.o core* $(ALL)

I get the following output…

g++ -I/usr/local/include/opencv -lm -lopencv_core -lopencv_highgui -lopencv_video -o vision vision.o
Undefined symbols for architecture x86_64:
  "cv::equalizeHist(cv::Mat const&, cv::Mat&)", referenced from:
      _main in vision.o
  "cv::threshold(cv::Mat const&, cv::Mat&, double, double, int)", referenced from:
      _main in vision.o

ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [vision] Error 1

I'm confused; does this mean my install of OpenCV is wrong, the code (those methods specifically) needs to be changed, or something else entirely?

Note: When I comment out the problem methods from the vision.cpp code, everything compiles just fine.

2
  • There's a command-line tool named file, I would like to see the output of it when you use it on the hello world you compiled for opencv: file hello_world Apr 25, 2011 at 17:44
  • Thanks I learned a new command :) The output is: vision: Mach-O 64-bit executable x86_64
    – Warpling
    Apr 26, 2011 at 6:03

3 Answers 3

16

Add opencv_imgprocto your LDFLAGS:

LDFLAGS = -I/usr/local/include/opencv -lm -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgproc
2
  • Is the LDFLAGS in the Makefile?
    – Larry Hipp
    Aug 28, 2013 at 2:19
  • @hipplar pretty sure they were.
    – Warpling
    Oct 8, 2014 at 22:08
3

Here is a working example:

CXX = g++

SOURCES = aaa.cpp bbb.cpp
OBJS = $(SOURCES:.cpp=.o)

CXXFLAGS = -I. -I/opt/local/include \
            -std=c++11 -stdlib=libc++ \
            -g3 -Wall -O0
            # -std=c++0x -arch x86_64 -stdlib=libc++ \

LDFLAGS = -L/opt/local/lib -L/usr/lib $(pkg-config --libs --cflags opencv) -lm -ljpeg
LDFLAGS = -L/opt/local/lib -L/usr/lib -I/opt/local/include/opencv -I/opt/local/include -L/opt/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lm -ljpeg

.o:
    $(CXX) $(CXXFLAGS) -o $@ -c $^

all: $(OBJS)
    $(CXX) $(CXXFLAGS) $(LDFLAGS) -o out $(OBJS)

clean:
    rm -rf *.o
1

You can also have the computer guess you the libraries automatically:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.