Author Topic: undefined reference to shared library from shared library  (Read 18918 times)

Offline MyMindsAvatar

  • Multiple posting newcomer
  • *
  • Posts: 13
undefined reference to shared library from shared library
« on: November 24, 2012, 02:51:33 am »
Hi folks,

I'm having trouble compiling a project of mine and I'm not sure exactly where the problem lies. I suspect I haven't enabled/disabled something in the C::B IDE, but am at a complete loss as to exactly what that might be. Apologies for the slightly complicated title, but my problem is slightly complicated...

My situation is that I'm writing a shared library (say, library A) that links to another shared library (library B). I've got these two library projects and a MainApp project in a single workspace, with project dependencies: MainApp depends on library A, library A depends on library B. Both libraries build ok and in any order (i.e. library B builds ok on it's own, and library A also builds ok after building library B), but when I try to build MainApp I am hit with undefined reference errors on library A.

I have placed the reference to library B in the Link Settings -> Link Libraries for library A, and similarly the reference to library A for MainApp. I expected library A to implicitly use library B because of its link settings, but (on reading a post online: http://gcc.gnu.org/ml/gcc-help/2008-07/msg00266.html) when running "ldd" on the library A file to list the dependencies, I don't see library B listed.

Build log for library B (Images):
g++ -shared -L/home/richard/programming/wxWidgets-2.9.4/mybuild/lib  obj/Debug/src/StatusGridCellRendererBitmaps.o obj/Debug/src/ItemsPanelBitmaps.o   -o bin/Debug/libImages.so -L/usr/local/lib -pthread   -lwx_gtk2u-2.9

Build log for library A (RunSamplesWorkspace):
g++ -shared -L../Images/bin/Debug -L/home/richard/programming/wxWidgets-2.9.4/mybuild/lib  obj/Debug/src/TableArrangement.o obj/Debug/src/StatusGridCellRenderer.o obj/Debug/src/ItemExecutable.o obj/Debug/src/ItemsPanel.o obj/Debug/src/RunlistPanel.o   -o bin/Debug/libRunSamplesWorkspace.so -L/usr/local/lib -pthread   -lwx_gtk2u-2.9    -lImages

Build log for MainApp (MainApp):
g++ -L../RunSamplesWorkspace/bin/Debug -L/home/richard/programming/wxWidgets-2.9.4/mybuild/lib  -o bin/Debug/MainApp obj/Debug/src/App.o   -L/usr/local/lib -pthread   -lwx_gtk2u-2.9    -lRunSamplesWorkspace
../RunSamplesWorkspace/bin/Debug/libRunSamplesWorkspace.so: undefined reference to `RunSamples::GetDeleteBitmap()'
     (message repeated for all namespace functions included, but are all defined in the library B implementation files)


Please help. What have I missed or done wrong? Any and all advice is most welcome, and please request any extra information as you need. Or, indeed, if this is the wrong forum to ask this question (is it compiler/linker related?), then please forgive me but please tell me so I can go to their forums and try again.

Thanks in advance.

Richard ~

Ubuntu 12.10, Code::Blocks svn (pasgui, precise package), gcc 4.7.2

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: undefined reference to shared library from shared library
« Reply #1 on: November 24, 2012, 05:53:13 pm »
Wrong forum/website; if you have undefined references it is a Linking problem.

Why do you NOT add "Images" library to library list.

Tim S.



C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline MyMindsAvatar

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: undefined reference to shared library from shared library
« Reply #2 on: November 24, 2012, 06:19:22 pm »
Thanks, I knew it had to be a linking problem (I still relatively new to C++ programming), but I had linked the libraries through the Linker Settings under Build Options (hence my first thought that I'd done something wrong in C::B). I thought this would work (it works when linking library A to MainApp). Is this the library list you mentioned? I've tried linking to library B from both library A and MainApp but that doesn't work (same messages as before anyway). If there's nothing I've done wrong from a C::B perspective then I will move to a linking forum with thanks.

Richard ~


Offline MyMindsAvatar

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: undefined reference to shared library from shared library
« Reply #4 on: November 25, 2012, 12:41:29 am »
Thanks for those links. I've pored through these help files and from what I can make out, I have done everything correctly from a C::B perspective. I strongly suspect (as you mentioned earlier) a linking problem. The ldd output for my library A didn't include library B, which it should have done, so there must be something wrong with the library building/linking.

Thanks for your help. I feel quite confident that I've done things correctly in C::B now. Just gotta source the linking problem.

All the best,

Richard ~

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: undefined reference to shared library from shared library
« Reply #5 on: November 25, 2012, 01:40:52 am »
You still need to add the missing library to the list.
Did not figure out how to add the library to the list from the links I posted?

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline MyMindsAvatar

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: undefined reference to shared library from shared library
« Reply #6 on: November 25, 2012, 01:55:35 am »
Under Build Options -> Linker Settings -> Link Libraries, I have:

for RunSamplesWorkspace (library A): Images (library B, with the path to the .so file added to the Search Directories -> Linker list)
for MainApp: Images (library B, as above), and RunSamplesWorkspace (library A, as above)

Under Properties -> Build Targets -> Dependencies, I have:

for MainApp: relative paths to both libraries
for RunSamplesWorkspace (library A): relative path to library B

Have I genuinely missed something here?

Thanks again for your help.

Richard ~

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: undefined reference to shared library from shared library
« Reply #7 on: November 25, 2012, 02:05:20 am »
Quote
Build log for MainApp (MainApp):
g++ -L../RunSamplesWorkspace/bin/Debug -L/home/richard/programming/wxWidgets-2.9.4/mybuild/lib  -o bin/Debug/MainApp obj/Debug/src/App.o   -L/usr/local/lib -pthread   -lwx_gtk2u-2.9    -lRunSamplesWorkspace
../RunSamplesWorkspace/bin/Debug/libRunSamplesWorkspace.so: undefined reference to `RunSamples::GetDeleteBitmap()'
     (message repeated for all namespace functions included, but are all defined in the library B implementation files)

Do you see the missing "Images" library above?
I do NOT see it.

Edit: Once you get the "Images" library in the list; I am guessing it should be to the end of the command.
Edit2: Just re-read the first post; CB by design does next to nothing implicitly; it is NOT Visual Studio which does so much implicitly that it is not worth using to learn to program. Once you know to program, VS should be OK; but, bad idea to use it to learn.

Tim S.
« Last Edit: November 25, 2012, 02:44:51 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline MyMindsAvatar

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: undefined reference to shared library from shared library
« Reply #8 on: November 25, 2012, 10:11:13 am »
My apologies, that must have been from an earlier attempt, but as you can see below it doesn't seem to make a difference (link order doesn't impact this either, -lImages can come after -lRunSamplesWorkspaces with no change in result):

g++ -L../RunSamplesWorkspace/bin/Debug -L../Images/bin/Debug  -o bin/Debug/MainApp obj/Debug/src/App.o   -L/usr/local/lib -pthread   -lwx_gtk2u-2.9    -lImages -lRunSamplesWorkspace
../RunSamplesWorkspace/bin/Debug/libRunSamplesWorkspace.so: undefined reference to `RunSamples::GetDeleteBitmap()'
(etc)


Though, forgive me if I'm wrong here, I didn't think that should matter when it's only library A (RunSamplesWorkspace) that uses library B (Images). I figured that A would depend on B, so would also be listed in ldd A, but because it isn't listed I suspect a linker problem with building A (for completeness, below is the latest build log).

g++ -shared -L../Images/bin/Debug  obj/Debug/src/TableArrangement.o obj/Debug/src/StatusGridCellRenderer.o obj/Debug/src/ItemExecutable.o obj/Debug/src/ItemsPanel.o obj/Debug/src/RunlistPanel.o   -o bin/Debug/libRunSamplesWorkspace.so -L/usr/local/lib -pthread   -lwx_gtk2u-2.9    -lImages

Thanks again,

Richard ~

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: undefined reference to shared library from shared library
« Reply #9 on: November 25, 2012, 03:12:08 pm »
The order matters for most linkers!

Code
g++ -L../RunSamplesWorkspace/bin/Debug -L../Images/bin/Debug  -o bin/Debug/MainApp obj/Debug/src/App.o   -L/usr/local/lib -pthread   -lwx_gtk2u-2.9    -lImages -lRunSamplesWorkspace
../RunSamplesWorkspace/bin/Debug/libRunSamplesWorkspace.so: undefined reference to `RunSamples::GetDeleteBitmap()'
(etc)

Try to always paste the library you are adding at the end or the beginning if it still has the linking error.
In this case, the end is needed for GCC/g++.
So, move "-lImages" after "-lRunSamplesWorkspace"

Edit2: In code::blocks this means move the library down the list so "Images" is at the bottom.
I suggest adding all new libraries at the bottom of the list till you find a better way.

Tim S.
« Last Edit: November 25, 2012, 03:23:48 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline MyMindsAvatar

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: undefined reference to shared library from shared library
« Reply #10 on: November 25, 2012, 10:51:39 pm »
Thanks for the linking advice, but I don't mean to be stubborn here but MainApp doesn't use anything from library B, just library A, so shouldn't library A list B when tying ldd libraryA. This is why I wonder about the linking when building library A (RunSamplesWorkspace).

I tried swapping the order as you suggested anyway, but, as with my previous attempts, it's made no difference.
Code
g++ -L../RunSamplesWorkspace/bin/Debug -L../Images/bin/Debug  -o bin/Debug/MainApp obj/Debug/src/App.o   -L/usr/local/lib -pthread   -lwx_gtk2u-2.9    -lRunSamplesWorkspace -lImages
../RunSamplesWorkspace/bin/Debug/libRunSamplesWorkspace.so: undefined reference to `RunSamples::GetDeleteBitmap()'
(etc)

Again, I really don't mean to be stubborn or rude here; I really do appreciate your feedback and comments. I'm just trying to understand where my problem lies. It's good to know that I've been handling Code::Blocks correctly (mostly) when trying to write for and with custom shared libraries.

Thanks,

Richard ~

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: undefined reference to shared library from shared library
« Reply #11 on: November 25, 2012, 11:12:08 pm »
I think it is time to post your question in another forum.
I am only a C programmer; it is possible this is a C++ linking issue I am not familiar with.

Here a site that is good. http://cboard.cprogramming.com/cplusplus-programming/

Without the code it is hard to do much more; and even with the code I am unlikely to help very much.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline MyMindsAvatar

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: undefined reference to shared library from shared library
« Reply #12 on: November 25, 2012, 11:22:49 pm »
Much obliged for your help and pointers. I have posted already on StackOverflow (http://stackoverflow.com/questions/13546812/ldd-doesnt-show-custom-shared-library-after-building) and will await responses there (will probably try your suggestion as well, no harm I'm sure). I'm under no time constraint so can afford the time here, it's just very annoying at this stage (I have alternatives to using libs-linked-to-libs, but I'd rather use them if I can for other reasons).

Many thanks again,

Richard ~

Edit: FOUND IT! Though I don't understand it.
I had declared a small collection of functions in a namespace in the header files. I tried removing the functions from the namespace and voila. My question then turns into a "Why does this not work with functions in a namespace?" but that is most definitely not in the realms of this forum. Again, my thanks for your support.
« Last Edit: November 26, 2012, 01:35:32 am by MyMindsAvatar »