undefined reference to `SetPduPowerConsumptionCnt'错误的解决方法

时间:2021-11-04 07:16:44

问题:
程序模型用C++语言编写,在程序模型和调用函数之间添加了一个接口文件modelc.cpp,用来让用c语言程序调用程序模型中的函数;

新添加了两个函数setPduPowerConsumptionCnt()和setPduPowerConsumptionTot(),用来清除PDU模型中的总功耗和功耗计数器;

编译时出现错误提示:

复制代码 代码如下:

dingq@wd-u1110:~/hwsvn/2sw/1prj_linux/pdu/src/branches/pdu-isocket$ make clean;make
rm -f *.o pdu
arm-linux-g++ -g3 -Wall -o0 -I../../../tools/eldk42/arm/usr/include/ -I../../../tools/eldk42/arm/usr/include/C++/4.2.2/ -c main.cpp -o main.o
arm-linux-g++ -g3 -Wall -o0 -I../../../tools/eldk42/arm/usr/include/ -I../../../tools/eldk42/arm/usr/include/C++/4.2.2/ -c model.cpp -o model.o
arm-linux-g++ -g3 -Wall -o0 -I../../../tools/eldk42/arm/usr/include/ -I../../../tools/eldk42/arm/usr/include/C++/4.2.2/ -c modelc.cpp -o modelc.o
arm-linux-g++ -g3 -Wall -o0 -I../../../tools/eldk42/arm/usr/include/ -I../../../tools/eldk42/arm/usr/include/C++/4.2.2/ -c IniFile.cpp -o IniFile.o
arm-linux-g++ -g3 -Wall -o0 -I../../../tools/eldk42/arm/usr/include/ -I../../../tools/eldk42/arm/usr/include/C++/4.2.2/ -c ClientSocket.cpp -o ClientSocket.o
arm-linux-g++ -g3 -Wall -o0 -I../../../tools/eldk42/arm/usr/include/ -I../../../tools/eldk42/arm/usr/include/C++/4.2.2/ -c Thread.cpp -o Thread.o
arm-linux-g++ -g3 -Wall -o0 -I../../../tools/eldk42/arm/usr/include/ -I../../../tools/eldk42/arm/usr/include/C++/4.2.2/ -c SensorReader.cpp -o SensorReader.o
arm-linux-g++ -g3 -Wall -o0 -I../../../tools/eldk42/arm/usr/include/ -I../../../tools/eldk42/arm/usr/include/C++/4.2.2/ -c LcdWriter.cpp -o LcdWriter.o
arm-linux-g++ -g3 -Wall -o0 -I../../../tools/eldk42/arm/usr/include/ -I../../../tools/eldk42/arm/usr/include/C++/4.2.2/ -c Monitor.cpp -o Monitor.o
arm-linux-g++ -g3 -Wall -o0 -I../../../tools/eldk42/arm/usr/include/ -I../../../tools/eldk42/arm/usr/include/C++/4.2.2/ -c Helper.cpp -o Helper.o
arm-linux-g++ -g3 -Wall -o0 -I../../../tools/eldk42/arm/usr/include/ -I../../../tools/eldk42/arm/usr/include/C++/4.2.2/ -c Mutex.cpp -o Mutex.o
arm-linux-g++ -g3 -Wall -o0 -I../../../tools/eldk42/arm/usr/include/ -I../../../tools/eldk42/arm/usr/include/C++/4.2.2/ -c serial.cpp -o serial.o
arm-linux-gcc -g3 -Wall -o0 -c frm_package.c -o frm_package.o
arm-linux-gcc -g3 -Wall -o0 -c nettrans.c -o nettrans.o
arm-linux-gcc -g3 -Wall -o0 -c sensor-att7053.c -o sensor-att7053.o
arm-linux-g++ -L../../../tools/lib/ -lpthread -o pdu main.o model.o modelc.o IniFile.o ClientSocket.o Thread.o SensorReader.o LcdWriter.o Monitor.o Helper.o Mutex.o serial.o frm_package.o nettrans.o sensor-att7053.o
sensor-att7053.o: In function `reset_EnergyPort':
/home/dingq/hwsvn/2sw/1prj_linux/pdu/src/branches/pdu-isocket/sensor-att7053.c:83: undefined reference to `SetPduPowerConsumptionCnt'
/home/dingq/hwsvn/2sw/1prj_linux/pdu/src/branches/pdu-isocket/sensor-att7053.c:92: undefined reference to `SetPduPowerConsumptionTot'
collect2: ld returned 1 exit status
make: *** [pdu] Error 1


解决办法:
1. 忘了在modelc.cpp的函数实现中添加extern "C"了。

复制代码 代码如下:


extern "C" int setPduPowerConsumptionCnt(int index, unsigned int val) {
 if (index > 8 || index < 1) {
  printf("Error: The index available is between 1 and 8.\n");
  return -1;
 }
 model->Channels()[index-1].TheEnergyPort().SetPowerConsumptionCount(val);

 

 return 0;
}


添加之后,再次编译,错误仍然存在。
查找了半天,原来头文件modelc.h中和实现文件modelc.cpp中的函数名差了一个符号,头文件中是小写的s,setPduPowerConsumptionCnt,而实现文件中用了大写的S,SetPduPowerConsumptionCnt;而使用vim编辑器时,没有设定区分大小写,导致没有很容易一眼看出来。

 

修改之后,成功编译。
问题解决。