creating dynamic libraries

Sam Steingold sds@gnu.org
Fri Sep 25 21:10:00 GMT 2009


Hi,
How do I create a dynamic library which uses data in the executable which uses it?

specifically, I have this main program:

=================== main.c
#include <stdio.h>
extern int shared_func (void);
int my_int_var = 42;
int my_int_addr = &my_int_var;

int main (void) {
   printf("[%d]\n",shared_func());
   return 0;
}
=================== main.c

and this shared library:

=================== shared.c
extern int my_int_var;
int shared_func (void) { return my_int_var; }
=================== shared.c

this is what I get on linux:

$ gcc -fPIC -shared -o shared.so shared.c
$ gcc -o main main.c shared.so
$ LD_LIBRARY_PATH=. ./main
[42]

however when I try the same trick on cygwin, I get this:

$ gcc -shared -o shared.dll shared.c
/.../6/cc1e5Kdk.o:shared.c:(.text+0x4): undefined reference to `_my_int_var'

so, what do I do?

I suppose I could extract my_int_var into a separate shared library, declare it 
there as __declspec(dllexport), then use it as __declspec(dllimport) in shared.c.
alas, this means that my_int_addr fails with
"error: initializer element is not constant".

so, is there a way to create a dll which would use data in the executable it 
will be linked against?

thanks.
Sam.


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list