static libs imported into DSO?

Kent Watsen kent@watsen.net
Mon Jan 13 03:30:00 GMT 2003


Hi all,

I have simple application that I want to link a
plugin into.  The intent is for the plugin the
use the same libraries linked into the main app,
but instead the plugin is linking in its own copy!
This code works as expected using gcc on OpenBSD.
What am I doing wrong?  Can I tell the linker to
ignore unresolved symbols and rld will be ok?


$ uname -a
CYGWIN_NT-5.1 1.3.10(0.51/3/2) 2002-02-25 i686

$ gcc -v
gcc version 2.95.3-5 (cygwin special)


staticlib.h
-----------
void setVal(int val);
int  getVal(void);


staticlib.c
-----------
static int VAL = -1;
void setVal(int val) { VAL = val; }
int  getVal(void) { return VAL; }


dynamiclib.c
------------
#include "staticlib.h"
void foo(void) { printf("val = %d\n", getVal()); }


main.c
------
#include "staticlib.h"
typedef void Func(void);

int main(void) {
   int   dl;
   Func* func;
   int   result;

   setVal(5);
   printf("val = %d\n", getVal());

   dl = dlopen("./dynamiclib.so");
   if (dl == 0) {
     printf("dlopen failed\n");
     return 1;
   }

   func = (Func*)dlsym(dl, "foo");
   if (func == 0) {
     printf("dlsym failed\n");
     return 1;
   }

   func();

   result = dlclose(dl);
   if (result != 0) {
     printf("dlclose failed\n");
     return 1;
   }

   return 0;
}



$ gcc -c staticlib.c
$ gcc main.c staticlib.o
$ gcc -shared dynamiclib.c staticlib.o -o dynamiclib.so
                            ^^^^^^^^^^^
           else undefined ref ---^


$ ./a.exe
val = 5
val = -1    <---- THIS SHOULD HAVE BEEN 5!













More information about the Cygwin mailing list