ld 无法与主可执行文件链接
在 OSX 10.6.4 上,使用 TextMate 和 Makefile 进行 i686-apple-darwin10-g++-4.2.1 编译,该文件首先是为 Linux 制作的,我正在尝试为 OSX 进行翻译。
编译 C++ 项目时,出现“无法与主可执行文件链接”错误:
g++ -Wall -g -I ~/svnX-Repository/axp-Projekte/xrlfupa/trunk/src/ -I ~/svnX-Repository/boost_1_44_0 -I /opt/local/var/macports/software/boost/1.44.0_0/opt/local/lib/ -I /opt/local/var/macports/software/gsl/1.14_0/opt/local/include/ -o xrfLibTest xrfLibTest.o excitFunctions.o xrfFunctions.o filterFunctions.o detectorFunctions.o -L/opt/local/var/macports/software/boost/1.44.0_0/opt/local/lib/ -L/opt/local/var/macports/software/gsl/1.14_0/opt/local/lib/ -lm -lxrlTUB -lboost_serialization -lgsl -lgslcblas # Debug 1
ld: in /usr/local/lib/libxrlTUB.so, can't link with a main executable
collect2: ld returned 1 exit status
make: *** [prog] Error 1
提到的库 (libxrlTUB.so) 位于其位置 (/usr/local/lib/libxrlTUB.so),但是,可能是这样这就是问题所在,libxrlTUB.so也是我自己事先编译的。 编译过程完成了,它是由 swig 生成的,尽管有一个警告:
g++ -arch x86_64 -m32 -g -fpic -I /usr/include/python2.6 -c PyXrl_wrap.cxx
In function 'void SWIG_Python_AddErrorMsg(const char*)':
warning: format not a string literal and no format arguments
据我所知,这不应该是一个问题。 (或者是吗?)
不幸的是,这整件事是大学项目的一部分。实际上,我应该用 python 编写一个 X 射线分析脚本,这很好,如果……好吧,如果我不希望使用这个 c++ 项目产生的库。 (之后它们应该通过 python 中的导入来使用。)
我对 c++ 并没有真正的经验,也没有在 OSX 系统上进行编译。到目前为止,我一直在为 scipting(python、bash 等)而烦恼。所以也许我只是错过了一些简单的东西。希望有人能给我一个提示,我可以在哪里继续阅读,以处理上述“无法与主可执行文件链接”错误......
提前感谢, 利亚姆
On OSX 10.6.4 with i686-apple-darwin10-g++-4.2.1 compiling using TextMate and a Makefile which in the first place has been made für a Linux and I am trying to translate for OSX.
When compiling a c++ project I get the "can't link with a main executable" error:
g++ -Wall -g -I ~/svnX-Repository/axp-Projekte/xrlfupa/trunk/src/ -I ~/svnX-Repository/boost_1_44_0 -I /opt/local/var/macports/software/boost/1.44.0_0/opt/local/lib/ -I /opt/local/var/macports/software/gsl/1.14_0/opt/local/include/ -o xrfLibTest xrfLibTest.o excitFunctions.o xrfFunctions.o filterFunctions.o detectorFunctions.o -L/opt/local/var/macports/software/boost/1.44.0_0/opt/local/lib/ -L/opt/local/var/macports/software/gsl/1.14_0/opt/local/lib/ -lm -lxrlTUB -lboost_serialization -lgsl -lgslcblas # Debug 1
ld: in /usr/local/lib/libxrlTUB.so, can't link with a main executable
collect2: ld returned 1 exit status
make: *** [prog] Error 1
The library that is mentioned (libxrlTUB.so) is in its place (/usr/local/lib/libxrlTUB.so) but, possibly that is where the problem came from, the libxrlTUB.so has been compiled by myself beforehand as well.
The compile process went through, it was generated by swig, though there was a warning:
g++ -arch x86_64 -m32 -g -fpic -I /usr/include/python2.6 -c PyXrl_wrap.cxx
In function 'void SWIG_Python_AddErrorMsg(const char*)':
warning: format not a string literal and no format arguments
which, as far as I could find out, shouldnt be a problem. (Or is it?)
Unfortunately this whole thing is part of a project from the university. Actually I am supposed to write an X-ray-analysis script in python, which would be fine, if... well if I wouldn't be expected to use the librarys that are meant to result from this c++ project.
(Afterwards they should be used via import in python.)
I am not really experienced with c++, neither with compiling on OSX systems. So far I have been bothering with scipting (python, bash, etc). So Maybe I am just missing something simple. Hopefully someone can give me an hint where I can continue reading in order to deal with the above "can't link with a main executable" error...
Thanx in advance,
Liam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误消息告诉您问题所在,即
/usr/local/lib/libxrlTUB.so
不是共享库;它是一个可执行文件。您无法链接到可执行文件。可能您用于libxrlTUB.so
的任何构建过程都不了解如何在 Mac 上构建共享库(这更可疑,因为.dylib
是要使用的正确扩展名。 )看看苹果的 关于编译动态库的文档。您可以使用
file
来确保输出的类型正确,例如:如果没有
-dynamiclib
,您最终会得到一个可执行文件,这可能是您遇到的问题遇到。The error message is telling you the problem—it is that
/usr/local/lib/libxrlTUB.so
is not a shared library; it's an executable. You can't link against an executable. Probably whatever build process you used forlibxrlTUB.so
didn't understand how to build shared libraries on the Mac (it's more suspect because.dylib
is the correct extension to use.)Take a look at Apple's documentation on compiling dynamic libraries. You can use
file
to make sure your output is of the correct type, for example:Without
-dynamiclib
you end up with an executable, which may be the problem you've run into.