編譯時間出現undefined reference to `SetPduPowerConsumptionCnt'錯誤要如何解決呢?有沒有什麼好的解決方案?下面小編就為大家解答吧,如果你也遇到了這種情況,可以過來參考下
問題:
程式模型用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編輯器時,沒有設定區分大小寫,導致沒有很容易一眼看出 來。
修改之後,成功編譯。
問題解決。