“未定义的引用”共享库中的许多(全部?)功能(新手)

发布于 2024-10-17 19:46:58 字数 1712 浏览 1 评论 0原文

我是 C++ 新手。如果这不连贯,请耐心等待。我被要求在 Linux 上构建一个大型系统,该系统最初是在 OS X 上构建的,并且运行良好。原作者已不再在公司工作。构建系统使用自动工具,但也有一些手工制作的 Makefiles 遍历系统调用自动制作的 Makefiles。我已经成功编译了所有 C++ 代码。构建系统还使用libtools,生成共享库并将其存放在/usr/local/lib中。

所以现在我想使用这些库。我编写了一个简短的程序,它只是实例化 ds_dictionary 类的对象并调用其方法之一。 是

#include <iostream>
#include <DSUtils/DSUtils.h>

int main (int argc, char * const argv[]) {

    int32_t integer_data=123;
    char key_alice_integer[] = "alice_integer"; 

    ds_dictionary my_dict;
    my_dict.add_int(key_alice_integer, integer_data);

    return 0;
}

如下:我使用以下命令进行编译,

g++ -lDSUtils -o main my_test_code.cpp

结果

//usr/local/lib/libDSUtils.so: undefined reference to `ds_breakdown_from_time_interval'
//usr/local/lib/libDSUtils.so: undefined reference to `ds_date_breakdown_with_string'
//usr/local/lib/libDSUtils.so: undefined reference to `ds_seconds_duration_of_interval'
...  (about 25 lines like these)
collect2: ld returned 1 exit status

:让我们看看库内部:

garyp@VM:/usr/local/lib$ nm libDSUtils.so | grep ds_breakdown_from_time
    U ds_breakdown_from_time_interval
  1. 上面一行中的“U”...这是否意味着库没有正确构建?
  2. 我正确调用 g++ 吗?
  3. 我是否必须在代码中添加一些内容来告诉它我正在使用该库中找到的函数?
  4. 可能出现哪些错误?我应该从哪里开始探索?

编辑: 啊哈。 DSUtils 库是由多个 C++ 源代码构建的。源代码中有一个 C 程序,它包含所有问题函数。 Makefile 系统根本不处理那个 c 文件。该c 程序可以编译。理想情况下,我想我会弄清楚如何修改 Makefile 来编译该文件并将其添加到库中,但我还没有达到可以弄清楚如何做到这一点的程度。

我可以将 .o 文件添加到现有库中吗?如何?用一个文件创建一个库? ETC?

EDIT_2:我只是做了

g++ -o main -lDSUtils main.o my_new_objectfile.o

,编译、链接和运行都没有错误。那应该有效吗?修复了逻辑错误后,它确实可以工作。

I'm a novice at C++. Be patient if this is incoherent. I'm called upon to build a large system on linux that was originally built on OS X, where it works fine. The original authors are no longer with the company. The build system makes use of autotools, but there are also some hand made Makefiles which walk through the system calling the auto-made Makefiles. I've managed to get all of the c++ code compiled. The build system also uses libtools, and shared libraries are produced and deposited in /usr/local/lib.

So now I'd like to use these libraries. I've written a short program that simply instantiates an object of class ds_dictionary and calls one of its methods. Here it is:

#include <iostream>
#include <DSUtils/DSUtils.h>

int main (int argc, char * const argv[]) {

    int32_t integer_data=123;
    char key_alice_integer[] = "alice_integer"; 

    ds_dictionary my_dict;
    my_dict.add_int(key_alice_integer, integer_data);

    return 0;
}

I compile this with

g++ -lDSUtils -o main my_test_code.cpp

With the result:

//usr/local/lib/libDSUtils.so: undefined reference to `ds_breakdown_from_time_interval'
//usr/local/lib/libDSUtils.so: undefined reference to `ds_date_breakdown_with_string'
//usr/local/lib/libDSUtils.so: undefined reference to `ds_seconds_duration_of_interval'
...  (about 25 lines like these)
collect2: ld returned 1 exit status

Let's look inside the library:

garyp@VM:/usr/local/lib$ nm libDSUtils.so | grep ds_breakdown_from_time
    U ds_breakdown_from_time_interval
  1. The "U" in the line above ... does that mean that the library wasn't built correctly?
  2. Am I calling g++ correctly?
  3. Do I have to put something in the code to tell it that I'm using functions found in that library?
  4. What are possible errors? Where should I start poking around?

EDIT:
Aha. The library DSUtils is built from several c++ sources. There is one c program in the source, and it contains all of the problem functions. The Makefile system doesn't deal at all with that one c file. That c program compiles. Ideally I suppose I'd figure out how to modify the Makefile to compile that file and add it to the library, but I'm not to the point where I can figure out how to do that.

Can I add the .o file to the existing library? How? Create a library with one file? etc?

EDIT_2: I simply did

g++ -o main -lDSUtils main.o my_new_objectfile.o

and the thing compiles, links, and runs without error. Should that work? After fixing a logic bug, it does work.

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

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

发布评论

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

评论(1

ぽ尐不点ル 2024-10-24 19:46:58

U ds_breakdown_from_time_interval

告诉我,ds_breakdown_from_time_interval 将在运行时由另一个库解析。所以我猜您需要链接到定义 ds_breakdown_from_time_interval 的库。

This

U ds_breakdown_from_time_interval

tells me that ds_breakdown_from_time_interval will be resolved by another library during runtime. So I am guessing you need to link to the library that defines ds_breakdown_from_time_interval.

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