尽管名称存在,静态链接失败

发布于 2024-11-06 15:09:44 字数 1302 浏览 0 评论 0原文

我正在尝试链接到静态库 libcovis.a。一切看起来都很好,但我仍然有

未定义对“CoViG_PublicDemo::MoInS::reset()”的引用

我检查了该名称是否存在于库中

$nm libcovis.a | grep 重置

...

_ZN16CoViG_PublicDemo5MoInS5resetEv

...

我正在使用链接参数 -L/path/to/libcovis.a -lcovis

我做错了什么?

编辑: 如果这样做的话,错误可能是其他原因

gcc main.cpp -I/usr/include/opencv -I/usr/include/cairo -I../../Source -o slam -rdynamic -lGLU -lGL -lSM -lICE -lX11 -lXext - lglut -lXi -lxml2 -lboost_文件系统-mt -llapack -lblas -lcv -lcxcore -lcvaux -lhighgui -lcairo ../../Source/libcovis.a ../../Source/contrib/gnuplot_i/libcovis_contrib_gnuplot_i.a -lgl2ps

它有效!

但是当我在 KDevelop 中使用 cmake 时,我就不再工作了。我用

CMAKE_EXE_LINKER_FLAGS:STRING=-rdynamic -lGLU -lGL -lSM -lICE -lX11 -lXext -lglut -lXi -lxml2 -lboost_filesystem-mt -llapack -lblas -lcv -lcxcore -lcvaux -lhighgui -lcairo /usr/local/src/CoViS-0.0.0-1/Source/libcovis.a /usr/local/src/CoViS-0.0.0-1/Source/contrib/gnuplot_i/libcovis_contrib_gnuplot_i.a -lgl2ps

CMAKE_CXX_FLAGS:STRING=-I/usr/local/src/CoViS-0.0.0-1/Source -I/usr/include/opencv -I/usr/include/cairo

我能看到的唯一区别是路径是绝对的而不是相对的,但如果他找不到库,他会说出来......

I'm trying to link to a static library, libcovis.a. Everything looks fine but I still have

undefined reference to `CoViG_PublicDemo::MoInS::reset()'

I checked that the name exists in the library

$nm libcovis.a | grep reset

...

_ZN16CoViG_PublicDemo5MoInS5resetEv

...

I'm using linking arguments -L/path/to/libcovis.a -lcovis

What am I doing wrong ?

Edit:
The error might be something else, if do

gcc main.cpp -I/usr/include/opencv -I/usr/include/cairo -I../../Source -o slam -rdynamic -lGLU -lGL -lSM -lICE -lX11 -lXext -lglut -lXi -lxml2 -lboost_filesystem-mt -llapack -lblas -lcv -lcxcore -lcvaux -lhighgui -lcairo ../../Source/libcovis.a ../../Source/contrib/gnuplot_i/libcovis_contrib_gnuplot_i.a -lgl2ps

It works !

But when I'm in KDevelop using cmake, I doesn't work anymore. I use

CMAKE_EXE_LINKER_FLAGS:STRING=-rdynamic -lGLU -lGL -lSM -lICE -lX11 -lXext -lglut -lXi -lxml2 -lboost_filesystem-mt -llapack -lblas -lcv -lcxcore -lcvaux -lhighgui -lcairo /usr/local/src/CoViS-0.0.0-1/Source/libcovis.a /usr/local/src/CoViS-0.0.0-1/Source/contrib/gnuplot_i/libcovis_contrib_gnuplot_i.a -lgl2ps

CMAKE_CXX_FLAGS:STRING=-I/usr/local/src/CoViS-0.0.0-1/Source -I/usr/include/opencv -I/usr/include/cairo

The only difference I can see is that the paths are absolute and not relative, but if he couldn't find the libs, he would say it...

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

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

发布评论

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

评论(3

魂ガ小子 2024-11-13 15:09:44

那里有两个不同的问题,第一个是最简单的,您使用了错误的编译器选项。 -L 选项告诉链接器在查找库时也在目录中查找。 -l 告诉它链接特定的库。要链接,您可以使用:

g++ -o test test.o -L/path/to -lcovis

g++ -o test test.o -l/path/to/libcovis.a

如果同一库作为动态库存在于同一目录中,则强制静态链接。

第二个潜在问题是链接器命令行中静态库的顺序确实很重要,因此如果存在对不同静态库的依赖性,这也可能是一个问题。

g++ -o 测试tests.o -ldependent -lprovider

链接器将按命令行中的顺序处理库,并且从每个静态库中,它只会提取所需的符号(具有链接器拥有的尽可能多的信息)当时)。在上面的命令行中,链接器将从 dependent 中提取 test.o 所需的符号,这可能会向程序添加新的未定义符号(依赖项依赖)。当它处理 provider 时,它将填充这些符号。如果命令行中的顺序相反,则 dependent 所需的符号(但 test.o 不需要)将不会添加到可执行文件中,因为链接器不会将其添加到可执行文件中。知道处理 provider 时将需要这些符号。

There are two different issues there, the first of which is the simplest, you have used the wrong compiler options. The -L option tells the linker to also look in the directory when looking for a library. The -l tells it to link the specific library. To link you would then use:

g++ -o test test.o -L/path/to -lcovis

or

g++ -o test test.o -l/path/to/libcovis.a

To force static linking if the same library is present as a dynamic library in the same directory.

The second potential issue is that the order of static libraries in the linker command line does matter, so that might also be an issue if there is a dependency on different static libs.

g++ -o test tests.o -ldependent -lprovider

The linker will process the libraries in order as they are in the command line, and from each static lib it will only pull those symbols that are required (with as much information as the linker has at that time). In the command line above, the linker will extract from dependent the symbols it needs for test.o, and that might in turn add new undefined symbols to the program (the dependencies of dependent). When it processes provider it will fill in those symbols. If the order was reversed in the command line, the symbols that are required by dependent but not by test.o would not be added to the executable, as the linker does not know that those symbols will be needed when processing provider.

香草可樂 2024-11-13 15:09:44

参数应该像 -L/path/to/ -lcovis 吗?

此外,目标文件应放在库之前,例如

g++ obj1.o obj2.o -L/path/to/ -lcovis。

Should the arguments be like -L/path/to/ -lcovis?

Besides, object files should be placed before libs, for example

g++ obj1.o obj2.o -L/path/to/ -lcovis.

暮年慕年 2024-11-13 15:09:44

如果您看到链接在一个上下文中成功,但在另一个上下文中失败,我怀疑问题可能是由链接操作的执行顺序引起的,因为链接器将丢弃库中的符号(如果在该点不需要它们)该库被引用。

这是一个解释的链接: http://www.network-theory.co .uk/docs/gccintro/gccintro_18.html

我过去遇到过类似的情况,发现链接顺序是问题的原因。

If you see the link succeeding in one context but not another, I suspect the problem may be caused by the order in which the link operation is executed as the linker will discard symbols in a library if they're not needed at the point in which the library is referenced.

Here is a link explaining: http://www.network-theory.co.uk/docs/gccintro/gccintro_18.html

I've run into similar situations in the past the linking order was found to be the cause of the problem.

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