delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/12/31/07:24:58

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/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
Message-ID: <20031231122438.80631.qmail@web60808.mail.yahoo.com>
Date: Wed, 31 Dec 2003 23:24:38 +1100 (EST)
From: =?iso-8859-1?q?Danny=20Smith?= <danny_r_smith_2001 AT yahoo DOT co DOT nz>
Subject: Re: DLL and external symbols
To: cygwin AT cygwin DOT com
MIME-Version: 1.0

From: Jani Tiainen <jani dot tiainen at keypro dot fi> 
> 
>
--------------------------------------------------------------------------------
> Hi,
> 
> I'm sure that this has been answered several times, but I'll ask again
because couldn't find any solution by myself.
> 
> I would like to build DLL (or any other sort of library) that refers to
external symbols in main application.
> 
> So far I have been able to build non-working constructs. =)
> 
> Let's say that I've two files:
> 
> main.c:
> 
> #include <stdio.h>
> 
> extern void funcInMyLib(int);
> 
> 
> void funcInMain(int i)
> {
> 	printf("funcInMain(%ld)\n", i);
> }
> 
> 
> int main(int argc, char **argv)
> {
> 	funcInMyLib(1);
> 	return 0;
> }
> 
> and second file (library)
> 
> 
> mylib.c:
> extern void funcInMain(int);
> 
> 
> void funcInMyLib(int i)
> {
> 	printf("Calling back...");
> 	funcInMain(i + 1);
> }
> 
> --------
> 
> Now how I can get this working, or is it possible at all?
> 
> With .DEF files I can get things compiled and linked, but even program
crashes at startup, or end up requesting "main.exp.dll".
> 


Make a .def file for the library, with IMPORTS section:

============================
;mylib.def
LIBRARY mylib.dll
EXPORTS
  funcInMyLib
IMPORTS
  main.exe.funcInMain
============================

... and one for the main app:

============================
;main.def
NAME main.exe
EXPORTS
  funcInMain
IMPORTS
  mylib.dll.funcInMyLib
============================

Build the dll.

gcc -shared -o mylib.dll  mylib.def mylib.c


Build the exe

gcc -o main.exe  main.def main.c


It may be useful to create a import lib for the
dll, especially if you are exporting stdcall symbols or plan to reuse the
dll with other app's.

eg:
gcc -shared -o mylib.dll -Wl,--out-implib,libmylib.a  mylib.def mylib.c

gcc -o main.exe  main.def main.c -L. -lmylib


Danny

http://personals.yahoo.com.au - Yahoo! Personals
New people, new possibilities. FREE for a limited time.

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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019