简体   繁体   English

链接到Linux上我自己的共享库中某些功能的问题

[英]Issues with linking to certain functions in my own shared library on Linux

I have created a shared library(.so) in Linux, i am facing a very weird issue. 我在Linux中创建了一个共享库(.so),我面临一个非常奇怪的问题。 Initially i have added 3 functions(write_myown,setfolder,register) in library, then i added a few more(init,write_re). 最初,我在库中添加了3个函数(write_myown,setfolder,register),然后又添加了一些函数(init,write_re)。

Basically the entire library is implemented in C++, however the APIs need to be called from both C and C++ based executables. 基本上,整个库都是用C ++实现的,但是需要从基于C和C ++的可执行文件中调用API。 Hence i have used extern "C" for the APIs. 因此,我将extern“ C”用于API。

The strange issue i am facing is that when i try to use the functions write_myown, setfolder and register, the linking goes thru and executable is created. 我面临的一个奇怪的问题是,当我尝试使用函数write_myown,setfolder和register时,链接通过并创建了可执行文件。

But when i try to call functions init or write_re the linking fails, with undefined reference error. 但是,当我尝试调用函数init或write_re时,链接失败,并出现未定义的引用错误。

Even stranger thing that i think but not sure about is that i guess when i am building C only programs it works fine, but when i am building C++ based code, this issue is seen. 我认为但不确定的甚至更奇怪的事情是,我猜我在构建仅C程序时工作正常,但是在构建基于C ++的代码时,就会看到此问题。

I have already checked and confirmed the following things 我已经检查并确认以下事项

  1. The library (.so ) is existing in /usr/lib ( I put it there ) 库(.so)存在于/ usr / lib中(我放在那里)
  2. ldconfig -p | ldconfig -p | grep mylibname shows that my library is present. grep mylibname显示我的库存在。
  3. When i dont call init or write_re the executable is generated, and objdump/readelf shows the appropriate references. 当我不调用init或write_re时,将生成可执行文件,并且objdump / readelf显示适当的引用。
  4. ldd on mylibrary.so shows both init and write_re also exist, visibility is global. mylibrary.so上的ldd显示init和write_re也都存在,可见性是全局的。
  5. The executables or .0 created show the right dependency to mylibrary. 创建的可执行文件或.0显示对mylibrary的正确依赖性。
  6. There are no versions of mylibrary. 没有任何版本的图书馆。
  7. I have confirmed that it is referencing the right .so. 我已经确认它引用了正确的.so。
  8. nm on my library show that all the functions exist. 我的库中的nm表明所有功能都存在。

Any idea how i can solve this? 知道我该如何解决吗? And what could be the issue. 可能是什么问题。 If not how i can proceed with investigation. 如果没有,我该如何进行调查。 Please let me know if any further details are required. 如果需要其他详细信息,请告诉我。

Best Regards, Pavan 最好的问候,帕万

The strange issue i am facing is that when i try to use the functions write_myown, setfolder and register, the linking goes thru and executable is created. 我面临的一个奇怪的问题是,当我尝试使用函数write_myown,setfolder和register时,链接通过并创建了可执行文件。

But when i try to call functions init or write_re the linking fails, with undefined reference error. 但是,当我尝试调用函数init或write_re时,链接失败,并出现未定义的引用错误。

My crystall ball says: when you wrote prototypes for write_myown and setfolder in your .h file, you've added extern "C" to them. 我的write_myown说:当您在.h文件中为write_myownsetfolder编写原型时,已在它们中添加了extern "C"

But when you later added write_re , you put extern "C" on their definition in the .cc file, but neglected to add it to their prototypes in .h . 但是,当您以后添加write_re ,您将extern "C"放在.cc文件中的定义中,但忽略了将其添加到.h的原型中。

If so, 如果是这样的话,

  • the functions are defined with their non-mangled form in the library, and 这些函数在库中以其非混杂形式定义 ,并且
  • a pure C executable can use them fine, and 一个纯C可执行文件可以很好地使用它们,并且
  • a C++ object that references them (outside of the library) does not see extern "C" on them, and so wants their mangled form (which is of course not provided by the library). 一个C++引用它们(库外)对象没有看到extern "C"上他们,因此希望其受到损坏的形式(这是被库提供当然)。

This hypothesis I think fits all the facts currently in your question, and is trivial to verify: did the error message from the linker say undefined symbol: write_re , or did it say undefined symbol: write_re(some, parameter, types) ? 我认为这个假设适合您问题中的所有事实,并且很难验证:来自链接器的错误消息是说undefined symbol: write_re还是说undefined symbol: write_re(some, parameter, types)

If the latter, that's a dead giveaway that some object is referencing mangled name. 如果是后者,那是一些对象引用错位名称的破绽。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM