How to use dll export class from MinGW gcc?

A

Allen

In vc2005, I create a dll name ByteBuffer.dll, which export 3 classes,
i.e. ByteBuffer, ReadByteBuffer and WriteByteBuffer. The source code
is somewhat as following.

#ifndef _BYTE_BUFFER_H_
#define _BYTE_BUFFER_H_

class BYTEBUFFER_API ByteBuffer
{
public:

ByteBuffer(void);
virtual ~ByteBuffer(void);

void Wrap(void * pBuffer, const INT32 nLength);
void SetOrder(const INT32 nOrder = BORDER::OrderLitEndian);
...
};

#endif /* _BYTE_BUFFER_H_ */

Now my workmates want to use these 3 classes in MinGW.
For example, there is a test.cpp.

#include "WriteByteBuffer.h"

int main()
{
char buffer[512];
WriteByteBuffer writer;
writer.Wrap(buffer, 512);
writer.Set((INT32)12);
return 0;
}

I do like this:

d:\mingw\bin> pexports ByteBuffer.dll >ByteBuffer.def
d:\mingw\bin> g++ -o test test.cpp -I../include -L./ -lByteBuffer

But link fails with errors:
cciAKM3R.o:test.cpp:(.text+0x82): undefined reference to
`WriteByteBuffer::WriteByteBuffer()'
cciAKM3R.o:test.cpp:(.text+0xac): undefined reference to
`ByteBuffer::Wrap(void*, int)'
cciAKM3R.o:test.cpp:(.text+0xc2): undefined reference to
`WriteByteBuffer::Set(int)'
cciAKM3R.o:test.cpp:(.text+0xda): undefined reference to
`WriteByteBuffer::~WriteByteBuffer()'
cciAKM3R.o:test.cpp:(.text+0x119): undefined reference to
`WriteByteBuffer::~WriteByteBuffer()'
collect2: ld returned 1 exit status

How can I solve the problem? Please help me.
Thank you.

Allen
 
A

Allen

In vc2005, I create a dll name ByteBuffer.dll, which export 3 classes,
i.e. ByteBuffer, ReadByteBuffer and WriteByteBuffer. The source code
is somewhat as following.

#ifndef _BYTE_BUFFER_H_
#define _BYTE_BUFFER_H_

class BYTEBUFFER_API ByteBuffer
{
public:

        ByteBuffer(void);
        virtual ~ByteBuffer(void);

        void Wrap(void * pBuffer, const INT32 nLength);
        void SetOrder(const INT32 nOrder = BORDER::OrderLitEndian);
        ...

};

#endif /* _BYTE_BUFFER_H_ */

Now my workmates want to use these 3 classes in MinGW.
For example, there is a test.cpp.

#include "WriteByteBuffer.h"

int main()
{
        char buffer[512];
        WriteByteBuffer writer;
        writer.Wrap(buffer, 512);
        writer.Set((INT32)12);
        return 0;

}

I do like this:

d:\mingw\bin> pexports ByteBuffer.dll >ByteBuffer.def
d:\mingw\bin> g++ -o test test.cpp -I../include -L./ -lByteBuffer
 
J

jason.cipriani

In vc2005, I create a dll name ByteBuffer.dll, which export 3 classes,
i.e. ByteBuffer, ReadByteBuffer and WriteByteBuffer. The source code
is somewhat as following.
#ifndef _BYTE_BUFFER_H_
#define _BYTE_BUFFER_H_
class BYTEBUFFER_API ByteBuffer
{
public:
        ByteBuffer(void);
        virtual ~ByteBuffer(void);
        void Wrap(void * pBuffer, const INT32 nLength);
        void SetOrder(const INT32 nOrder = BORDER::OrderLitEndian);
        ...

#endif /* _BYTE_BUFFER_H_ */
Now my workmates want to use these 3 classes in MinGW.
For example, there is a test.cpp.
#include "WriteByteBuffer.h"
int main()
{
        char buffer[512];
        WriteByteBuffer writer;
        writer.Wrap(buffer, 512);
        writer.Set((INT32)12);
        return 0;

I do like this:
d:\mingw\bin> pexports ByteBuffer.dll >ByteBuffer.def
d:\mingw\bin> g++ -o test test.cpp -I../include -L./ -lByteBuffer

-------------------------------------------------------------------
Sorry, missing dlltool command
d:\mingw\bin> dlltool --dllname ByteBuffer.dll --def ByteBuffer.def --
output-lib libbb.a
But link fails with errors:
cciAKM3R.o:test.cpp:(.text+0x82): undefined reference to
`WriteByteBuffer::WriteByteBuffer()'
cciAKM3R.o:test.cpp:(.text+0xac): undefined reference to
`ByteBuffer::Wrap(void*, int)'
cciAKM3R.o:test.cpp:(.text+0xc2): undefined reference to
`WriteByteBuffer::Set(int)'
cciAKM3R.o:test.cpp:(.text+0xda): undefined reference to
`WriteByteBuffer::~WriteByteBuffer()'
cciAKM3R.o:test.cpp:(.text+0x119): undefined reference to
`WriteByteBuffer::~WriteByteBuffer()'
collect2: ld returned 1 exit status
How can I solve the problem? Please help me.
Thank you.


I've never had to use pexports; this *should* be good enough:

dlltool -D mydll.dll -l mydll.lib

Then include mydll.lib on gcc's link line. You don't need to deal with
creating the .def file. Be sure you're using MinGW's dlltool and not
some other one you may have installed (do 'which dlltool' and verify).

That said, this has nothing to do with C++. Ask on the mingw-users
mailing list. You can find it here:

http://www.mingw.org/mailing_lists

Jason
 
J

jason.cipriani

Allen <[email protected]> kirjutas:




Are you sure that vc2005 is binarily compatible with MinGW g++? If you are,
then I suggest to turn to some MinGW group or forum, this seems to be a
tool-specific problem.

They are not binary-compatible; although DLLs themselves are a
consistent format. The utilities he's referring to theoretically
create a MinGW GCC import library for any DLL (no matter what was used
to compile the DLL).

Jason
 
R

red floyd

Allen said:
In vc2005, I create a dll name ByteBuffer.dll, which export 3 classes,
i.e. ByteBuffer, ReadByteBuffer and WriteByteBuffer. The source code
is somewhat as following.

#ifndef _BYTE_BUFFER_H_
#define _BYTE_BUFFER_H_

Totally unrelated to your problem, your include guard uses a symbol that
you shouldn't. Identifiers with a leading underscore followed by an
uppercase letter are reserved to the implementation. Period.

That means you may not use them for your own purposes. Just stick with
the trailing underscore: BYTE_BUFFER_H_
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top