0420 测试记录 gcc 编译时 库文件 头文件问题及其解决方案


[root@localhost c]# g++ -L/usr/local/lib -I/usr/local/includes -o morphology morphology.c
morphology.c:1:16: 错误:cv.h:没有那个文件或目录
morphology.c:2:21: 错误:highgui.h:没有那个文件或目录
morphology.c:6: 错误:expected constructor, destructor, or type conversion before ‘*’ token
morphology.c:7: 错误:expected constructor, destructor, or type conversion before ‘*’ token
morphology.c:9: 错误:expected constructor, destructor, or type conversion before ‘*’ token
morphology.c:10: 错误:‘CV_SHAPE_RECT’ 在此作用域中尚未声明
morphology.c: In function ‘void OpenClose(int)’:
morphology.c:22: 错误:‘element’ 在此作用域中尚未声明
morphology.c:22: 错误:‘cvCreateStructuringElementEx’ 在此作用域中尚未声明
morphology.c:25: 错误:‘src’ 在此作用域中尚未声明
morphology.c:25: 错误:‘dst’ 在此作用域中尚未声明
morphology.c:25: 错误:‘cvErode’ 在此作用域中尚未声明
morphology.c:26: 错误:‘cvDilate’ 在此作用域中尚未声明
morphology.c:30: 错误:‘src’ 在此作用域中尚未声明
morphology.c:30: 错误:‘dst’ 在此作用域中尚未声明
morphology.c:30: 错误:‘cvDilate’ 在此作用域中尚未声明
morphology.c:31: 错误:‘cvErode’ 在此作用域中尚未声明
morphology.c:33: 错误:‘cvReleaseStructuringElement’ 在此作用域中尚未声明
morphology.c:34: 错误:‘dst’ 在此作用域中尚未声明
morphology.c:34: 错误:‘cvShowImage’ 在此作用域中尚未声明
morphology.c: In function ‘void ErodeDilate(int)’:
morphology.c:42: 错误:‘element’ 在此作用域中尚未声明
morphology.c:42: 错误:‘cvCreateStructuringElementEx’ 在此作用域中尚未声明
morphology.c:45: 错误:‘src’ 在此作用域中尚未声明
morphology.c:45: 错误:‘dst’ 在此作用域中尚未声明
morphology.c:45: 错误:‘cvErode’ 在此作用域中尚未声明
morphology.c:49: 错误:‘src’ 在此作用域中尚未声明
morphology.c:49: 错误:‘dst’ 在此作用域中尚未声明
morphology.c:49: 错误:‘cvDilate’ 在此作用域中尚未声明
morphology.c:51: 错误:‘cvReleaseStructuringElement’ 在此作用域中尚未声明
morphology.c:52: 错误:‘dst’ 在此作用域中尚未声明
morphology.c:52: 错误:‘cvShowImage’ 在此作用域中尚未声明
morphology.c: In function ‘int main(int, char**)’:
morphology.c:59: 错误:‘src’ 在此作用域中尚未声明
morphology.c:59: 错误:‘cvLoadImage’ 在此作用域中尚未声明
morphology.c:69: 错误:‘dst’ 在此作用域中尚未声明
morphology.c:69: 错误:‘src’ 在此作用域中尚未声明
morphology.c:69: 错误:‘cvCloneImage’ 在此作用域中尚未声明
morphology.c:72: 错误:‘cvNamedWindow’ 在此作用域中尚未声明
morphology.c:76: 错误:‘cvCreateTrackbar’ 在此作用域中尚未声明
morphology.c:85: 错误:‘cvWaitKey’ 在此作用域中尚未声明
morphology.c:90: 错误:‘CV_SHAPE_ELLIPSE’ 在此作用域中尚未声明
morphology.c:92: 错误:‘CV_SHAPE_RECT’ 在此作用域中尚未声明
morphology.c:94: 错误:‘CV_SHAPE_CROSS’ 在此作用域中尚未声明
morphology.c:100: 错误:‘cvReleaseImage’ 在此作用域中尚未声明
morphology.c:104: 错误:‘cvDestroyWindow’ 在此作用域中尚未声明
[root@localhost c]# g++ -L/usr/local/lib -I/usr/local/include/opencv -o morphology morphology.c
/tmp/cchWFzpS.o: In function `ErodeDilate(int)':
morphology.c:(.text+0x66): undefined reference to `cvCreateStructuringElementEx'
morphology.c:(.text+0x9a): undefined reference to `cvErode'
morphology.c:(.text+0xc5): undefined reference to `cvDilate'
morphology.c:(.text+0xd1): undefined reference to `cvReleaseStructuringElement'
morphology.c:(.text+0xe6): undefined reference to `cvShowImage'
/tmp/cchWFzpS.o: In function `OpenClose(int)':
morphology.c:(.text+0x156): undefined reference to `cvCreateStructuringElementEx'
morphology.c:(.text+0x18a): undefined reference to `cvErode'
morphology.c:(.text+0x1b3): undefined reference to `cvDilate'
morphology.c:(.text+0x1de): undefined reference to `cvDilate'
morphology.c:(.text+0x207): undefined reference to `cvErode'
morphology.c:(.text+0x213): undefined reference to `cvReleaseStructuringElement'
morphology.c:(.text+0x228): undefined reference to `cvShowImage'
/tmp/cchWFzpS.o: In function `main':
morphology.c:(.text+0x27a): undefined reference to `cvLoadImage'
morphology.c:(.text+0x2b2): undefined reference to `cvCloneImage'
morphology.c:(.text+0x2cb): undefined reference to `cvNamedWindow'
morphology.c:(.text+0x2df): undefined reference to `cvNamedWindow'
morphology.c:(.text+0x325): undefined reference to `cvCreateTrackbar'
morphology.c:(.text+0x357): undefined reference to `cvCreateTrackbar'
morphology.c:(.text+0x37f): undefined reference to `cvWaitKey'
morphology.c:(.text+0x41d): undefined reference to `cvReleaseImage'
morphology.c:(.text+0x429): undefined reference to `cvReleaseImage'
morphology.c:(.text+0x435): undefined reference to `cvDestroyWindow'
morphology.c:(.text+0x441): undefined reference to `cvDestroyWindow'
collect2: ld 返回 1
[root@localhost c]# g++ `pkg-config opencv --libs --cflags opencv` drawing.c -o drawing -L/usr/local/lib -I/usr/local/include/opencv
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
/tmp/ccY9gGMv.o: In function `main':
drawing.c:(.text+0x101): undefined reference to `cvCreateImage'
drawing.c:(.text+0x11b): undefined reference to `cvNamedWindow'
drawing.c:(.text+0x129): undefined reference to `cvSetZero'
drawing.c:(.text+0x13f): undefined reference to `cvShowImage'
drawing.c:(.text+0x370): undefined reference to `cvLine'
drawing.c:(.text+0x386): undefined reference to `cvShowImage'
drawing.c:(.text+0x392): undefined reference to `cvWaitKey'
drawing.c:(.text+0x587): undefined reference to `cvRectangle'
drawing.c:(.text+0x59d): undefined reference to `cvShowImage'
drawing.c:(.text+0x5a9): undefined reference to `cvWaitKey'
drawing.c:(.text+0x855): undefined reference to `cvEllipse'
drawing.c:(.text+0x86b): undefined reference to `cvShowImage'
drawing.c:(.text+0x877): undefined reference to `cvWaitKey'
drawing.c:(.text+0xc41): undefined reference to `cvPolyLine'
drawing.c:(.text+0xc57): undefined reference to `cvShowImage'
drawing.c:(.text+0xc63): undefined reference to `cvWaitKey'
drawing.c:(.text+0xfd3): undefined reference to `cvFillPoly'
drawing.c:(.text+0xfe9): undefined reference to `cvShowImage'
drawing.c:(.text+0xff5): undefined reference to `cvWaitKey'
drawing.c:(.text+0x11b7): undefined reference to `cvCircle'
drawing.c:(.text+0x11cd): undefined reference to `cvShowImage'
drawing.c:(.text+0x11d9): undefined reference to `cvWaitKey'
drawing.c:(.text+0x1449): undefined reference to `cvInitFont'
drawing.c:(.text+0x14d6): undefined reference to `cvPutText'
drawing.c:(.text+0x14ec): undefined reference to `cvShowImage'
drawing.c:(.text+0x14f8): undefined reference to `cvWaitKey'
drawing.c:(.text+0x1545): undefined reference to `cvInitFont'
drawing.c:(.text+0x156f): undefined reference to `cvGetTextSize'
drawing.c:(.text+0x15b5): undefined reference to `cvCloneImage'
drawing.c:(.text+0x16ce): undefined reference to `cvPutText'
drawing.c:(.text+0x16e4): undefined reference to `cvShowImage'
drawing.c:(.text+0x16f0): undefined reference to `cvWaitKey'
drawing.c:(.text+0x170d): undefined reference to `cvWaitKey'
drawing.c:(.text+0x171b): undefined reference to `cvReleaseImage'
drawing.c:(.text+0x1729): undefined reference to `cvReleaseImage'
drawing.c:(.text+0x1735): undefined reference to `cvDestroyWindow'
/tmp/ccY9gGMv.o: In function `cvSubS':
drawing.c:(.text.cvSubS[cvSubS]+0x89): undefined reference to `cvAddS'
collect2: ld 返回 1

[root@localhost c]# export PKG_CONFIG_PATH=$PKG_CONFGI_PATH:/usr/local/lib/pkgconfig
[root@localhost c]# g++ `pkg-config opencv --libs --cflags opencv` drawing.c -o drawing -I/usr/local/include/opencv -L
g++: ‘-L’缺少实参

[root@localhost c]# g++ `pkg-config opencv --libs --cflags opencv` drawing.c -o drawing -I/usr/local/include/opencv
[root@localhost c]# ls
agaricus-lepiota.data  delaunay.c      fruits.jpg               Makefile      pic3.png
airplane.jpg           demhist.c       houghlines.c             Makefile.am   pic4.png
baboon.jpg             dft.c           image.cpp                makefile.gnu  pic5.png
blobtrack.cpp          distrans.c      inpaint.cpp              Makefile.in   pic6.png
build_all.sh           drawing         kalman.c                 makefile.ms   pyramid_segmentation.c
camshiftdemo.c         drawing.c       kmeans.c                 minarea.c     squares.c
contours.c             edge.c          laplace.c                morphology.c  stuff.jpg
convert_cascade.c      facedetect.c    lena.jpg                 motempl.c     watershed.cpp
convexhull.c           facedetect.cmd  letter_recog.cpp         mushroom.cpp
cvsample.dsp           ffilldemo.c     letter-recognition.data  pic1.png
cvsample.vcproj        fitellipse.c    lkdemo.c                 pic2.png
[root@localhost c]# ./drawing
./drawing: error while loading shared libraries: libcxcore.so.1: cannot open shared object file: No such file or directory
[root@localhost c]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
[root@localhost c]# ./drawing


OK


重新测试与于原因分析
[fedora@localhost c]$ echo $PKG_CONFIG_PATH
[fedora@localhost c]$ g++ `pkg-config opencv --libs --cflags opencv` drawing.c -o drawing
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
drawing.c:6:16: 错误:cv.h:没有那个文件或目录
drawing.c:7:21: 错误:highgui.h:没有那个文件或目录
drawing.c:16: 错误:‘CvScalar’ 没有命名一个类型
drawing.c: In function ‘int main(int, char**)’:
drawing.c:24: 错误:‘CV_AA’ 在此作用域中尚未声明
drawing.c:26: 错误:‘CvPoint’ 在此作用域中尚未声明
drawing.c:26: 错误:expected `;' before ‘pt1’
drawing.c:28: 错误:‘CvSize’ 在此作用域中尚未声明
drawing.c:28: 错误:expected `;' before ‘sz’
drawing.c:29: 错误:expected `;' before ‘ptt’
drawing.c:30: 错误:‘pt’ 在此作用域中尚未声明
drawing.c:32: 错误:‘CvFont’ 在此作用域中尚未声明
drawing.c:32: 错误:expected `;' before ‘font’
drawing.c:33: 错误:‘CvRNG’ 在此作用域中尚未声明
drawing.c:33: 错误:expected `;' before ‘rng’
drawing.c:36: 错误:expected `;' before ‘text_size’
drawing.c:39: 错误:‘IplImage’ 在此作用域中尚未声明
drawing.c:39: 错误:‘image’ 在此作用域中尚未声明
drawing.c:39: 错误:‘cvSize’ 在此作用域中尚未声明
drawing.c:39: 错误:‘cvCreateImage’ 在此作用域中尚未声明
drawing.c:40: 错误:‘image2’ 在此作用域中尚未声明
drawing.c:43: 错误:‘cvNamedWindow’ 在此作用域中尚未声明
drawing.c:44: 错误:‘cvZero’ 在此作用域中尚未声明
drawing.c:45: 错误:‘cvShowImage’ 在此作用域中尚未声明
drawing.c:47: 错误:‘rng’ 在此作用域中尚未声明
drawing.c:47: 错误:‘cvRNG’ 在此作用域中尚未声明
drawing.c:48: 错误:‘ptt’ 在此作用域中尚未声明
drawing.c:56: 错误:‘pt1’ 在此作用域中尚未声明
drawing.c:56: 错误:‘cvRandInt’ 在此作用域中尚未声明
drawing.c:58: 错误:‘pt2’ 在此作用域中尚未声明
drawing.c:61: 错误:‘random_color’ 在此作用域中尚未声明
drawing.c:61: 错误:‘cvLine’ 在此作用域中尚未声明
drawing.c:63: 错误:‘cvWaitKey’ 在此作用域中尚未声明
drawing.c:68: 错误:‘pt1’ 在此作用域中尚未声明
drawing.c:68: 错误:‘cvRandInt’ 在此作用域中尚未声明
drawing.c:70: 错误:‘pt2’ 在此作用域中尚未声明
drawing.c:73: 错误:‘random_color’ 在此作用域中尚未声明
drawing.c:73: 错误:‘cvRectangle’ 在此作用域中尚未声明
drawing.c:75: 错误:‘cvWaitKey’ 在此作用域中尚未声明
drawing.c:80: 错误:‘pt1’ 在此作用域中尚未声明
drawing.c:80: 错误:‘cvRandInt’ 在此作用域中尚未声明
drawing.c:82: 错误:‘sz’ 在此作用域中尚未声明
drawing.c:87: 错误:‘random_color’ 在此作用域中尚未声明
drawing.c:87: 错误:‘cvEllipse’ 在此作用域中尚未声明
drawing.c:89: 错误:‘cvWaitKey’ 在此作用域中尚未声明
drawing.c:94: 错误:‘cvRandInt’ 在此作用域中尚未声明
drawing.c:107: 错误:‘random_color’ 在此作用域中尚未声明
drawing.c:107: 错误:‘cvPolyLine’ 在此作用域中尚未声明
drawing.c:109: 错误:‘cvWaitKey’ 在此作用域中尚未声明
drawing.c:114: 错误:‘cvRandInt’ 在此作用域中尚未声明
drawing.c:127: 错误:‘random_color’ 在此作用域中尚未声明
drawing.c:127: 错误:‘cvFillPoly’ 在此作用域中尚未声明
drawing.c:129: 错误:‘cvWaitKey’ 在此作用域中尚未声明
drawing.c:134: 错误:‘pt1’ 在此作用域中尚未声明
drawing.c:134: 错误:‘cvRandInt’ 在此作用域中尚未声明
drawing.c:137: 错误:‘random_color’ 在此作用域中尚未声明
drawing.c:138: 错误:‘cvCircle’ 在此作用域中尚未声明
drawing.c:140: 错误:‘cvWaitKey’ 在此作用域中尚未声明
drawing.c:145: 错误:‘pt1’ 在此作用域中尚未声明
drawing.c:145: 错误:‘cvRandInt’ 在此作用域中尚未声明
drawing.c:148: 错误:‘font’ 在此作用域中尚未声明
drawing.c:150: 错误:‘cvRound’ 在此作用域中尚未声明
drawing.c:150: 错误:‘cvInitFont’ 在此作用域中尚未声明
drawing.c:152: 错误:‘random_color’ 在此作用域中尚未声明
drawing.c:152: 错误:‘cvPutText’ 在此作用域中尚未声明
drawing.c:154: 错误:‘cvWaitKey’ 在此作用域中尚未声明
drawing.c:157: 错误:‘font’ 在此作用域中尚未声明
drawing.c:157: 错误:‘CV_FONT_HERSHEY_COMPLEX’ 在此作用域中尚未声明
drawing.c:157: 错误:‘cvInitFont’ 在此作用域中尚未声明
drawing.c:159: 错误:‘text_size’ 在此作用域中尚未声明
drawing.c:159: 错误:‘cvGetTextSize’ 在此作用域中尚未声明
drawing.c:161: 错误:‘pt1’ 在此作用域中尚未声明
drawing.c:163: 错误:‘cvCloneImage’ 在此作用域中尚未声明
drawing.c:167: 错误:‘cvScalarAll’ 在此作用域中尚未声明
drawing.c:167: 错误:‘cvSubS’ 在此作用域中尚未声明
drawing.c:168: 错误:‘CV_RGB’ 在此作用域中尚未声明
drawing.c:168: 错误:‘cvPutText’ 在此作用域中尚未声明
drawing.c:170: 错误:‘cvWaitKey’ 在此作用域中尚未声明
drawing.c:174: 错误:‘cvWaitKey’ 在此作用域中尚未声明
drawing.c:175: 错误:‘cvReleaseImage’ 在此作用域中尚未声明
drawing.c:177: 错误:‘cvDestroyWindow’ 在此作用域中尚未声明
[fedora@localhost c]$

下面使用 -I/usr/local/include/opencv     (这里参数是i的大写字母I,而不是L的小写字母I)

[fedora@localhost c]$ echo $PKG_CONFIG_PATH

[fedora@localhost c]$ echo $LD_LIBRARY_PATH

[fedora@localhost c]$ g++ `pkg-config opencv --libs --cflags opencv` drawing.c -o drawing -I/usr/local/include/opencv
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
/tmp/ccCKA3G6.o: In function `main':
drawing.c:(.text+0x101): undefined reference to `cvCreateImage'
drawing.c:(.text+0x11b): undefined reference to `cvNamedWindow'
drawing.c:(.text+0x129): undefined reference to `cvSetZero'
drawing.c:(.text+0x13f): undefined reference to `cvShowImage'
drawing.c:(.text+0x370): undefined reference to `cvLine'
drawing.c:(.text+0x386): undefined reference to `cvShowImage'
drawing.c:(.text+0x392): undefined reference to `cvWaitKey'
drawing.c:(.text+0x587): undefined reference to `cvRectangle'
drawing.c:(.text+0x59d): undefined reference to `cvShowImage'
drawing.c:(.text+0x5a9): undefined reference to `cvWaitKey'
drawing.c:(.text+0x855): undefined reference to `cvEllipse'
drawing.c:(.text+0x86b): undefined reference to `cvShowImage'
drawing.c:(.text+0x877): undefined reference to `cvWaitKey'
drawing.c:(.text+0xc41): undefined reference to `cvPolyLine'
drawing.c:(.text+0xc57): undefined reference to `cvShowImage'
drawing.c:(.text+0xc63): undefined reference to `cvWaitKey'
drawing.c:(.text+0xfd3): undefined reference to `cvFillPoly'
drawing.c:(.text+0xfe9): undefined reference to `cvShowImage'
drawing.c:(.text+0xff5): undefined reference to `cvWaitKey'
drawing.c:(.text+0x11b7): undefined reference to `cvCircle'
drawing.c:(.text+0x11cd): undefined reference to `cvShowImage'
drawing.c:(.text+0x11d9): undefined reference to `cvWaitKey'
drawing.c:(.text+0x1449): undefined reference to `cvInitFont'
drawing.c:(.text+0x14d6): undefined reference to `cvPutText'
drawing.c:(.text+0x14ec): undefined reference to `cvShowImage'
drawing.c:(.text+0x14f8): undefined reference to `cvWaitKey'
drawing.c:(.text+0x1545): undefined reference to `cvInitFont'
drawing.c:(.text+0x156f): undefined reference to `cvGetTextSize'
drawing.c:(.text+0x15b5): undefined reference to `cvCloneImage'
drawing.c:(.text+0x16ce): undefined reference to `cvPutText'
drawing.c:(.text+0x16e4): undefined reference to `cvShowImage'
drawing.c:(.text+0x16f0): undefined reference to `cvWaitKey'
drawing.c:(.text+0x170d): undefined reference to `cvWaitKey'
drawing.c:(.text+0x171b): undefined reference to `cvReleaseImage'
drawing.c:(.text+0x1729): undefined reference to `cvReleaseImage'
drawing.c:(.text+0x1735): undefined reference to `cvDestroyWindow'
/tmp/ccCKA3G6.o: In function `cvSubS':
drawing.c:(.text.cvSubS[cvSubS]+0x89): undefined reference to `cvAddS'
collect2: ld 返回 1
[fedora@localhost c]$ export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
[fedora@localhost c]$ g++ `pkg-config opencv --libs --cflags opencv` drawing.c -o drawing -I/usr/local/include/opencv     (这里参数是i的大写字母I,而不是L的小写字母I)
[fedora@localhost c]$

