What is wrong with this dll import code?


C

Chavoux

Hi When I try to compile the following code I get an error:
[Linker error: undefined reference to `[email protected]'
....
<code>
#include <cstdlib>
#include <iostream>
#include "sDriverDLL.h"

using namespace std;

char * EXTERN InttoStr(int n, char * buff, int radix);
#ifdef __cplusplus
extern "C"
{
#endif //*/
int EXTERN CreatePort(char * PrtName);
int EXTERN OpenPort();
int EXTERN ReadaPacket(char * Packet);
int EXTERN ReadaTag(char * TagStr);
int EXTERN ClosePort();
int EXTERN FreePort();
#ifdef __cplusplus
}
#endif
//*/
int main(int argc, char *argv[])
{
cout << "starting... Creating COMPORT1";
int x = CreatePort("COM1");
}
</code>

I'm using Dev-C++ (minGW compiler) on Windows XP.

In the "sDriverDLL.h" file EXTERN is is declared as follows:
<code>
#if BUILDING_DLL
#define EXTERN __declspec (dllexport) __stdcall
#else /* Not BUILDING_DLL */
#define EXTERN __declspec (dllimport) __stdcall
#endif /* Not BUILDING_DLL */
</code>

The header file also includes the whole section:
<code>
#ifdef __cplusplus
extern "C"
{
#endif //*/
int EXTERN CreatePort(char * PrtName);
.....
#ifdef __cplusplus
}
#endif
</code>

I can actually import the .dll file into Delphi (!), but can't seem to
import it into C++ itself. What am I missing?

Thanks
Chavoux
 
Ad

Advertisements

C

Chavoux

To clarify my problem...
As I understand it, C++ mangles the function names when compiling
(i.e. "CreatePort" becomes "[email protected]"). Declaring the
functions in a
"#ifdef __cplusplus
extern "C"
{
#endif //*/
....
#ifdef __cplusplus
}
#endif"
block, should force the compiler to export it as "C" functions, without
any name mangling.

The linker cannot find the mangled C++ names in the .dll because it
doesn't exist in the .dll (because of the above code block).

However, I redeclare the functions locally in a similar "C" block, but
the linker still looks for the mangled C++ function names. How do I get
it not to do this?

Any ideas?
Chavoux
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top