[英]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
: 我在sensor.cpp
定义了一个自由函数:
std::string printTargetGasName(enalu::CombThreshold::TargetGas){}
Then I have declared the prototype of the function 然后我声明了函数的原型
in sensor.hpp
在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). 然后,我包括sensor.hpp
在core_enose.hpp
,并尝试使用该功能core_enose.cpp
( enalu
只是一个命名空间)。
I get undefined reference
linking error 我收到undefined reference
链接错误
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: make文件中的链接说明似乎正确,即sensor.opp在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: 我还检查了sensor.opp文件中是否正确存在该符号:
$> 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. 我也尝试过绝望的深夜措施,例如extern
,或者直接将cores.hpp包含在core_enose.cpp文件中。 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. 请注意,我没有提供代码,因为sensor.?pp文件相当大,其中包含我也在程序中使用过的一些类。 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. 因为您的链接错误是关于enalu::printTargetGasName
,所以我怀疑您在enalu
名称空间内的标头中声明了该函数,但相应的C ++没有名称空间外壳。 This might fix you in the sensor.cpp
file. 这可能会在sensor.cpp
文件中修复您。
namespace enalu
{
std::string printTargetGasName(enalu::CombThreshold::TargetGas){}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.