Index:
[thread]
[date]
[subject]
[author]
From: Garrett Mitchener <wgm2@duke.edu>
To :
Date: 04 Mar 1999 20:46:50 -0500
Re: Still having trouble with libraries
Donald Ellis Flood <def5@duke.edu> writes:
> We are still having problems writing a Makefile for libraries. We can
> get both type of libraries to be made but when we try to run our
> executable with the shared library it tells us that it is an "Illegal
> instruction". Whats the deal? With the static library the executable
> runs fine. Does that mean it works? The Makefile can be found at
> ~def5/public. Any help would be appreciated.
>
> -Flash & company
I've talked to this group and figured out the problem:
When you make a shared library, you need to do the following:
* Compile all your library .o files with the -fPIC flag. You do not
need to compile the .o files for your executable with -fPIC,
although it shouldn't hurt. You should not compile with -shared.
The web page says this and it seems to be wrong. (Sorry ola)
* Link the library .o files with ld -G etc. as on the web page.
I've figured out what the -shared flag actually does. It is used only
during the linking phase, and specifies that g++ should output a
shared library rather than an executable file. The group that had
problems was compiling like this:
g++ -o sools -shared sools.o etc. etc. -lscandir
The result was that sools was given executable permissions, but was in
fact a shared library. You can use the file command to get
information about a file, and it says this about shared libraries:
teer12% file /usr/lib/libc.so
/usr/lib/libc.so: ELF 32-bit MSB dynamic lib SPARC Version 1,
dynamically linked, not stripped
When they tried to run their program, the operating system tried to
run the library as an executable and crashed. When we removed the
-shared from the line that created sools, their program worked fine.
Here are some suggestions:
* Put your library source code in a separate directory and use a
separate makefile. They were doing it all in one directory and with
one makefile, so they ended up compiling their sools with the same
flags as their library, which cause the above goof.
* Don't ever compile with the -shared flag. It is properly used as
follows in a linking command to make g++ output a shared library
instead of an executable:
g++ -shared -o libstuff.so spam.o foo.o bar.o
In this usage, -shared should be part of LD_FLAGS, not CFLAGS or
CCFLAGS or CXXFLAGS, etc. The ld command given in the web page will
do the same thing as the above g++ line.
* Do not link sools or any other executable with -shared. In short,
unless you are using something like the above g++ line to create
libscandir.so or some other library, don't use -shared at all.
-- Garrett :-)
Index:
[thread]
[date]
[subject]
[author]