home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / bx75p3.zip / doc / building-reloc-dlls.txt < prev    next >
Text File  |  1999-02-23  |  4KB  |  144 lines

  1. To: gnu-win32@cygnus.com
  2. From: John Cerney <j-cerney1@ti.com>
  3. Subject: Building a relocatable DLL - example
  4. Date: Wed, 5 Mar 1997 09:01:55 -0800
  5.  
  6. The cygwin32 FAQ and release notes tell you how to build a simple
  7. non-relocatable DLL, and how to build relocatable executable, but not how to
  8. build a relocatable DLL.
  9.  
  10. Here is a simple example of building a relocatable DLL. This method of building
  11. DLLs is the same method used in building the cygwin.dll file. See the makefile
  12. (Makefile.in) in the winsup directory of the cygwin32 source distribution for
  13. more details.
  14.  
  15. Source Files:
  16. ==> main.c <============================================
  17. // Main file to try linking with a DLL under gnuwin32
  18.  
  19.  
  20. int
  21. main()
  22. {
  23.         printf("doit(5) returns %d\n", doit(5));
  24.         printf("doittoo(5) returns %d\n", doittoo(5));
  25. }
  26. ==> foo.c <============================================
  27. // Test file to check out building DLLs with gnuwin32
  28. //  This uses printf from the std lib
  29.  
  30. #include <stdio.h>
  31.  
  32.  
  33. int
  34. doit (int i)
  35. {
  36.      printf("In foo.c inside of doit\n");
  37.      return( doittoo(i) );
  38. }
  39.  
  40. ==> foo2.c <============================================
  41. // Test file to check out building DLLs with gnuwin32
  42. //  This uses printf from the std lib
  43.  
  44. #include <stdio.h>
  45.  
  46.  
  47.  
  48. int
  49. doittoo(int i)
  50. {
  51.       printf("In foo2.c inside of doittoo\n");
  52.       return(i+10);
  53. }
  54.  
  55. ==> init.cc <=============================================
  56. /* init.cc for WIN32.
  57.  
  58.    Copyright 1996 Cygnus Solutions
  59.  
  60. This program is free software; you can redistribute it and/or modify
  61. it under the terms of the GNU General Public License as published by
  62. the Free Software Foundation; either version 2 of the License, or
  63. (at your option) any later version.
  64.  
  65. This program is distributed in the hope that it will be useful,
  66. but WITHOUT ANY WARRANTY; without even the implied warranty of
  67. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  68. GNU General Public License for more details.
  69.  
  70. You should have received a copy of the GNU General Public License
  71. along with this program; if not, write to the Free Software
  72. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  73.  
  74. #include <windows.h>
  75.  
  76. extern "C"
  77. {
  78.   int WINAPI dll_entry (HANDLE h, DWORD reason, void *ptr);
  79. };
  80.  
  81. int WINAPI dll_entry (HANDLE ,
  82.                      DWORD reason,
  83.                      void *)
  84. {
  85.   switch (reason)
  86.     {
  87.     case DLL_PROCESS_ATTACH:
  88.       break;
  89.     case DLL_PROCESS_DETACH:
  90.       break;
  91.     case DLL_THREAD_ATTACH:
  92.       break;
  93.     case DLL_THREAD_DETACH:
  94.       break;
  95.     }
  96.   return 1;
  97. }
  98.  
  99. ==> fixup.c <=========================================================
  100. /* This is needed to terminate the list of inport stuff */
  101. /* Copied from winsup/dcrt0.cc in the cygwin32 source distribution. */
  102.         asm(".section .idata$3\n" ".long 0,0,0,0, 0,0,0,0");
  103. -------------------- snip (start of build script)-----------------------------
  104. #! /bin/sh
  105. #  Example Script to compile and link a relocatable DLL
  106. #    Files that make up the DLL = foo.c foo2.c init.cc fixup.c.
  107. #        (init.cc and fixup.c are housekeeping routines needed for the DLL. The
  108. actual
  109. #                      library routines are in foo.c and foo2.c)
  110. # ***Fill in your path to libcygwin.a here (with no trailing slash)***
  111. LIBPATH=/gnuwin32/H-i386-cygwin32/i386-cygwin32/lib
  112.  
  113. # Compile source files:
  114. gcc -c foo.c
  115. gcc -c foo2.c
  116. gcc -c init.cc
  117. gcc -c fixup.c
  118.  
  119. # Make .def file:
  120. echo EXPORTS > fooB.def
  121. nm foo.o foo2.o init.o fixup.o | grep '^........ [T] _' | sed 's/[^_]*_//' >>
  122. fooB.def
  123.  
  124. # Link DLL.
  125. ld --base-file fooB.base --dll -o fooB.dll foo.o foo2.o init.o fixup.o \
  126.  $LIBPATH/libcygwin.a -e _dll_entry@12
  127. dlltool --as=as --dllname fooB.dll --def fooB.def --base-file fooB.base
  128. --output-exp fooB.exp
  129. ld --base-file fooB.base fooB.exp --dll -o fooB.dll foo.o foo2.o init.o fixup.o
  130. \
  131.   $LIBPATH/libcygwin.a -e _dll_entry@12
  132. dlltool --as=as --dllname fooB.dll --def fooB.def --base-file fooB.base
  133. --output-exp fooB.exp
  134. ld fooB.exp --dll -o fooB.dll foo.o foo2.o init.o fixup.o\
  135.  $LIBPATH/libcygwin.a -e _dll_entry@12
  136.  
  137. # Build the fooB.a lib to link to:
  138. dlltool --as=as --dllname fooB.dll --def fooB.def --output-lib fooB.a
  139.  
  140. # Linking with main
  141. gcc main.c fooB.a -o main.exe
  142.  
  143.  
  144.