在C中链接Opencv库

时间:2014-04-12 16:27:45

标签: c opencv makefile

我一直在尝试使用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。谢谢。

1 个答案:

答案 0 :(得分:-1)

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