Is a struct preserved when used into a cpp file?

C

CViniciusM

Hello,

a) I have a .h (header file) that declares a struct:
....
typedef struct {
UINT4 state[4]; /* state (ABCD) */
UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
unsigned char buffer[64]; /* input buffer */
} MD5_CTX;
....

b) and I have .c (C source file) that uses the header above:
....
void MD5Init(MD5_CTX *context);
....

c) I have created a .lib (library file) with the functions above.

I have used the header file above into a .cpp (C++ source file). The
..cpp file uses the .lib functions, but the compiles says "Unresolved
external MD5Init(MD5_CTX*) ...".

I think the .lib was included correctly on the project, so is a struct
preserved when used into a cpp file?

Thanks in advance, Vinicius.

PS: I'm using actually the Borland C++Builder 3 IDE for this project,
but my doubts is concerned about the theory, excuse me for anything.
 
K

Karl Heinz Buchegger

CViniciusM said:
Hello,

a) I have a .h (header file) that declares a struct:
...
typedef struct {
UINT4 state[4]; /* state (ABCD) */
UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
unsigned char buffer[64]; /* input buffer */
} MD5_CTX;
...

b) and I have .c (C source file) that uses the header above:
...
void MD5Init(MD5_CTX *context);
...

c) I have created a .lib (library file) with the functions above.

I have used the header file above into a .cpp (C++ source file). The
.cpp file uses the .lib functions, but the compiles says "Unresolved
external MD5Init(MD5_CTX*) ...".

In the header file you have a prototype for the function.
What does it look like?

On epossibility would eg. be:

extern "C" void MD5Init( MD5_CTX *constext );

you need to tell the C++ compiler, that MD5Init was compiled with
a C compiler, such that the C++ compiler doesn't use name mangling
for that function.
 
C

CViniciusM

Karl Heinz Buchegger said:
CViniciusM said:
Hello,

a) I have a .h (header file) that declares a struct:
...
typedef struct {
UINT4 state[4]; /* state (ABCD) */
UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
unsigned char buffer[64]; /* input buffer */
} MD5_CTX;
...

b) and I have .c (C source file) that uses the header above:
...
void MD5Init(MD5_CTX *context);
...

c) I have created a .lib (library file) with the functions above.

I have used the header file above into a .cpp (C++ source file). The
.cpp file uses the .lib functions, but the compiles says "Unresolved
external MD5Init(MD5_CTX*) ...".

In the header file you have a prototype for the function.
What does it look like?

On epossibility would eg. be:

extern "C" void MD5Init( MD5_CTX *constext );

you need to tell the C++ compiler, that MD5Init was compiled with
a C compiler, such that the C++ compiler doesn't use name mangling
for that function.

Yes, I did do the following:
md5.h:
#ifdef __cplusplus
....
extern "C" void MD5Init(MD5_CTX *context);
....
#else
....
void MD5Init(MD5_CTX *context);
....
#endif

This way, I can use this header file both to compile a C library as to
use on a C++ project (including the header file into a C++ source file
and the library into the project).

Thank you.
 

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,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top