简体   繁体   中英

g++ undefined reference to a free standing function

this must have been answered a million times, yet I cannot find a suitable solution.

I have defined a free function in sensor.cpp :

std::string printTargetGasName(enalu::CombThreshold::TargetGas){}

Then I have declared the prototype of the function

in sensor.hpp

std::string printTargetGasName(enalu::CombThreshold::TargetGas);

Then I include sensor.hpp in core_enose.hpp and try to use the function in core_enose.cpp ( enalu is just a namespace).

I get undefined reference linking error

core_enose.cpp:284: undefined reference to `enalu::printTargetGasName(enalu::CombThreshold::TargetGas)'

the linking instructions in the make file seem correct, ie the sensor.opp comes after the core_enose.opp:

g++ -g -Wall -Wextra -pedantic -std=c++11 [...] obj_dbg/core_enose.opp [...] obj_dbg/sensor.opp [...]

I also checked to see if the symbol correctly exists in the sensor.opp file:

$> nm obj_dbg/sensor.opp  | grep printTarget
$> 000000000000cc9c T _Z18printTargetGasNameN5enalu13CombThreshold9TargetGasE

I have tried desperate late night measures as well, such as extern , or re including the sensor.hpp directly in the core_enose.cpp file. Nothing helps and at this point I am frustrated at the simple answer that eludes me.

Note that I am not providing code because sensor.?pp files are rather big containing a few classes that I have also been using in my program. What I describe above are the exact steps I followed to add this free function to an otherwise working application.

Could you help me?

Because your link error is about enalu::printTargetGasName , I suspect that you declared the function in your header within the enalu namespace, but the corresponding C++ doesn't have the namespace enclosure. This might fix you in the sensor.cpp file.

namespace enalu
{
    std::string printTargetGasName(enalu::CombThreshold::TargetGas){}
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM