符号查找错误

发布于 2024-12-05 01:04:04 字数 874 浏览 2 评论 0原文

我正在 Ubuntu 下用 C++ 编写一个共享库。源代码包含两个文件:ClassA.h 和 ClassA.cpp。以下是部分内容:

ClassA.h:

namespace calss_a{
class ClassA{
public:
   ...
   void foo(int nBlockIndex);
   ...
}
}

ClassA.h:

namespace calss_a{
    ...
   void classA::foo(int nBlockIndex){printf("....");}
    ...
}

上述源代码编译后的 .so 文件在 test.cpp 中使用如下:

...
class_a::ClassA * ptr = new class_a::ClassA();
...
ptr->foo(0);
...

但是,当 test.cpp 的可执行文件运行时,出现错误如下:

符号查找错误:/home/hzhu/test:未定义符号:_ZN16class_a15ClassA16fooEj

但是如果我在 ClassA.cpp 和 ClassA.h 中更改类型将foo()的参数“nBlockIndex”从“int”更改为“unsigned in”,并重新编译.so文件,问题就消失了。在进一步的实验中,参数“nBlockIndex”的类型仍然定义为“int”,但在 test.cpp 中我这样调用“foo”:

ptr->foo(1); //pass 1 instead of 0

那么问题也消失了。

谁能告诉我这是怎么回事?

谢谢。

I am writing a shared library with C++ under Ubuntu. The source code contains two files: ClassA.h and ClassA.cpp. Bellow are parts of the contents:

ClassA.h:

namespace calss_a{
class ClassA{
public:
   ...
   void foo(int nBlockIndex);
   ...
}
}

ClassA.h:

namespace calss_a{
    ...
   void classA::foo(int nBlockIndex){printf("....");}
    ...
}

The compiled .so file from the above source codes is used in test.cpp as follows:

...
class_a::ClassA * ptr = new class_a::ClassA();
...
ptr->foo(0);
...

However, when the executable build from test.cpp runs, there is an error as follows:

symbol lookup error: /home/hzhu/test: undefined symbol: _ZN16class_a15ClassA16fooEj

But if I change in ClassA.cpp and ClassA.h the type of foo()'s argument "nBlockIndex" from "int" to "unsigned in", and recompile the .so file, then the problem disappears. In a further experiment, the argument "nBlockIndex"'s type is still defined to be "int", but in test.cpp I call "foo" this way:

ptr->foo(1); //pass 1 instead of 0

then the problem disappear as well.

Can anyone tell me what is going on here?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你不是我要的菜∠ 2024-12-12 01:04:04

我自己对这种事情有点陌生,但我也遇到了符号查找错误,所以我想分享一下我发现的内容。

符号查找错误的问题通常是在两个不同的地方以两种不同的方式定义某些东西。如果

 nm -u your-executable | grep undefined-symbol

这样做,您将找到目标文件或可执行文件中使用的符号的定义位置

I am a bit new to this kind of thing myself, but I am also running into a symbol lookup error and so thought I would share what I have found out.

The problem with symbol lookup errors usually is that something is being defined two different ways in two different places. If you do

 nm -u your-executable | grep undefined-symbol

then you will find out where the symbols being used in an object file or executable file are defined

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文