你如何用C正确链接停止符号被剥夺? [英] How do you link correctly in C to stop symbols being stripped?

查看:227
本文介绍了你如何用C正确链接停止符号被剥夺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦用C正确链接到的库。

我敢肯定,这是其中的一个神秘ç联的规则,我不完全理解的,但我不明白。

我已经libn,我编译成一个静态libary,libn.a

纳米libn显示:

 道格@忍者:〜/项目/ libnw /建造$纳米../../libn/build/libn.a | grep的参数nIndex
00000034ŧ参数nIndex
00000000ÐnIndex_
00000026ŧnIndex_finalize
00000013ŧnIndex_init
00000000ŧnIndex_map

我也有libnw,这取决于libn。在libnw纳米所示:

 道格@忍者:〜/项目/ libnw /建造$纳米libnw.a | grep的指数
         ü参数nIndex

然而,当我编译程序链接到的libnw和libn我得到:

 道格@忍者:〜/项目/ libnw /编译$化妆
[70%]内置目标NW
[80%]内置目标测试模板
目标测试核心的依赖扫描
[85%]大厦C座对象测试/ NW / MVC /核心/ CMakeFiles /测试Core.dir / Tests.c.o
[90%]大厦C座对象测试/ NW / MVC /核心/ CMakeFiles /测试Core.dir / test.c.o
链接C可执行测试核
../../../../libnw.a(Impl.c.o):在功能`nwCore__Impl_init:
/home/doug/projects/libnw/src/nw/mvc/Core/Impl.c:76:未定义的引用`参数nIndex
collect2:劳工处返回1退出状态
使[2]:*** [测试/ NW / MVC /核心/测试核]错误1
使[1]:*** [测试/ NW / MVC /核心/ CMakeFiles /测试Core.dir /全部]错误2
使:*** [全部]错误2

究其原因,是相当清楚的。当Tests.c - > Tests.c.o,这不是捡了参数nIndex为标志它需要保持:

 道格@忍者:〜/项目/ libnw /编译$纳米测试/ NW / MVC /核心/ CMakeFiles /测试Core.dir / Tests.c.o
         ümyController的
000005a4 t检验
00000000ÐTests_
00000125ŧTests_can_attach_controller
00000080ŧTests_can_create_core
000003d3ŧTests_can_handle_native_event
000001c8ŧTests_can_set_controller
00000322ŧTests_can_update
0000万吨Tests_core_factory
0000056cŧTests_finalize
000005c0ŧTests_getType
0000048cŧTests_init
         ünFactory
         üNTEST
         ünType_nalloc
         ünType_nfree
         ünwCore
         ünwDummyContext_getType
         ünwDummyEvents_getType
         ünwDummyRender_getType
         ünwIContext_getType
         ünwIEvents_getType
         ünwIRender_getType

(注意:完全没有ü参数nIndex的测试目标文件)。

