1.是的,g++ 可以链接由 gcc 编译 .c 文件生成的 .o (目标) 文件。这是 C++ 与 C 混合编程的常见做法。
但是要在C的头文件中使用extern "C"的包装声明,这样g++就能正确链接。
/* myclib.h */
#ifdef __cplusplus
extern "C" {
#endif
void my_c_function(int param);
#ifdef __cplusplus
}
#endif
注意:
确保 C 代码不包含 C++ 特有的特性
如果 C 代码需要调用 C++ 函数,需要在 C++ 侧使用 extern "C" 声明。
混合编程时要注意内存管理的一致性(如 malloc/free 与 new/delete)
浙公网安备 33010602011771号