链接C中的Opencv库



我一直在尝试使用opencv库编写测试一个简单程序。我在教程中找到了以下测试代码。我正在运行OSX 10.9.2,并设法(我认为)使用自制软件在我的电脑上成功安装了opencv。我的问题是,我无法编译这些代码,因为每当我试图编译时,我的make文件都会抛出错误。我相信问题是我没有正确地连接图书馆,再多的谷歌搜索似乎也无法帮助我解决问题。

////////////////////////////////////////////////////////////////////////
//
// This is a simple, introductory OpenCV program. The program reads an
// image from a file, inverts it, and displays the result. 
//
////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>

int main(int argc, char** argv)
{
    IplImage* img = cvLoadImage( "Conumdrum.jpeg", 0 ); //change the name (image.jpg)   according to your Image filename.
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage("Example1", img);
    cvWaitKey(0);
    cvReleaseImage( &img );
    cvDestroyWindow( "Example1" );
    return 0;
}

我在网上找到了一个示例makefile,我试图修改它,但没有成功,下面是我的makefile:

# define the C compiler to use
CC = gcc
# define any compile-time flags
CFLAGS = -Wall -g
# define any directories containing header files other than /usr/include
#
INCLUDES = -I/Users/MY_NAME/Projects/Tank Game/Webcam_Tests/Webcam_Test_v1/include
# define library paths in addition to /usr/lib
#   if I wanted to include libraries not in /usr/lib I'd specify
#   their path using -Lpath, something like:
LFLAGS = -L/Users/MY_NAME/Projects/Tank Game/Webcam_Tests/Webcam_Test_v1/lib
# define any libraries to link into executable:
#   if I want to link in libraries (libx.so or libx.a) I use the -llibname 
#   option, something like (this will link in libmylib.so and libm.so:
LIBS = -l libopencv_core.dylib  -lm
# define the C source files
SRCS = Webcam_Test_v1.c
# define the C object files 
#
# This uses Suffix Replacement within a macro:
#   $(name:string1=string2)
#         For each word in 'name' replace 'string1' with 'string2'
# Below we are replacing the suffix .c of all words in the macro SRCS
# with the .o suffix
#
OBJS = $(SRCS:.c=.o)
# define the executable file 
MAIN = mycc
#
# The following part of the makefile is generic; it can be used to 
# build any executable just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
#
.PHONY: depend clean
all:    $(MAIN)
    @echo  Simple compiler named mycc has been compiled
$(MAIN): $(OBJS) 
    $(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)
# this is a suffix replacement rule for building .o's from .c's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .c file) and $@: the name of the target of the rule (a .o file) 
# (see the gnu make manual section about automatic variables)
.c.o:
    $(CC) $(CFLAGS) $(INCLUDES) -c $<  -o $@
clean:
    $(RM) *.o *~ $(MAIN)
depend: $(SRCS)
    makedepend $(INCLUDES) $^
# DO NOT DELETE THIS LINE -- make depend needs it

当我尝试编译时,终端会给我以下输出:

gcc -Wall -g -I/Users/MY_NAME/Projects/Tank Game/Webcam_Tests/Webcam_Test_v1/include -c Webcam_Test_v1.c  -o Webcam_Test_v1.o
gcc -Wall -g -I/Users/MY_NAME/Projects/Tank Game/Webcam_Tests/Webcam_Test_v1/include -o mycc Webcam_Test_v1.o -L/Users/MY_NAME/Projects/Tank Game/Webcam_Tests/Webcam_Test_v1/lib -l libopencv_core.dylib  -lm
ld: library not found for -llibopencv_core.dylib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mycc] Error 

任何帮助都将不胜感激,几天来我一直在努力解决这个问题。还应该注意的是,我是一个新手,尤其是在makefile方面。谢谢

您的链接器选项错误。尝试使用-lopencv_core而不是-l libopencv_core.dylib。例如,如果要链接文件名为libfoo.dylib的库,则正确的链接器选项为-lfoo。您还需要添加-lopencv_highgui

相关内容

  • 没有找到相关文章

最新更新