【发布时间】:2015-06-18 12:56:27
【问题描述】:
我是 OpenCV 的初学者,我必须从源代码编译,因为我在 HPC 机器上的帐户上使用它。我在本地编译它,使其存在于我的主目录~/ext 下。现在我正在尝试编译 simple example from this documentation page,但在编译和链接我的新本地 openCV 安装时遇到了问题。
这是我的 test.cpp 文件中的代码:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
if( argc != 2 || !image.data )
{
printf( "No image data \n" );
return -1;
}
namedWindow( "Display Image", WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
这是我的简单 Makefile:
CC = g++
SRC = test.cpp
EXEC = test
CFLAGS = -I/home/my_username/ext/include/
LFLAGS = -L/home/my_username/ext/lib/ -lcxcore -lcv -lhighgui -lcvaux -lml
# YOU PROBABLY DO NOT HAVE TO CHANGE ANYTHING BELOW THIS LINE.
# This generates a list of object file names from the source file names
OBJ = $(addsuffix .o, $(basename $(SRC)))
# "make" makes the executable.
$(EXEC): $(OBJ)
$(CC) $(LFLAGS) $(OBJ) -o $(EXEC)
# This says how to build an object (.o) file from a source (.c) file
%.o : %.cpp
$(CC) $(CFLAGS) -c $< -o $@
# "make clean" deletes objects and executable
clean:
rm -f $(EXEC) *.o
使用此配置,当我尝试make 时收到以下错误。
g++ -I/home/kjorg50/ext/include/ -c test.cpp -o test.o
g++ -L/home/kjorg50/ext/lib/ -lcxcore -lcv -lhighgui -lcvaux -lml test.o -o test
test.o: In function `main':
test.cpp:(.text+0x1c7): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test.cpp:(.text+0x1fb): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
test.o: In function `cv::Mat::operator=(cv::Mat const&)':
test.cpp:(.text._ZN2cv3MataSERKS0_[cv::Mat::operator=(cv::Mat const&)]+0x111): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
test.o: In function `cv::Mat::release()':
test.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
make: *** [test] Error 1
我很确定我缺少一些 #include 语句,或者我的 CFLAGS 和/或 LFLAGS 值不正确。 那么,有人知道我在编译中缺少哪些库文件吗?
编辑 - 为了让它编译,我必须将正确的路径添加到我的 LIBRARY_PATH 和 LD_LIBRARY_PATH 环境变量中
【问题讨论】:
-
附带说明,您应该在尝试使用 argv[1] 作为文件名读取图像之前测试参数的数量,而不是在它之后。
-
@lightalchemist 谢谢我意识到这一点,但我不太关心这个 c++ 代码。我更关心的是能够用这个本地安装编译一个 openCV 程序。
标签: c++ opencv compiler-errors