Could you help me better? Which macros? |
Create a completely new header called ie.
export.h and put these macros into export.h header:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// export.h header file
#ifndef MY_EXPORT_H
#define MY_EXPORT_H
// DLL export symbols
#ifdef COMPILE_MY_DLL
#define MY_API __declspec(dllexport)
#elif defined (LINK_MY_DLL)
#define MY_API __declspec(dllimport)
#else // Compile LIB or link LIB
#define MY_API
#endif // COMPILE_MY_DLL
#endif // MY_EXPORT_H
|
Now every header in your DLL project from which you want to export functions, structs, classes etc., first include this header, for example:
1 2 3 4
|
//user_force.h
#include export.h
// other header includes go here ...
|
After this step mark all functions that you want to export with
MY_API macro, ex:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
// my_header.h
// this function is exported from DLL, it needs this macro
MY_API void ExportedFunctionName(int a, int b);
// This class is exported from DLL, it needs this macro
class MY_API ClassName
{
// class function, no need for macro here!
void SomeFunction();
// other class declaration...
};
|
You also need to add this macro in cpp file for all functions whose definitions resides in cpp file, except for class functions! ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
// my_source.cpp
#include "my_header.h"
// this function is exported from DLL
// it needs this macro in source file too!
MY_API void ExportedFunctionName(int a, int b)
{
// function implementation goes here
}
// this function is exported from DLL, it does not need macro
// because it's part of a class
vod ClassName::SomeFunction()
{
// function implementation goes here
}
|
Do so for all your headers and sources in your DLL project.
Next step before you compile the DLL, is to define
COMPILE_MY_DLL macro in your project propertis, so that all sources and headers see this macro, you do this in:
Project Properties -> C/C++ -> Preprocessor -> preprocessor definitions
put
COMPILE_MY_DLL into that box and build DLL project.
Now in your other project where you want to link the DLL, you only have to put
LINK_MY_DLL into the same project property field.
and
MY_API
macro will expand to importing a symbol not exporting like in DLL.
Note that using functions in target project does not require any macros, except defining
LINK_MY_DLL
in project properties.
Note1: that if you compile the DLL as static library, you must not define
COMPILE_MY_DLL
in project properties or any other macro must not be defined except those in export.h, and there will be no exports or imports in any case, macros in export.h will handle this as you can see.
Note2: _declspec(dllImport)
is not required, it's optimization feature.
Note3: Do not use
extern "C"
unless your DLL is C code, that is compiled as C instead of C++.
There is much more you should learn about DLL's but this should be enough to get what you want.