Could someone familiar with command line compiling using GNU C/C++ Compiler Collection please help me with a Dll creation problem I have? What I would like to know is how to create a Windows Dll out of the following very simple test file that is essentially just a "Hello, World!" type thing...
//dllMain.cpp
#include <stdio.h>
void __stdcall Print(const char* pTxt)
{
printf("%s\n", pTxt);
}
As you can see there is just one function which just outputs a char* to the console. The main wrinkles that have so far foiled all my attempts at this are that the function is defined as __stdcall not __cdecl, and I wish to use a Windows Module Definition File, i.e., a *.def file to indicate the exports to the linker - not __declspec(dllexport) or anything like that. Here is the def file. Lets call the project MkDll so that the module definition file is MkDll.def....
; MkDll.def
LIBRARY "MkDll.dll"
DESCRIPTION 'SERVER Windows Dynamic Link Library'
EXPORTS
Print PRIVATE
Here is what I have accomplished so far. My project directory is C:\Code\CodeBlks\MkDll...
C:\Code\CodeBlks\MkDll>g++ -c dllMain.cpp -o dllMain.o
C:\Code\CodeBlks\MkDll>dir
Volume in drive C is Main
Volume Serial Number is 0C18-9CF6
Directory of C:\Code\CodeBlks\MkDll
02/28/2011 10:13 AM <DIR> .
02/28/2011 10:13 AM <DIR> ..
02/28/2011 10:10 AM 92 dllMain.cpp
02/28/2011 10:13 AM 391 dllMain.o
02/28/2011 10:07 AM 124 MkDll.bat
02/28/2011 10:10 AM 148 MkDll.def
4 File(s) 755 bytes
2 Dir(s) 36,273,405,952 bytes free
C:\Code\CodeBlks\MkDll>
As can be seen above I have successfully compiled dllMain.cpp into dllMain.o From my research on the internet I have determined that my next critical step is to use DllTool.exe to read in my MkDll.def file and my dllMain.o object file and create an exports file and lib file. Below you can see my command line invocation of DllTool which I ran in verbose mode, followed by a dir listing...
C:\Code\CodeBlks\MkDll>DllTool -v -d MkDll.def -e expMkDll.o -l libMkDll.lib dllMain.o
DllTool: Processing def file: MkDll.def
DllTool: LIBRARY: MkDll.dll base: ffffffff
DllTool: Processed def file
DllTool: Scanning object file dllMain.o
DllTool: Done reading dllMain.o
DllTool: Processing definitions
DllTool: Processed definitions
DllTool: Generating export file: expMkDll.o
DllTool: Opened temporary file: dquc.s
DllTool: run: as -o expMkDll.o dquc.s
DllTool: Generated exports file
DllTool: Creating library file: libMkDll.lib
DllTool: run: as -o dquh.o dquh.s
DllTool: run: as -o dqut.o dqut.s
DllTool: Created lib file
C:\Code\CodeBlks\MkDll>dir
Volume in drive C is Main
Volume Serial Number is 0C18-9CF6
Directory of C:\Code\CodeBlks\MkDll
02/28/2011 10:25 AM <DIR> .
02/28/2011 10:25 AM <DIR> ..
02/28/2011 10:14 AM 605 CmdLnOutput.txt
02/28/2011 10:10 AM 92 dllMain.cpp
02/28/2011 10:13 AM 391 dllMain.o
02/28/2011 10:25 AM 716 expMkDll.o
02/28/2011 10:25 AM 1,510 libMkDll.lib
02/28/2011 10:07 AM 124 MkDll.bat
02/28/2011 10:19 AM 148 MkDll.def
7 File(s) 3,586 bytes
2 Dir(s) 36,273,344,512 bytes free
C:\Code\CodeBlks\MkDll>
There do not appear to be any errors, and the dir listing seems to indicate my export lib and main library file have been created. The final step should be to run the linker and create my Dll, but that is where I'm getting stuck. I can't seem to find much in the way of documentation on running the ld linker directly, and most documentation shows calling the linker through gcc or g++. Here is my attempt...
g++ -shared -v dllMain.o expMkDll.o -o MkDll.dll
C:\Code\CodeBlks\MkDll>g++ -shared dllMain.o expMkDll.o -o MkDll.dll
expMkDll.o:fake:(.edata+0x38): undefined reference to `Print'
collect2: ld returned 1 exit status
C:\Code\CodeBlks\MkDll>
As can be seen its failing, and I don't know what else to do. At this point, while I'd be thrilled if someone
could help me with just this, I'd like to briefly mention why I'm doing it, because as you might have guessed, its part of a larger problem. I'm trying to use the GNU C/C++ compiler Collection to create a working COM Dll. I'm not sure if anyone has ever even tried to do this. Creating a COM Dll with Microsoft's IDEs or command line compiling is of course very easy. All one needs to do to ensure that Dll export names will remain unmangled through the compile/linkage process is add the *.def file to the compilation string, or, in the case of just using the IDE, include it in the project files as part of the project and the Microsoft compilers will respect the contents of the def file.
I've unsuccessfully tried to use the Code::Blocks Ide, and now I'm trying command line compilation methods to accomplish this. Nothing I have tried works. My exports get continually mangled, and any system calls to the four critical COM exports DllRegisterServer(), DllUnregisterServer(), DllGetClassObject(), and DllCanUnloadNow() fail. COM's Service Control Manager ( SCM ) can successfully do a LoadLibrary() on the COM Dll, but can't locate the functions with GetProcAddress() due to GNU not respecting the export names in my def file. No one at the Code::Blocks Forums have helped me, and the MinGw site has been down for days.
So can anyone help with my simple Dll above? If you can, I'm pretty sure I can manage the rest.