help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Win32 Exporting API functions


From: Guy Harrison
Subject: Re: Win32 Exporting API functions
Date: Mon, 09 Aug 2004 23:00:20 GMT
User-agent: KNode/0.7.7

JKop wrote:

> I'm writing a Win32 executable. While I want this program
> to be a ".exe" file that I can execute, I also want to
> export functions (and maybe classes) as does a DLL.
> 
> Could someone please tell me what I have to do to my
> actual code, eg. to export the following function:

It's far too involved to go into here as a lot of it involves talking
(indirectly via gcc/g++) to the linker. However, as gcc appears to be
getting popular on win32 I've slung together this (crude) example so as to
be able to point folk to it later. Basic idea is it builds a static
archive, exports everything within it into the DLL, then builds an app
which it links against the DLL import lib. Rest of comments are to be read
generally.

> int Blah()
> {
> 
> }
> 
> And if possible the following class:
> 
> class Cow
> {
> 
> };
> 
> 
> And also what command line switches do I need to provide
> when compiling it?

Alter CC and CXX accordingly (I'm cross-compiling, you'll likely just delete
those two lines). Preserve the tabs in the makefile...

<makefile>
WA_1    =-Wl,-whole-archive
WA_0    =-Wl,-no-whole-archive

CC      =i586-mingw32msvc-gcc
CXX     =i586-mingw32msvc-g++

INC     =-I.
LIB     =-L. $(LIB_DLL)

LIB_NAM =d
LIB_OBJ =d.o
LIB_ARC =$(LIB_NAM).a
LIB_DLL =$(LIB_NAM).dll

APP_NAM =c
APP_OBJ =c.o
APP_EXE =$(APP_NAM).exe

CFLAGS  =$(INC) $(LIB)
CXXFLAGS=$(INC) $(LIB)

$(APP_EXE):     $(LIB_DLL) $(APP_OBJ)
                $(CXX) -o $(APP_EXE) $(APP_OBJ) $(LIB)

$(LIB_DLL):     $(LIB_ARC)
                $(CXX) -shared \
                -o $(LIB_DLL) $(WA_1) $(LIB_ARC) $(WA_0) \
                -Wl,--out-implib,lib$(LIB_DLL).a

$(LIB_ARC):     $(LIB_OBJ)
                $(AR) rs $(LIB_ARC) $(LIB_OBJ)
</makefile>

<c.cpp>
#include "d.h"
int main()
{
 Blah();
 Cow();
 return 0;
}
</c.cpp>

<d.h>
#ifndef D_H
#define D_H D_H
int Blah(void);
struct Cow {
 Cow();
};
#endif  //D_H
</d.h>

<d.cpp>
#include <iostream>
#include "d.h"
int Blah(void)
{return -1;}
Cow::Cow()
{std::cout<<"moo\n";}
</d.cpp>

Things to note:

We never talk to the linker directly. We do it via "-Wl" which is in the gcc
manual. The options associated with "-Wl" will be found in the linker
manual for your system.

Win32 additionally has two more tools, 'dlltool' and 'dllwrap'. It's enough
to mention them. They have manuals and the gcc native forums such as mingw
and cygwin mailing lists have examples for those prepared to search.

It's possible to create *.DEF files and it's possible to dllimport/dllexport
by hand in similar manner to non-gcc compilers. Re above paragraph & re
win32 opensource projects for liberating hints.

The makefile is *dire* to keep size down. It would be poor two decades ago
(no header dependancies). Gnu make is assumed (eg: nothing sets AR). Thus
"man [g]make" with priority on ".PHONY" targets - a makefile that can't
clean itself up is a debugging nightmare crying out to happen (stuff won't
rebuild when it ought and you don't get told). WA_0/WA_1 are irrelevent
variables, only there so as not to linewrap in posting.

To the OP... as you've guessed further questions are offtopic. However if
you remain stuck, time permitting, I can be reached via "swampdog AT
ntlworld d0t com".



reply via email to

[Prev in Thread] Current Thread [Next in Thread]