ld can't link with a main executable

2019-07-22 06:00发布

问题:

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

回答1:

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 for libxrlTUB.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:

% gcc -c foo.c
% gcc -dynamiclib foo.o -o foo.dylib
% file foo.dylib
foo.dylib: Mach-O 64-bit dynamically linked shared library x86_64

Without -dynamiclib you end up with an executable, which may be the problem you've run into.