| www.delorie.com/archives/browse.cgi | search |
| X-Recipient: | archive-cygwin AT delorie DOT com |
| X-SWARE-Spam-Status: | No, hits=0.1 required=5.0 tests=BAYES_00,J_CHICKENPOX_62,RCVD_NUMERIC_HELO,SPF_HELO_PASS,SPF_PASS |
| X-Spam-Check-By: | sourceware.org |
| To: | cygwin AT cygwin DOT com |
| From: | Sam Steingold <sds AT gnu DOT org> |
| Subject: | creating dynamic libraries |
| Date: | Fri, 25 Sep 2009 17:09:43 -0400 |
| Lines: | 48 |
| Message-ID: | <4ABD3197.1090509@gnu.org> |
| Mime-Version: | 1.0 |
| User-Agent: | Thunderbird 2.0.0.22 (X11/20090625) |
| X-IsSubscribed: | yes |
| Mailing-List: | contact cygwin-help AT cygwin DOT com; run by ezmlm |
| List-Id: | <cygwin.cygwin.com> |
| List-Subscribe: | <mailto:cygwin-subscribe AT cygwin DOT com> |
| List-Archive: | <http://sourceware.org/ml/cygwin/> |
| List-Post: | <mailto:cygwin AT cygwin DOT com> |
| List-Help: | <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs> |
| Sender: | cygwin-owner AT cygwin DOT com |
| Mail-Followup-To: | cygwin AT cygwin DOT com |
| Delivered-To: | mailing list cygwin AT cygwin DOT com |
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
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |