This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Can you use global variables in a shared library?


I am trying to implement my first shared library.  I am running on Solaris
2.6 with:

$ gcc --version
gcc-2.95

$ ld --version 
GNU ld 2.9.1
Copyright 1997 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License.  This program has absolutely no warranty.
  Supported emulations:
   elf32_sparc

I have several .C files which are each compiled with the following command:

g++ -c -fPIC -DPIC gen_random_file_name.C -o gen_random_file_name.o -g 

All of the resulting .o files are linked into a libarary with something
like:

ld -shared -soname liboca.so.3 -o liboca.so.3 liboca.o String.o
gen_random_file_name.o

Which is linked to a generic name for teh SO:

ln liboca.so.3 liboca.so

I then compile a "main()" program into a .o with:

g++ -c oca2.C -o oca2.o -g

And then link the whole works together with:

g++ -g oca2.o -o oca2 -R. -L. -loca static_lib.a -L/usr/local/lib -ltac++
-lsocket++ -lnsl -lsocket -lsocket -lnsl 

But I get the following error messages while trying to link my main object
file with the library:

./liboca.so: undefined reference to `MEMORY_ALLOC_ERROR'

So now for details about MEMORY_ALLOC_ERROR.  MEMORY_ALLOC_ERROR is a
global variable defined in the relevant files as such:

gen_random_file_name.C:extern const String MEMORY_ALLOC_ERROR;
oca2.h:const String MEMORY_ALLOC_ERROR = "Memalloc error.";

and oca2.h is included by oca2.C

So, I guess I am left with the question, can you declare a global variable
(an extern) in a shared library with the expectation that the object that
is linked with the shared library to produce an executable must contain the
actual definition of the global variable?  If so, any idea what I am doing
wrong?

b.



--
Brian J. Murrell                                        Brian_Murrell@bctel.net
BCTel Advanced Communications                                      604 454 5279
Vancouver, B.C.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]