During compilation, the undefined reference to 'setpdupowerconsumptioncnt 'error occurs. How can this problem be solved? Is there any good solution? Let's answer the following small series. If you have encountered this problem, please refer to it.
Problem:
The program model is written in C ++. An interface file modelc. cpp is added between the program model and the called function to allow the C program to call the functions in the program model;
Two new functions setPduPowerConsumptionCnt () and setPduPowerConsumptionTot () are added to clear the total power consumption and power consumption counters in the PDU model;
The following error occurs during compilation:
Copy codeThe Code is as follows:
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. 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
Solution:
1. I forgot to add extern "C" to the function implementation of modelc. cpp.
Copy codeThe Code is as follows:
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;
}
After it is added, compile again and the error still exists.
After searching for half a day, the original header file modelc. h and implement the file modelc. the function name in cpp has a different symbol. the header file contains the lower-case s and setPduPowerConsumptionCnt, while the implementation file uses the upper-case S and SetPduPowerConsumptionCnt. In the vim Editor, The Case sensitivity is not set, as a result, it is not easy to see at a glance.
After modification, compilation is successful.
Solve the problem.