程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言基礎知識 >> undefined reference to `SetPduPowerConsumptionCnt錯誤的解決方法

undefined reference to `SetPduPowerConsumptionCnt錯誤的解決方法

編輯:C語言基礎知識

問題:
程序模型用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編輯器時,沒有設定區分大小寫,導致沒有很容易一眼看出來。

修改之後,成功編譯。
問題解決。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved