如果我尝试在 Ubuntu 下编译一个小型 opencv 测试程序,链接器无法解析符号。该程序是:
root@lynx:/root(13)# cat ~rose/Txt/projects/OpenCV/Examples/cv_version.c
#include "cv.h"
#include "highgui.h"
#include "opencv2/core/version.hpp"
#include <stdio.h>
int main( int argc, char** argv ) {
fprintf(stderr,"CV_MAJOR_VERSION=[%%d]%d CV_MINOR_VERSION=[%%d]%d\n",
CV_MAJOR_VERSION,CV_MINOR_VERSION);
#if defined CV_MAJOR_VERSION == 3
fprintf(stderr,"CV_MAJOR_VERSION=3 is defined\n");
#endif
#ifdef CV_MAJOR_VERSION
fprintf(stderr,"CV_MAJOR_VERSION is defined: %d\n",CV_MAJOR_VERSION);
#endif
#if CV_MAJOR_VERSION == 3
fprintf(stderr,"CV_MAJOR_VERSION=3 (if CV_MAJOR_VERSION == 3)\n");
#endif
return 0;
}
gcc 的输出是:
rose@lynx-ubuntu:/home/rose/Txt/projects/OpenCV/Examples(32)$ gcc -v `pkg-config --cflags opencv` `pkg-config --libs opencv` -lm -ocv_version cv_version.c
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04)
COLLECT_GCC_OPTIONS='-v' '-I' '/usr/include/opencv' '-o' 'cv_version' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/cc1 -quiet -v -I /usr/include/opencv -imultiarch x86_64-linux-gnu cv_version.c -quiet -dumpbase cv_version.c -mtune=generic -march=x86-64 -auxbase cv_version -version -fstack-protector -Wformat -Wformat-security -o /tmp/cc1aYSh7.s
GNU C (Ubuntu 4.8.4-2ubuntu1~14.04) version 4.8.4 (x86_64-linux-gnu)
compiled by GNU C version 4.8.4, GMP version 5.1.3, MPFR version 3.1.2-p3, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/opencv
/usr/lib/gcc/x86_64-linux-gnu/4.8/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
GNU C (Ubuntu 4.8.4-2ubuntu1~14.04) version 4.8.4 (x86_64-linux-gnu)
compiled by GNU C version 4.8.4, GMP version 5.1.3, MPFR version 3.1.2-p3, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 8e5167dd5049e4eb768f2a6a3a56b723
COLLECT_GCC_OPTIONS='-v' '-I' '/usr/include/opencv' '-o' 'cv_version' '-mtune=generic' '-march=x86-64'
as -v -I /usr/include/opencv --64 -o /tmp/ccz4qy9b.o /tmp/cc1aYSh7.s
GNU assembler version 2.24 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.24
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-I' '/usr/include/opencv' '-o' 'cv_version' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro -o cv_version /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so /usr/lib/x86_64-linux-gnu/libopencv_contrib.so /usr/lib/x86_64-linux-gnu/libopencv_core.so /usr/lib/x86_64-linux-gnu/libopencv_features2d.so /usr/lib/x86_64-linux-gnu/libopencv_flann.so /usr/lib/x86_64-linux-gnu/libopencv_gpu.so /usr/lib/x86_64-linux-gnu/libopencv_highgui.so /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so /usr/lib/x86_64-linux-gnu/libopencv_legacy.so /usr/lib/x86_64-linux-gnu/libopencv_ml.so /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so /usr/lib/x86_64-linux-gnu/libopencv_ocl.so /usr/lib/x86_64-linux-gnu/libopencv_photo.so /usr/lib/x86_64-linux-gnu/libopencv_stitching.so /usr/lib/x86_64-linux-gnu/libopencv_superres.so /usr/lib/x86_64-linux-gnu/libopencv_ts.so /usr/lib/x86_64-linux-gnu/libopencv_video.so /usr/lib/x86_64-linux-gnu/libopencv_videostab.so -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lm /tmp/ccz4qy9b.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o
/tmp/ccz4qy9b.o: In function `cvDecRefData':
cv_version.c:(.text+0xa6a): undefined reference to `cvFree_'
cv_version.c:(.text+0xaf2): undefined reference to `cvFree_'
/tmp/ccz4qy9b.o: In function `cvGetRow':
cv_version.c:(.text+0xc08): undefined reference to `cvGetRows'
/tmp/ccz4qy9b.o: In function `cvGetCol':
cv_version.c:(.text+0xc36): undefined reference to `cvGetCols'
/tmp/ccz4qy9b.o: In function `cvReleaseMatND':
cv_version.c:(.text+0xc50): undefined reference to `cvReleaseMat'
/tmp/ccz4qy9b.o: In function `cvSubS':
cv_version.c:(.text+0xdac): undefined reference to `cvAddS'
/tmp/ccz4qy9b.o: In function `cvCloneSeq':
cv_version.c:(.text+0xde5): undefined reference to `cvSeqSlice'
/tmp/ccz4qy9b.o: In function `cvSetNew':
cv_version.c:(.text+0xe52): undefined reference to `cvSetAdd'
/tmp/ccz4qy9b.o: In function `cvGetSetElem':
cv_version.c:(.text+0xefa): undefined reference to `cvGetSeqElem'
/tmp/ccz4qy9b.o: In function `cvEllipseBox':
cv_version.c:(.text+0xfe6): undefined reference to `cvEllipse'
/tmp/ccz4qy9b.o: In function `cvFont':
cv_version.c:(.text+0x1030): undefined reference to `cvInitFont'
/tmp/ccz4qy9b.o: In function `cvReadIntByName':
cv_version.c:(.text+0x1126): undefined reference to `cvGetFileNodeByName'
/tmp/ccz4qy9b.o: In function `cvReadRealByName':
cv_version.c:(.text+0x11d0): undefined reference to `cvGetFileNodeByName'
/tmp/ccz4qy9b.o: In function `cvReadStringByName':
cv_version.c:(.text+0x1263): undefined reference to `cvGetFileNodeByName'
/tmp/ccz4qy9b.o: In function `cvReadByName':
cv_version.c:(.text+0x12a3): undefined reference to `cvGetFileNodeByName'
cv_version.c:(.text+0x12b9): undefined reference to `cvRead'
/tmp/ccz4qy9b.o: In function `cvContourPerimeter':
cv_version.c:(.text+0x12ed): undefined reference to `cvArcLength'
/tmp/ccz4qy9b.o: In function `cvCalcHist':
cv_version.c:(.text+0x132f): undefined reference to `cvCalcArrHist'
collect2: error: ld returned 1 exit status
rose@lynx-ubuntu:/home/rose/Txt/projects/OpenCV/Examples(33)$ cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.3 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.3 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
答案1
不,它没有损坏:你只需要注意库链接顺序。特别是,由于翻译单元中的对象依赖于 OpenCV 库中的对象,因此它必须在命令行中cv_version.c
位于前面,例如pkg-config --libs opencv
gcc -v `pkg-config --cflags opencv` cv_version.c `pkg-config --libs opencv` -lm -ocv_version
gcc 手册页链接选项明确提到了这一点:
-llibrary
-l 库链接时搜索名为 library 的库。(第二种将库作为单独参数的替代方法仅用于符合 POSIX 规范,不推荐使用。)
在命令中写入此选项的位置有所不同;链接器会按照指定的顺序搜索和处理库和目标文件。因此,“foo.o -lz bar.o”会在文件 foo.o 之后但在 bar.o 之前搜索库“z”。如果 bar.o 引用“z”中的函数,则可能不会加载这些函数。