0

I'm trying to write makefile for opencv file on ubuntu 14.04

Here is example: Makefile to Compile OpenCV Code in C++ on Ubuntu/Linux

If I try yo use it in this form:

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

% : %.cpp
        g++ $(CFLAGS) $(LIBS) -o $@ $<

I get

make: *** No targets. Stop.

If I try

IFLAGS = `pkg-config --cflags opencv`
LFLAGS = `pkg-config --libs opencv`

all: minimal

minimal: minimal.cpp
    g++ $(IFLAGS) $(LFLAGS) -o minimal minimal.cpp

I get

g++ `pkg-config --cflags opencv` `pkg-config --libs opencv` -o minimal minimal.cpp
/tmp/ccKWVtBh.o: In function `main':
minimal.cpp:(.text+0x4d): undefined reference to `cv::imread(std::string const&, int)'
minimal.cpp:(.text+0x123): undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
minimal.cpp:(.text+0x1a6): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
minimal.cpp:(.text+0x1ce): undefined reference to `cv::waitKey(int)'
/tmp/ccKWVtBh.o: In function `cv::Mat::~Mat()':
minimal.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccKWVtBh.o: In function `cv::Mat::operator=(cv::Mat const&)':
minimal.cpp:(.text._ZN2cv3MataSERKS0_[_ZN2cv3MataSERKS0_]+0x111): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
/tmp/ccKWVtBh.o: In function `cv::Mat::release()':
minimal.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference to `cv::Mat::deallocate()'
/tmp/ccKWVtBh.o: In function `cv::_InputArray::_InputArray<unsigned char>(cv::Mat_<unsigned char> const&)':
minimal.cpp:(.text._ZN2cv11_InputArrayC2IhEERKNS_4Mat_IT_EE[_ZN2cv11_InputArrayC5IhEERKNS_4Mat_IT_EE]+0x17): undefined reference to `vtable for cv::_InputArray'
/tmp/ccKWVtBh.o: In function `cv::Mat_<unsigned char>::operator=(cv::Mat const&)':
minimal.cpp:(.text._ZN2cv4Mat_IhEaSERKNS_3MatE[_ZN2cv4Mat_IhEaSERKNS_3MatE]+0x77): undefined reference to `cv::Mat::reshape(int, int, int const*) const'
minimal.cpp:(.text._ZN2cv4Mat_IhEaSERKNS_3MatE[_ZN2cv4Mat_IhEaSERKNS_3MatE]+0xdd): undefined reference to `cv::Mat::convertTo(cv::_OutputArray const&, int, double, double) const'
/tmp/ccKWVtBh.o: In function `cv::_OutputArray::_OutputArray<unsigned char>(cv::Mat_<unsigned char>&)':
minimal.cpp:(.text._ZN2cv12_OutputArrayC2IhEERNS_4Mat_IT_EE[_ZN2cv12_OutputArrayC5IhEERNS_4Mat_IT_EE]+0x2a): undefined reference to `vtable for cv::_OutputArray'
collect2: error: ld returned 1 exit status
make: *** [minimal] Error 1

But if I use this form it compiles successfully

IFLAGS = `pkg-config --cflags opencv`
LFLAGS = `pkg-config --libs opencv`

all: minimal

minimal: minimal.cpp
    g++ $(IFLAGS) -o minimal minimal.cpp $(LFLAGS)

Why standard makefile example don't work for me? What order of libs, includes and *.cpp we should use in makefile?

Community
  • 1
  • 1
mrgloom
  • 20,061
  • 36
  • 171
  • 301
  • The first makefile you listed has no explicit targets so make doesn't know what to build when you run `make`. Run `make somefile` where you have a `somefile.cpp` and it will work. Libraries are searched *once* when they are seen on the command line. So they need to be *after* anything that uses them. – Etan Reisner Nov 04 '15 at 19:35

1 Answers1

0

You need CXXFLAGS instead of CFLAGS and LDFLAGS instead of LIBS. See implicit variables

Your entire Makefile could be

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

all: minimal
jayant
  • 2,349
  • 1
  • 19
  • 27
  • He's using the flags he is setting. That isn't the problem. – Etan Reisner Nov 04 '15 at 19:34
  • @EtanReisner I thought he was asking why standard makefile doesn't work for him. – jayant Nov 04 '15 at 19:35
  • I read that as asking why the first simple example makefile didn't work. Your suggestion to use the built-in rules is a good one though. – Etan Reisner Nov 04 '15 at 19:42
  • @EtanReisner :) you're probably right! I see you've been around on SO for a while. I have been meaning to ask this to someone. In this kind of situation shall I delete my answer as it doesn't answer the question? – jayant Nov 04 '15 at 19:52
  • 1
    You could but it might be as worth it just to explain as you did that make has built-in rules and for simple uses the makefile can be as simple as you showed. That's a valid answer, to my mind, to the "meta" question of "how to get a simple makefile to build against opencv to work". – Etan Reisner Nov 04 '15 at 19:57
  • @EtanReisner Thanks for the explanation – jayant Nov 04 '15 at 20:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94243/discussion-between-jayant-and-etan-reisner). – jayant Nov 04 '15 at 20:13
  • http://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc May this help you. – cm161 Nov 05 '15 at 11:24