在ubuntu13.04上,我使用linux发行版提供的GCC-4.7.3从共享库构建可执行文件时出错。
我猜问题在libpng和zlib之间(前者使用后者),但我不知道为什么。
首先,我的命令是:
$ gfortran -o test_muesli_config_fml test_muesli_config_fml.o -fopenmp
-Wl,--rpath,/usr/local/lib/muesli /usr/local/lib/muesli/libfml.so -lstdc++
-Wl,--rpath,/usr/lib /usr/lib/liblapack.so -Wl,--rpath,/usr/lib /usr/lib/libblas.so
-lpng -lz -lpthread -lreadline -lhistory这将产生以下错误:
/usr/local/lib/muesli/libfml.so: undefined reference to `gzwrite'
/usr/local/lib/muesli/libfml.so: undefined reference to `gzopen'
/usr/local/lib/muesli/libfml.so: undefined reference to `gzclose'
/usr/local/lib/muesli/libfml.so: undefined reference to `gzread'
collect2: error: ld returned 1 exit status但是请注意,-lz是存在的。之后,我添加了链接器选项--trace-symbol=,以获得更多信息:
$ gfortran -o test_muesli_config_fml test_muesli_config_fml.o -fopenmp
-Wl,--rpath,/usr/local/lib/muesli /usr/local/lib/muesli/libfml.so -lstdc++
-Wl,--rpath,/usr/lib /usr/lib/liblapack.so -Wl,--rpath,/usr/lib /usr/lib/libblas.so
-lpng -lz -lpthread -lreadline -lhistory -Wl,--trace-symbol=gzwrite这反过来又给出了结果:
/usr/local/lib/muesli/libfml.so: reference to gzwrite
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libz.so: definition of gzwrite
/usr/local/lib/muesli/libfml.so: undefined reference to `gzwrite'
/usr/local/lib/muesli/libfml.so: undefined reference to `gzopen'
/usr/local/lib/muesli/libfml.so: undefined reference to `gzclose'
/usr/local/lib/muesli/libfml.so: undefined reference to `gzread'
collect2: error: ld returned 1 exit status所以,gzwrite是在libz.so中找到的,但是链接器不使用它!
偶然地,我想删除-lpng选项(实际上,没有使用libpng库),我的问题就解决了!为什么?
其次,我用GCC的另一个版本编译了我的整个代码-- 4.7.3(由我自己编译--我习惯于测试编译器的许多版本),而且错误没有发生,甚至使用-lpng和-lz!
有什么想法吗?
此外,与另一个程序(使用libpng)进行不同的尝试会导致成功构建。
编辑2013-10-08
现在我很确定它是Ubuntu-13.04中的一个bug :我尝试了另外两个linux发行版(Fedora 16 -- Ubuntu-10.04),链接器的行为是标准的,这与我在消息的第一部分中描述的不一样。
我计划在ubuntu社区报告这个问题。致以问候。
编辑2013-10-09
该bug已向https://bugs.launchpad.net/ubuntu/+source/gcc-defaults/+bug/1237270报告。
发布于 2013-11-21 12:08:07
有两种可能的修复方法(直到ubuntu不自行修复):
libpng.a和libz.a编译为静态库(它只能是一个临时解决方案,因为静态库在大多数情况下都是有害的)。libz.a编译为静态的。https://stackoverflow.com/questions/19211585
复制相似问题