// 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 30Now, 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 30NOW, 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 30This 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. :)


Sign In
Create Account

Back to top










