SFML2 application cannot find shared objects - c++

I downloaded and compiled SFML2 from git ( debug, release, static and dynamic ) and I successfully compile some sample code from their tutorial using:
g++ main.cpp -lsfml-graphics -lsfml-window -lsfml-system
The problem occurs when I try to run the binary, it can't find any shared objects (libsfml-graphics.so.2, libsfml-window.so.2 etc)
I checked and they are present in /usr/local/lib.
Am I missing something?
Using Fedora 17 x64 and g++ 4.7.2 if that's relevant

/usr/local/lib is normally not searched by the dynamic linker. Add it to LD_LIBRARY_PATH.
Alternatively, configure the dynamic linker to always search /usr/local/lib and perhaps /usr/local/lib64. This is usually done by adding the paths to the /etc/ld.so.conf file, and running ldconfig.
There is sometimes also a 32/64 bit issue, that is, one tries to run a 32-bit executable and only 64-bit libraries are present, or vice versa. Run file <somtething>.so and file <your-executable> to determine their architecture. In general, 32-bit libraries go to <whatever>/lib and 64-bit ones to <whatever>/lib64, but sometimes they end up in a wrong place.

Related

Linux executable can't find shared library in same folder

I am relatively new to Linux development, having been using Windows for a while now. Anyway, I am compiling a C++ game using g++ on both Windows and Linux (using mingw32 when needed), and am linking against SDL2 and SDL2_mixer. On Windows, one would only need to put the DLL files in the same folder as the executable and everything would run fine. On Linux however, although the code compiled just fine with not even a single warning, I get this at runtime :
./nKaruga: error while loading shared libraries: libSDL2_mixer-2.0.so.0: cannot open shared object file: No such file or directory
although said shared lib is in the same folder. I looked up several similar cases on Stack Overflow, all of them involving the use of LD_LIBRARY_PATH, and tried it but to no avail.
% LD_LIBRARY_PATH=pwd
% export LD_LIBRARY_PATH
% ./nKaruga
./nKaruga: error while loading shared libraries: libSDL2_mixer-2.0.so.0: cannot open shared object file: No such file or directory
I want to distribute this program on systems that do not necessarily have admin rights to install dependencies, hence why I am putting the SO in the same folder as the executable.
Thanks by advance !
LD_LIBRARY_PATH is a quick ad-hoc hack to specify alternate library loading search paths. A more permanent and cleaner solution is to specify the specific sets of paths in which libraries shall be searched specific for your particular binary. This is called the rpath (Wikipedia article on it: https://en.wikipedia.org/wiki/Rpath). There are a number of "variables" that can be specified in the binary rpath that get substituted. In your case the rpath variable ${ORIGIN} would be the most interesting for you. ${ORIGIN} tells the dynamic linker to look for libraries within the very same directory in which also the binary resides.
The rpath can be set at link time with the -rpath linker option, i.e. when invoked through GCC the option would be -Wl,-rpath='${ORIGIN}', i.e.
gcc -o program_binary -Wl,-rpath='${ORIGIN}' -lSDL2_mixer a.o b.o …
For an existing binary the rpath can be set post-hoc using the chrpath or patchelf tools; it's better to set it at link time proper, though.

Library link error when starting Windows application compiled with MinGW on another computer

I wrote a simple HelloWorld console application and compiled it on Windows 7 with MinGW compiler using one of these commands:
gcc -Wall -pedantic Hello.c -o Hello.exe
g++ -Wall -pedantic Hello.cpp -o Hello.exe
However the compiler links some own dynamic libraries into the app and when i copy the executable into another computer with Windows 7, which does not have MinGW installed, i'm getting missing library error. On Linux this problem is solved by package system, which automatically installs all needed libs, but in Windows you surely don't want to tell your users to install MinGW in order to run your program.
So my question is: How do i link all libraries properly and what else do i have to do to make my application run independently?
Although i believe, this must be a fundamental problem to all Windows programmers, i have been unable to find any answers on the internet (maybe i just don't know how and what to search).
It was in the FAQ at some stage, but now I seem to find it only on this page:
Why I get an error about missing libstdc++-6.dll file when running my program?
GCC4 dynamically link to libgcc and libstdc++ libraries by default
which means that you need a copy of libgcc_s_dw2-1.dll and
libstdc++-6.dll files to run your programs build with the GCC4 version
(These files can be found in MinGW\bin directory). To remove these DLL
dependencies, statically link the libraries to your application by
adding "-static-libgcc -static-libstdc++" to your "Extra linking
options" in the project settings.
Try this,
g++ -static-libgcc -static-libstdc++ -Wall -pedantic Hello.cpp -o Hello.exe
I'm afraid to say that with all of the applications installed on my machine, it's easy to identify which ones were built with MinGW. The telltale sign is a folder filled with libraries.
Check to see if the libraries that you need are distributable, and then simply include them in your .exe directory.
Although you may have other applications installed on user's machine, and some of them may contain the libraries that you need, there's a good chance that your application wont be compatible with them. This is why asking your users to install MinGW would be unlikely to work anyways.

How to deploy a SFML game server to a Linux Server?

I wrote a mini client-server game that works fine on my computers (running linux), since I installed SFML (and GCC 4.8) on both the Client and Server. Now I want to deploy the server application to another Linux that does not have SFML.
First I tried to dynamic link the SFML libraries used (network and system):
g++ server.cpp -o ServerLinux -std=c++11 -Os -lsfml-network -lsfml-system
But when I run the Server application it says it could not find sfml-network.so.2 and sfml-system.so.2 even though those 2 files are on the same folder of the binary.
I then static linked both libraries:
g++ -DSFML_STATIC server.cpp -o ServerLinux -std=c++11 -Os -lsfml-network-s -lsfml-system-s
And then when I run it says it could not find GLIBC_2.15 and GLIBC_2.17
Finally on my last try I static linked both libstc++ and libgcc:
g++ -DSFML_STATIC server.cpp -o ServerLinux -std=c++11 -Os -lsfml-network-s -lsfml-system-s -static-libstdc++ -static-libgcc
But I still get the same error (could not find GLIBC_2.15 and GLIBC_2.17).
Reading similar problems it seems that one should never static link glibc. But I don't know how to proceed, how can I deploy my mini game-server to a Linux box that does not have SFML?
Linux systems search for shared libraries by utilizing the LD_LIBRARY_PATH environment variable and they don't automatically look for binary files next to the application, as it is the case on Windows.
An very often used method of deploying with shared libraries, is to include them in an sub-directory or similar and instead of launching the application directly run a shell script that would add the directory with the libraries temporarily to ?LD_LIBRARY_PTH` and then start the application.
The other issue you're having is related to dependencies.
For shared libraries you'd not only have to provide the shared SFML libraries, but also provide the shared libraries of the dependencies, unless you can 100% guarantee that the target system will have the equal library version.
If you just build static libraries of SFML, they'll still point to shared runtime libraries and alike, thus if you don't provide the matching version with the application, it will simply fail to start, since it can't find the library.
If you link statically against the runtime libraries you wouldn't need to provide shared libraries for your application, but since the SFML libraries were still link dynamically against the runtime libraries, they request the shared libraries anyways.
So if you don't want to any shared library files any more, you'll need to link SFML statically against the runtime library (uncheck BUILD_SHARED_LIBS and check SFML_USE_STATIC_STD_LIBS).
Keep in mind that when linking statically, you'll need to link statically against all dependencies - -static might be useful.

C++ linking boost library

First I built the Boost libraries that require building by going to /usr/local/boost_1_49_0/ and running the bootstrap.sh. This went off alright.
Step (1) created all the .so and .a files in /usr/local/boost_1_49_0/stage/lib
I tested linking against a library, say lboost_regex and #include <boost/regex> in my source code. This also went off alright.
Finally trying out the example on asio, I tried:
g++ -I/usr/local/boost_1_49_0 MAIN.cpp -o MAIN -L/usr/local/boost_1_49_0/stage/lib -lboost_thread -lboost_system -lpthread
(4) compiled alright. But when I run the program with ./MAIN, I get the following error:
./MAIN: error while loading shared libraries: libboost_system.so.1.49.0: cannot open shared object file: No such file or directory
The -L option only sets a compile-time library search path; if you want a shared library to be found at runtime then its directory must be known at runtime.
One way to set this with g++ is to pass -rpath to the linker, via the compiler; in your case you could say -Wl,-rpath -Wl,/usr/local/boost_1_49_0/stage/lib. (This embeds the directory in the executable.)
Another way is to install the libraries in a place that the linker searches by default (e.g. /usr/local/lib might be such a place, depending on how the system is configured).
Yet another way is to set an environment variable such as LD_LIBRARY_PATH (Linux or Solaris) or DYLD_LIBRARY_PATH (Mac OS X), to tell the linker where to search when launching executables from the shell where the variable is set.
Are you sure the shared library is in a place where the loader can find it? Either put it in a system wide directory or the same directory as the executable.
Here's a link with more information about the loader.

Cannot run an executable binary file on another Linux System?

I'm using Ubuntu 10.04 and Qt4.6, and I've created an executable binary file on my own computer through QtCreator.
Now I want to put my executable file on CentOS 5, but it seems that this executable file cannot run on CentOS.
The error message is
bash: ./[filename]: cannot execute binary file
Now I know this comes from 32-bits and 64-bits problem, and successfully create 32-bit exexutable file.
However, this executable file still cannot run on CentOS because of the dynamic linkage problem, it always shows that :
Error while loading shared libraries: libQtGUI.so.4: cannot open shared object file: No such file or directory
I tried to add the "-static" flag on .pro file
QMAKE_CFLAGS_RELEASE += -Os -static
QMAKE_CPPFLAGS_RELEASE += -Os -static
QMAKE_CXXFLAGS_RELEASE += -Os -static
QMAKE_CCFLAGS_RELEASE += -Os -static
however, looks like that it only generate "static binary" but not "static linked", the dependency still exists.
I also tried to add following line on .pro file:
QMAKE_LFLAGS += static
But this project cannot compile after doing this.
I don't have permission to install Qt on Cent OS, how can I compile this project with static linkage so that the executable file can run independently?
Thanks for your help!
Check 64-bit vs. 32-bit - file(1) is your friend here. Then check what libraries are missing with ldd(1).
Edit:
Take a look at this SO question Qt static linking and deployment.
There could be a handful of reasons for your executable not being able to run. However, check the dependencies first with "ldd" to get a clue.
In general it's always a bad idea to run an executable from one distro on another. Apart from the architectural differences (32 vs 64 bits) you may also run into libraries incompatibilities, ABI changes, and other fun stuff. You can get rid of the libraries problem by compiling a static binary, but this comes with other drawbacks.
You should consider distributions as systems of their own, regardless of the fact they are all based on the Linux kernel, and compile binaries for each of those you want to support. The OpenSUSE Build Factory may help you if your goal is to provide binary packages.