用于体系结构的 C++ 未定义符号 x86_64:"function_name"引用自

c++ undefined symbols for architecture x86_64: "function_name" referenced from

本文关键字:function 体系结构 name 引用 未定义 C++ 符号 x86 用于      更新时间:2023-10-16

是的,我知道已经有几个问题深入到这个细节,但我觉得我的问题不是那么具体。只是有东西在盯着我的脸,但我看不太清楚。

我正在编译C和c++文件一起在一个Makefile。一切似乎都运行良好,直到我得到名义上的错误,这涉及到CFile2中的函数。

编译工作如下(w/占位符名称):

g++ -c main.cpp
g++ -c Class.cpp
gcc -c CFile1.c
gcc -c CFile2.c
g++ main.o Class.o CFile1.o CFile2.o -lcurl

我的Class.cpp有一个Class.hpp,我的CFile1和CFile2都分别有。h文件。所有内容都位于同一目录中,并且它们都具有相同的头保护结构。

#ifndef
#define
//Insert function prototypes here
#endif

我也只包括CFile1.h和CFile2.h在Class.hpp。是否有一些东西我错过了基于这个信息?

也许你错过了c头中的extern "C" ?

如果你编译c文件并想与c++链接,你必须添加extern "C",但c编译器不识别它,所以它必须是#ifdef __cplusplus

在标题开始,function减速之前添加:

#include <stdio> // and all other includes...
#ifdef __cplusplus
extern "C" {
#endif
void funnction_name1(); //sample 1
int funnction_name2(char* p); //sample 2
//must close the extern "C" block
#ifdef __cplusplus
}
#endif

也可以在每个函数前添加,如下所示:

 #ifdef __cplusplus
 extern "C"
 #endif
 // no block for the extern "C", so it applied only for a single function
 void funnction_name(); //a sample func.