0

Link Fast MD5 Assembly

above given link has assembly implementation of MD5. when i am trying to call the following code in c with given execute instruction, it run successfully. but when as i try to call or execute in C++11, i got error as undefined reference to Md5_hash function

for compiling in c

gcc --std=c99 md5-test.c md5-fast-x8664.S -o md5-test

same is used for c++11

gcc --std=c++11 md5-test.cpp md5-fast-x8664.S -o md5-test

it show error

/staticLibmd5/main.cpp|32|undefined reference to md5_compress(unsigned char 
const*, unsigned long, unsigned int*)'

is there any other instruction to that in c++ that is missing.

  • 1
    Please show the C++ code you are trying to compile. This is probably a name mangling issue. – Max Langhof Aug 14 '19 at 12:57
  • C and C++ are not the same language. Just because a lot of C code is valid C++ does not mean every valid C program is a valid C++ program. More than likely you'll need to make some changes to the code as C++ has different name mangling than C. – NathanOliver Aug 14 '19 at 12:57
  • 1
    In particular, it is very likely that [this](https://stackoverflow.com/a/12574420/9528746) answer applies. But we can't be sure until we see the code you are giving to your compiler. – Max Langhof Aug 14 '19 at 13:00
  • @MaxLanghof Looking at the linked code, yes, I'm almost certain that's the right answer. – zwol Aug 14 '19 at 13:03
  • 1
    Possibly only some `extern "C"` lacking? – Aconcagua Aug 14 '19 at 13:05
  • The attached duplicate is somewhat correct, but the correct answer is here : https://stackoverflow.com/questions/27215256/unresolved-external-symbol-c-with-assembler#35359098 – Robert Andrzejuk Aug 14 '19 at 13:17

1 Answers1

0

Assuming you are compiling the code from your link verbatim, change these lines of md5-test.c...

// Link this program with an external C or x86 compression function
extern void md5_compress(uint32_t state[static STATE_LEN],
                         const uint8_t block[static BLOCK_LEN]);

to read instead

// Link this program with an external C or x86 compression function
#ifdef __cplusplus
extern "C" {
#endif
extern void md5_compress(uint32_t state[static STATE_LEN],
                         const uint8_t block[static BLOCK_LEN]);
#ifdef __cplusplus
} // extern "C"
#endif

and it should start working. To understand why this is the problem, see this old answer.

In a complete application, the declaration of md5_compress would be in a header file provided by the library that contains the MD5 implementation, and it would be that library's responsibility to put the extern "C" annotations in its header files. This is why you may not have encountered this problem when working with off-the-shelf libraries.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 1
    I don't consider it a good idea to edit 3rd party library headers... If the library author really failed to apply the correct C++ guards, I personally would prefer placing them around the header *include* like in the referenced answer at the end (not nice either, I know...). – Aconcagua Aug 14 '19 at 13:10
  • @Aconcagua Yeah, all I was saying is most popular libraries _do_ have those guards and that's why OP might not have seen the problem before. – zwol Aug 14 '19 at 13:15