所以,我可以很容易地通过添加在我的测试脚本)来参数nIndex调用(解决这个问题,但没有解决根本问题:

程序取决于力霸取决于libb,力霸有失踪libb它需要解决,但程序有这些符号的引用,所以他们似乎被剥夺的符号。

我在做什么错了?

(是的,我建设使用cmake并根据libn和libnw的静态内置版本)。

编辑:

现在与连接线:

 的/ usr / bin中/ gcc的-std = C99 -g CMakeFiles /测试Core.dir / Tests.c.o \\
      CMakeFiles /测试Core.dir / test.c.o \\
      CMakeFiles /测试Core.dir /助理/ MyController.c.o \\
      CMakeFiles /测试Core.dir /助理/ MyModel.c.o \\
      -o测试核心-rdynamic \\
      /home/doug/projects/tapspin-android/source/deps/libn/build/libn.a \\
      ../../../../libnw.a


解决方案

我不明白您链接线,所以我很难说肯定的,但是这似乎是它可以是一个排序问题

如果所有被 libb 所需的符号都在力霸,那么你应该列出 libb 首先,让他们被列为符号 - 待解决,并参观了 libb 时,他们将得到解决。这不是每本身,它只是不包括(即,它是不是遗漏了积极去除,我在鸡蛋里挑骨头?也许)

此外,有时如果有一个循环依赖(力霸需要一些符号从 libb libb 需要一些符号从力霸)一个人到库多次列出
(这不是 cmake的答案,因为我不使用 cmake的,但使用的接头很多年了,这哪里是一种常见的错误)。

请注意,不像从图书馆的符号,目标文件中的所有符号链接

最简单的解决首先要尝试只是交换两个库的顺序

I'm having some trouble linking properly against libraries in C.

I'm sure this is one of those arcane C linking rules I don't fully understand, but I can't figure it out.

I have libn, which I compile into a static libary, libn.a

nm libn shows:

doug@ninja:~/projects/libnw/build$ nm ../../libn/build/libn.a |grep nIndex
00000034 T nIndex
00000000 D nIndex_
00000026 T nIndex_finalize
00000013 T nIndex_init
00000000 T nIndex_map

I also have libnw, which depends on libn. nm on libnw shows:

doug@ninja:~/projects/libnw/build$ nm libnw.a |grep Index
         U nIndex

However, when I compile a programming linking against libnw and libn I get:

doug@ninja:~/projects/libnw/build$ make
[ 70%] Built target nw
[ 80%] Built target test-template
Scanning dependencies of target test-Core
[ 85%] Building C object tests/nw/mvc/Core/CMakeFiles/test-Core.dir/Tests.c.o
[ 90%] Building C object tests/nw/mvc/Core/CMakeFiles/test-Core.dir/test.c.o
Linking C executable test-Core
../../../../libnw.a(Impl.c.o): In function `nwCore__Impl_init':
/home/doug/projects/libnw/src/nw/mvc/Core/Impl.c:76: undefined reference to `nIndex'
collect2: ld returned 1 exit status
make[2]: *** [tests/nw/mvc/Core/test-Core] Error 1
make[1]: *** [tests/nw/mvc/Core/CMakeFiles/test-Core.dir/all] Error 2
make: *** [all] Error 2

The reason, is quite clear. When Tests.c --> Tests.c.o, it's not picking nIndex up as a symbol it needs to keep:

doug@ninja:~/projects/libnw/build$ nm tests/nw/mvc/Core/CMakeFiles/test-Core.dir/Tests.c.o 
         U MyController
000005a4 T Tests
00000000 D Tests_
00000125 T Tests_can_attach_controller
00000080 T Tests_can_create_core
000003d3 T Tests_can_handle_native_event
000001c8 T Tests_can_set_controller
00000322 T Tests_can_update
00000000 t Tests_core_factory
0000056c T Tests_finalize
000005c0 T Tests_getType
0000048c T Tests_init
         U nFactory
         U nTest
         U nType_nalloc
         U nType_nfree
         U nwCore
         U nwDummyContext_getType
         U nwDummyEvents_getType
         U nwDummyRender_getType
         U nwIContext_getType
         U nwIEvents_getType
         U nwIRender_getType

(Notice the total absence of U nIndex in the tests object file).

So, I can fix this easily by adding a call to nIndex() in my tests script, but that doesn't solve the basic problem:

Program depends on liba depends on libb, liba has missing symbols from libb it needs to resolve, but program has no references to those symbols, so they seem to be being stripped.

What am I doing wrong?

(Yes, I'm building using cmake and depending on the statically built versions of libn and libnw).

Edit:

Now with linker line:

/usr/bin/gcc  -std=c99 -g   CMakeFiles/test-Core.dir/Tests.c.o \
      CMakeFiles/test-Core.dir/test.c.o \
      CMakeFiles/test-Core.dir/helpers/MyController.c.o \
      CMakeFiles/test-Core.dir/helpers/MyModel.c.o \
      -o test-Core -rdynamic \
      /home/doug/projects/tapspin-android/source/deps/libn/build/libn.a \
      ../../../../libnw.a 

解决方案

I don't see you link line so i's hard to tell for sure but this seems like it could be an ordering problem

If all of the symbols that are needed by libb are in liba, then you should list libb first, so that they are listed as symbols-to-be-resolved, and when libb is visited they will be resolved. This is not stripping per-se, it is just not including (i.e. it is omission not an active removal, am I splitting hairs? maybe)

Also, sometimes if there is a cyclical dependency (liba needs some symbols from libb and libb needs some symbols from liba) one has to list the libs multiple times (this is not a cmake answer as I do not use cmake, but have used linkers for many years, this where is a common error).

Note that unlike symbols from libraries, all symbols from object files are linked

Easiest fix to try first is just swap the order of the two libs

这篇关于你如何用C正确链接停止符号被剥夺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