简体   繁体   中英

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. Initially i have added 3 functions(write_myown,setfolder,register) in library, then i added a few more(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. Hence i have used extern "C" for the APIs.

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.

But when i try to call functions init or write_re the linking fails, with undefined reference error.

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.

I have already checked and confirmed the following things

  1. The library (.so ) is existing in /usr/lib ( I put it there )
  2. ldconfig -p | grep mylibname shows that my library is present.
  3. When i dont call init or write_re the executable is generated, and objdump/readelf shows the appropriate references.
  4. ldd on mylibrary.so shows both init and write_re also exist, visibility is global.
  5. The executables or .0 created show the right dependency to mylibrary.
  6. There are no versions of mylibrary.
  7. I have confirmed that it is referencing the right .so.
  8. nm on my library show that all the functions exist.

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.

But when i try to call functions init or write_re the linking fails, with undefined reference error.

My crystall ball says: when you wrote prototypes for write_myown and setfolder in your .h file, you've added extern "C" to them.

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 .

If so,

  • the functions are defined with their non-mangled form in the library, and
  • a pure C executable can use them fine, and
  • 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).

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) ?

If the latter, that's a dead giveaway that some object is referencing mangled name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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