static lib works fine, but why not the shared lib version

static lib works fine, but why not the shared lib version

Post by M.L. Ton » Mon, 12 Mar 2001 03:15:19



Hello,

I'm using Linux and gcc.

I've created a static library, say libx.a, containing the following
file,

/* errorlog.c begins */
...
extern int debug;
...
/* errorlog.c ends */

And there is some other file, say afile.c, that has nothing to do with
the 'debug' variable.

When I 'gcc afile.c libx.a', it does compile and works fine.

But now I turn the library into a shared library, say libx.so.1.0,
and when I 'gcc afile.c libx.so.1.0', the linker complains,

libx.so.1.0: undefined reference to `debug'
collect2: ld returned 1 exit status

How do I solve this?

Thanks.

 
 
 

static lib works fine, but why not the shared lib version

Post by M?ns Rullg? » Tue, 13 Mar 2001 17:32:17



> Hello,

> I'm using Linux and gcc.

> I've created a static library, say libx.a, containing the following
> file,

> /* errorlog.c begins */
> ...
> extern int debug;
> ...
> /* errorlog.c ends */

> And there is some other file, say afile.c, that has nothing to do with
> the 'debug' variable.

> When I 'gcc afile.c libx.a', it does compile and works fine.

> But now I turn the library into a shared library, say libx.so.1.0,
> and when I 'gcc afile.c libx.so.1.0', the linker complains,

> libx.so.1.0: undefined reference to `debug'
> collect2: ld returned 1 exit status

> How do I solve this?

> Thanks.

The problem is the keyword extern. 'extern' means that the variable
'debug' is defined in some other file, to be linked with later. Also,
when linking with libraries you should use 'gcc -l<library>'.

--
M?ns Rullg?rd


 
 
 

1. Build shared lib on Solaris with exsiting static libs

I'm using CC on Solaris to build a shared lib (lib*.so).
I know I need to use -G when I link obj files to produce
a shared lib. To ld with obj files with -G, obj files need
to be compiled with the -pic option. But I also need to
link with third-party static libs ( *.a files). Will this cause
link to fail if third-party obj files are compiled without the
-pic option. If yes, how do I get around this? Or is their
another way to build a shared lib without the -G option?

You help will be highly appreicated.

Thanks in advance.
Jun Wu

2. 3COM Boomerang 3c905 with 2.1.43?

3. Utility To Determine Static Libs In An Application (ldd for static libs)

4. Is rshd not allowed to root?

5. C++ linking problem creating a static lib from another static lib.

6. IP subnetting : school project

7. Help - shared libs lib****.so.4 lib***.so.5

8. Question for Marcelo

9. /usr/lib/gcc-lib/i386-linux and /usr/lib/gcc-lib/i486-linux

10. Linux kernel linked against shared libs? Why not?

11. convert shared lib to archive lib ?

12. HELP:::-> shared lib and archived lib

13. Shared libs in /home/ftp/lib. How?