delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1997/03/05/12:14:58

From: j-cerney1 AT ti DOT com (John Cerney)
Subject: Building a relocatable DLL - example
5 Mar 1997 12:14:58 -0800 :
Approved: cygnus DOT gnu-win32 AT cygnus DOT com
Distribution: cygnus
Message-ID: <BMSMTP85758005612a0182636.cygnus.gnu-win32@dsbmail.itg.ti.com>
Reply-To: John Cerney <j-cerney1 AT ti DOT com>
Mime-Version: 1.0
X-Mailer: BeyondMail for Windows/Professional 2.3
Original-To: gnu-win32 AT cygnus DOT com
X-BeyondMail-Priority: 1
Conversation-ID: <BMSMTP85758005713a0182636 AT dsbmail DOT itg DOT ti DOT com>
X-Receipt-From-Agent: true
Original-Sender: owner-gnu-win32 AT cygnus DOT com

The cygwin32 FAQ and release notes tell you how to build a simple
non-relocatable DLL, and how to build relocatable executable, but not how to
build a relocatable DLL. 

Here is a simple example of building a relocatable DLL. This method of building
DLLs is the same method used in building the cygwin.dll file. See the makefile
(Makefile.in) in the winsup directory of the cygwin32 source distribution for
more details.

Source Files:
==> main.c <============================================
// Main file to try linking with a DLL under gnuwin32


int
main()
{
        printf("doit(5) returns %d\n", doit(5));
        printf("doittoo(5) returns %d\n", doittoo(5));
}
==> foo.c <============================================
// Test file to check out building DLLs with gnuwin32
//  This uses printf from the std lib

#include <stdio.h>


int
doit (int i)
{
     printf("In foo.c inside of doit\n");
     return( doittoo(i) );
}

==> foo2.c <============================================
// Test file to check out building DLLs with gnuwin32
//  This uses printf from the std lib

#include <stdio.h>



int
doittoo(int i)
{
      printf("In foo2.c inside of doittoo\n");
      return(i+10);
}

==> init.cc <=============================================
/* init.cc for WIN32.

   Copyright 1996 Cygnus Solutions

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include <windows.h> 

extern "C" 
{
  int WINAPI dll_entry (HANDLE h, DWORD reason, void *ptr);
};

int WINAPI dll_entry (HANDLE , 
		     DWORD reason,
		     void *)
{
  switch (reason) 
    {
    case DLL_PROCESS_ATTACH:
      break;
    case DLL_PROCESS_DETACH:
      break;
    case DLL_THREAD_ATTACH:
      break;
    case DLL_THREAD_DETACH:
      break;
    }
  return 1;
}

==> fixup.c <=========================================================
/* This is needed to terminate the list of inport stuff */
/* Copied from winsup/dcrt0.cc in the cygwin32 source distribution. */
	asm(".section .idata$3\n" ".long 0,0,0,0, 0,0,0,0");
-------------------- snip (start of build script)-----------------------------
#! /bin/sh
#  Example Script to compile and link a relocatable DLL
#    Files that make up the DLL = foo.c foo2.c init.cc fixup.c.
#        (init.cc and fixup.c are housekeeping routines needed for the DLL. The
actual
#                      library routines are in foo.c and foo2.c)
# ***Fill in your path to libcygwin.a here (with no trailing slash)***
LIBPATH=/gnuwin32/H-i386-cygwin32/i386-cygwin32/lib

# Compile source files:
gcc -c foo.c
gcc -c foo2.c
gcc -c init.cc
gcc -c fixup.c

# Make .def file:
echo EXPORTS > fooB.def
nm foo.o foo2.o init.o fixup.o | grep '^........ [T] _' | sed 's/[^_]*_//' >>
fooB.def

# Link DLL.
ld --base-file fooB.base --dll -o fooB.dll foo.o foo2.o init.o fixup.o \
 $LIBPATH/libcygwin.a -e _dll_entry AT 12 
dlltool --as=as --dllname fooB.dll --def fooB.def --base-file fooB.base
--output-exp fooB.exp
ld --base-file fooB.base fooB.exp --dll -o fooB.dll foo.o foo2.o init.o fixup.o
\
  $LIBPATH/libcygwin.a -e _dll_entry AT 12 
dlltool --as=as --dllname fooB.dll --def fooB.def --base-file fooB.base
--output-exp fooB.exp
ld fooB.exp --dll -o fooB.dll foo.o foo2.o init.o fixup.o\
 $LIBPATH/libcygwin.a -e _dll_entry AT 12 

# Build the fooB.a lib to link to:
dlltool --as=as --dllname fooB.dll --def fooB.def --output-lib fooB.a

# Linking with main
gcc main.c fooB.a -o main.exe

-
For help on using this list, send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


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