home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / os / os2 / programm / 6266 < prev    next >
Encoding:
Text File  |  1992-11-08  |  3.3 KB  |  74 lines

  1. Newsgroups: comp.os.os2.programmer
  2. Path: sparky!uunet!cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!stanford.edu!kronos.arc.nasa.gov!iscnvx!netcomsv!netcom.com!ljensen
  3. From: ljensen@netcom.com (Colin Jensen)
  4. Subject: Re: Creating a DLL with GCC 2.2.2
  5. Message-ID: <1992Nov7.050943.18992@netcom.com>
  6. Organization: Netcom - Online Communication Services (408 241-9760 guest)
  7. References: <Rg1RTB1w165w@dat1hb.north.de>
  8. Date: Sat, 7 Nov 1992 05:09:43 GMT
  9. Lines: 63
  10.  
  11. In article <Rg1RTB1w165w@dat1hb.north.de> lion@dat1hb.north.de (Daniel Tietze) writes:
  12. >We are currently trying to persuade GCC 2.2.2 (and LINK386, of course) to
  13. >produce a DLL (for experimentation reasons with just one function:
  14. >DebugMsg() ).
  15. >We set up a DEF-File asking LINK386 to EXPORT the function, but this does
  16. >not happen. GCC compiles the stuff OK, but LINK386 says:
  17. >     function DebugMsg (alias DebugMSG) : undefined export
  18. >(or something to that effect).
  19. >Now, my question is:
  20. >Given a source module OURDLL.CC and a definition file OURDLL.DEF,
  21. >with a routine "void DebugMsg()" in OURDLL.CC, how do we combine this stuff
  22. >into a DLL with the tools already mentioned?
  23. >AND: How do we import it into, say DLLTEST.CC?
  24.  
  25. I have not made any DLLs with gcc.  But let me add my two cents anyways ;-)
  26.  
  27. In C++ (as your .cc file implies), function names are mangled greatly
  28. before linking.  Basically both the function name, the return type, and
  29. all of the parameter types are encoded into a single name for the function.
  30. The advantage of this name-mangling is that if you define a function
  31. in one file, but mis-declare the function in another file that you use
  32. the function from, you will be prevented from linking your incorrect
  33. program.
  34.  
  35. The problem is that the .DEF file expects that you are able to name
  36. your function - but you don't know what the mangled name is.
  37.  
  38. The way around this potential fiasco is to use a gcc extention called
  39. "asm" to tell gcc *exactly* what what you want the function to be named.
  40. For a function declaration like
  41.     int *foo(int one, long two);
  42. you can write
  43.     int *foo(int one, long two) asm("foo");
  44. to demand that gcc select the name "foo" for the name of the function
  45. to be put into the object file.
  46.  
  47. Similarly, for a function definition like
  48.     int *foo(int one, long two) { return NULL; }
  49. you can write
  50.     int *foo(int one, long two) asm("foo") { return NULL; }
  51.  
  52. So at the very least, you will have to use the asm keyword in both
  53. the DLL source code (for the definition of your function), and
  54. in the source code for the EXE that uses your DLL (for the declaration
  55. of the function).
  56.  
  57. Note that although I have mentioned only the gnu C++ compiler, the gnu
  58. C compiler has a similar problem in that it insists on putting an
  59. underscore in front of all C symbol names (a good UNIX tradition).
  60. You can either again use the asm keyword to rename C symbols to
  61. remove the underscore (as is done in your os2-include directory that
  62. comes with gcc/2 so go look there for examples), or you can simply
  63. add the underscore into your DEF files and live with it.
  64.  
  65. I think that if you use EMX and use the OMF output option, emx/gcc
  66. will automatically remove the leading underscores.  You will still
  67. need to use asm for C++ source though. [I don't use EMX myself,
  68. so I may be off]
  69.  
  70. Good luck!
  71. -- 
  72. Colin Jensen
  73. ljensen@netcom.netcom.com    cjensen@ampex.com
  74.