Jump to content

Am I... Stupid?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
So I was going to go ahead and answer a question from Davide regarding creating and using libraries (and yes Davide I have that answer, I'll post it soon enough), but while I was doing this I thought about something for a little and decided that I wanted to give something a try, something I thought should have given me no trouble at all. I wanted to link to dynamic libraries provided WITH the program directly, for example having the libraries in the same file, instead of simply installing them into the /usr/lib folder. Allow me to provide the code I used for this example:

// examplelib.hpp

#if !defined(EXAMPLELIB_HPP_INCLUDED)

#define EXAMPLELIB_HPP_INCLUDED 1


class Add2Nums

{

public:

    Add2Nums(int, int);

    int get_return();


private:

    int first, second;

};


#endif

// examplelib.cpp

#include "examplelib.hpp"


Add2Nums::Add2Nums(int first, int second)

{

    this->first = first;

    this->second = second;

}


int Add2Nums::get_return()

{

    return this->first + this->second;

}

// main.cpp

#include "examplelib.hpp"

#include <iostream>


int main()

{

    Add2Nums a(10, 20);

    std::cout << a.get_return() << std::endl;

}

This is pretty simple to understand, I would hope. It's meant to be nothing more than an example of creating a library and using it. Now when I create this as a static library and build everything, things are wonderful:
zekedragon@zeke-laptop:~/Desktop/prog$ g++ examplelib.cpp -c

zekedragon@zeke-laptop:~/Desktop/prog$ ar -cvq libexample.a examplelib.o

zekedragon@zeke-laptop:~/Desktop/prog$ g++ -L. main.cpp -o test

zekedragon@zeke-laptop:~/Desktop/prog$ ./test

30
Now, let's say I write this as, instead, a dynamic library (a shared object file). If I export the .so file to /usr/lib, things are again really awesome.
zekedragon@zeke-laptop:~/Desktop/prog$ rm test examplelib.o libexample.a

zekedragon@zeke-laptop:~/Desktop/prog$ g++ -shared -fPIC examplelib.cpp -o libexample.so

zekedragon@zeke-laptop:~/Desktop/prog$ sudo mv libexample.so /usr/lib/libexample.so

zekedragon@zeke-laptop:~/Desktop/prog$ g++ main.cpp -o test -lexample

zekedragon@zeke-laptop:~/Desktop/prog$ ./test

30
NOW, when I decide to try leaving libexample.so inside of the file WITH test, that's where the trouble arises...
zekedragon@zeke-laptop:~/Desktop/prog$ sudo rm /usr/lib/libexample.so test

zekedragon@zeke-laptop:~/Desktop/prog$ g++ -shared -fPIC examplelib.cpp -o libexample.so

zekedragon@zeke-laptop:~/Desktop/prog$ g++ -L. main.cpp -o test -lexample

zekedragon@zeke-laptop:~/Desktop/prog$ ./test

./test: error while loading shared libraries: libexample.so: cannot open shared object file: No such file or directory

zekedragon@zeke-laptop:~/Desktop/prog$ # Now, when I export LD_LIBRARY_PATH, I'm good.

zekedragon@zeke-laptop:~/Desktop/prog$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

zekedragon@zeke-laptop:~/Desktop/prog$ ./test

30
This has worked fine for me for a while, but I've always been concerned with the LD_LIBRARY_PATH hack since it messes with people's settings until reboot, it's sloppy, and I'm told that LD_LIBRARY_PATH is the spawn of satan. The "solutions" offered by this and other pages are rather unsatisfactory if you look at them. My question is... why isn't there a simpler way to access shared object files in this manner? Why don't the ELF file headers contain relative path information to find shared object files? I'd like to hear your thoughts, and if at all possible perhaps even tell me a solution I don't know about. That'd be awesome. :)
Wow I changed my sig!

#2
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
Wow, imagine my amazement when i saw my name in your post :)). Um... I was talking about the C++ Class Library in Visual Studio, /usr/lib sounds linux-related to me so i don't really know what you are talking about (even though it must be interesting). And, thanks for reminding me, is there any way i can move the DLL to another place? I mean like sort them in a folder instead of keeping all of them there in the same folder with the .exe.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics