在 C++ 中调用时对内联程序集代码的未定义引用

Undefined reference to inline assembly code while calling in c++

本文关键字:代码 程序集 未定义 引用 C++ 调用      更新时间:2023-10-16

Link Fast MD5 Assembly

上面给出的链接具有MD5的汇编实现。 当我尝试使用给定的执行指令在 C 中调用以下代码时,它成功运行。但是当我尝试在 C++11 中调用或执行时,我收到错误,因为未定义Md5_hash函数引用

用于在 C 语言中编译

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

同样用于 C++11

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

它显示错误

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

C++ 中是否缺少任何其他指令。

假设您正在逐字编译链接中的代码,请更改这些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]);

改为阅读

// 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

它应该开始工作。 要了解为什么会出现这个问题,请参阅此旧答案。

在完整的应用程序中,md5_compress的声明将位于包含 MD5 实现的库提供的头文件中,并且该库负责将extern "C"注释放在其头文件中。 这就是为什么您在使用现成库时可能没有遇到此问题的原因。