2015-11-04 90 views
0

我试着写Makefile文件在Ubuntu 14.04秩序的Makefile

这里OpenCV的文件例如: 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 [email protected] $< 

我得到

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 

但是,如果我用这种形式为什么标准Makefile例如没有为工作顺利编译

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

all: minimal 

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

我?我们应该在makefile中使用libs,include和* .cpp的顺序是什么?

+0

你列出的第一个Makefile有没有明确的目标,以便让不知道,当你运行'make'建设什么样的。在你有'somefile.cpp'的地方运行'make somefile',它会工作。在命令行中查看库时,会搜索*一次*。所以他们需要在*之后*使用它们的任何东西。 –

回答

0

您需要CXXFLAGS而不是CFLAGSLDFLAGS而不是LIBS。见implicit variables

你整个的Makefile可能是

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

all: minimal 
+0

他正在使用他正在设置的旗帜。这不是问题。 –

+0

@EtanReisner我以为他在问为什么标准的makefile不适合他。 – jayant

+0

我读到,问为什么第一个简单的示例makefile不起作用。不过,您使用内置规则的建议是一个很好的建议。 –