编译过程中 PKG_CONIG_PATH参数主要用来提供include的搜索路径

上面指令中-I/usr/local/include/opencv 没用解决实际问题,没设置PKG_CONIG_PATH参数时候不成功。
可以看出
-I/usr/local/include/opencv 指定了头文件位置,但并没有然编译器遭到起定义位置。

下面去掉他,看看





[fedora@localhost c]$ echo $PKG_CONFIG_PATH
:/usr/local/lib/pkgconfig

[fedora@localhost c]$ g++ `pkg-config opencv --libs --cflags opencv` drawing.c -o drawing
[fedora@localhost c]$

可见这里,可见-I/usr/local/include/opencv 其实根本没用 ,全由PKG_CONIG_PATH参数进行指定。

下面运行程序:
[fedora@localhost c]$ ./drawing
./drawing: error while loading shared libraries: libcxcore.so.1: cannot open shared object file: No such file or directory
[fedora@localhost c]$

可见还需要指定动态链接库

[fedora@localhost c]$ echo $LD_LIBRARY_PATH

[fedora@localhost c]$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
[fedora@localhost c]$ ./drawing

成功

总结:
1、编译时使用PKG_CONIG_PATH变量进行头文件路径指定  
             参考:http://blog.csdn.net/bloghome/archive/2008/04/21/2312873.aspx
2、运行时使用LD_LIBRARY_PATH变量进行动态链接库指定


原始指令= g++ `pkg-config opencv --libs --cflags opencv` read_and_show.c -o drawing
实际指令= g++ -I/usr/local/include/opencv -L/usr/local/lib -lcxcore -lcv -lhighgui -lcvaux -lml read_and_show.c -o drawing

你可能感兴趣的:(function,测试,gcc,Path,reference,destructor)