【发布时间】:2015-11-04 19:26:32
【问题描述】:
我正在尝试在 ubuntu 14.04 上为 opencv 文件编写 makefile
这是一个例子: Makefile to Compile OpenCV Code in C++ on Ubuntu/Linux
如果我尝试以这种形式使用它:
CFLAGS = `pkg-config --cflags opencv`
LIBS = `pkg-config --libs opencv`
% : %.cpp
g++ $(CFLAGS) $(LIBS) -o $@ $<
我明白了
make: *** 没有目标。停下来。
如果我尝试
IFLAGS = `pkg-config --cflags opencv`
LFLAGS = `pkg-config --libs opencv`
all: minimal
minimal: minimal.cpp
g++ $(IFLAGS) $(LFLAGS) -o minimal minimal.cpp
我明白了
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
但如果我使用这种形式,它编译成功
IFLAGS = `pkg-config --cflags opencv`
LFLAGS = `pkg-config --libs opencv`
all: minimal
minimal: minimal.cpp
g++ $(IFLAGS) -o minimal minimal.cpp $(LFLAGS)
为什么标准的 makefile 示例对我不起作用?我们应该在 makefile 中使用什么顺序的库、包含和 *.cpp?
【问题讨论】:
-
您列出的第一个 makefile 没有明确的目标,因此当您运行
make时,make 不知道要构建什么。在您有somefile.cpp的地方运行make somefile,它将起作用。库在命令行上出现时会被搜索一次。所以他们需要在任何使用他们的东西之后。