gpt4 book ai didi

c++ - OpenCV 2.4.8 编译链接错误

转载 作者:行者123 更新时间:2023-11-28 07:01:28 25 4
gpt4 key购买 nike

我是 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_PATHLD_LIBRARY_PATH env 变量

最佳答案

Here's a pretty decent reference on makefiles .

在您发表评论后,我使用上述引用创建了一个 Makefile,它在我的系统上编译您的代码(尽管它在运行时会抛出逻辑错误)。我原本以为您的图书馆的顺序可能是错误的。这是真的,因为 gnu C++ 编译器需要排序,你的库引用需要放在最后。但是,看起来至少对于最新的 Ubuntu opencv 库,您也没有正确的库名称。

你有:

# "make" makes the executable.
$(EXEC): $(OBJ)
$(CC) $(LFLAGS) $(OBJ) -o $(EXEC)

将此更改为:

$(EXEC): $(OBJ) 
$(CC) -o $(EXEC) $(OBJ) $(LFLAGS)

LFLAGS 为:

LFLAGS = -L/home/my_username/ext/lib/ -lopencv_core -lopencv_highgui 

试一试,让我知道进展如何。如果这不起作用,那么您没有使用 -L 命令引用库的正确位置。当我使用 aptitude 在我的系统上安装它们时,它们位于 /usr/lib 文件夹中。

关于c++ - OpenCV 2.4.8 编译链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22418443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com