这个问题老生长谈了,是因为程序连接阶段找不到函数的定义引起的。
不过今天要说的情况是在某个特殊的情况下引发的,倒是花了作者不少的时间去定位问题。
之前的版本被引用的程序代码sync_group.c是C文件,而引用文件为CPP文件,所以在CPP文件中要通过如下方式,来引用C文件中的函数。
1 #ifdef __cplusplus
2 extern "C" {
3 #endif
4 #include
"loginshm.h"
5 #include
"IpShm.h"
6 #include
"common.h"
7 #include
"struct.h"
8 #include
"sync_group.h"
18 #ifdef __cplusplus
19 }
20 #endif
但是后来sync_group.c升级成了sync_group.cpp,笔者在编译的时候,始终编译不通过,一直说函数未定义。
后来笔者做如下改动,就可以了
1 #include
"sync_group.h"
2 #ifdef __cplusplus
3 extern "C" {
4 #endif
5 #include
"loginshm.h"
6 #include
"IpShm.h"
7 #include
"common.h"
8 #include
"struct.h"
9
10 #ifdef __cplusplus
11 }
12 #endif
把sync_group.h放到extern "C"的外面。
上述问题,只是说明编译器,对待C文件和CPP文件连接的时候是做不同的处理的。(细节具体可以去google)。而触发这种不同处理的方式就是头文件如何被引用。
转载于:https://www.cnblogs.com/netbar/p/3365301.html