Compiling works, starting not - "standard path"? - c++

I want to use SFML with C++ under Ubuntu OS. I create two debug/release shared Libarys with cmake (by this tutorial)
I can compile and link my test application without giving any information about a path. So I think everything is alright with the standard path
g++ -c main.cpp
g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system
but when I start now my application with ./sfml-app it said
./sfml-app: error while loading shared libraries:
libsfml-graphics.so.2: cannot open shared object file: No such file or
directory
this confusing me. Because I think /usr/local/lib is the standard path and when I add this path during compiling
g++ main.o -o sfml-app -L /usr/local/lib -lsfml-graphics
-lsfml-window -lsfml-system
it works. But it should work also without giving information with -L
So what could be the Problem? I have made this before reinstalling Ubuntu. And on my old system it works well, can start my application by ./ terminal and also double clicking.
when I add in the console
export LD_LIBRARY_PATH=$PATH:/usr/local/lib
I can start the application from the console. but only in the specific one. I want the application starts always also by double click (not only from terminal). How can I add the Libary "systemwide" ?

You need to add /usr/local/lib to a file in /etc/ld.so.conf.d/ and then call ldconfig to rebuild its cache.
One way of doing it:
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/usrlocal'
sudo ldconfig
Then you should be able to compile without -L /usr/local/lib nor use export LD_LIBRARY_PATH=$PATH:/usr/local/lib.

Related

The applications I made with C++ work on my own computer, but on different computers it gives libstdc++-6.dll not found ,libgcc_s_dw2-1.dll not found

The applications I made with C++ sfml works on my own computer, but on my friends computers, it gives libstdc++-6.dll not found and ligcc_s_dw2-1.dll not found errors. I'm using visual studio code. I want applications to be opened on my friends computers without installing mingw to my friends computers, what can I do?
My makefile :
all: compile link run
compile:
g++ -I src/include -c main.cpp
link:
g++ main.o -o main -L src/lib -mwindows -l sfml-graphics -l sfml-window -l sfml-system -static-libgcc -static-libstdc++
run:
main

How to compile to SDL2 application to Windows from Linux?

So recently I downloaded the Linux Subsystem on Windows 10, with Ubuntu.
I can compile an SDL2 app to Linux with the g++ command but whenever I try doing it with i686-w64-mingw32-g++ this command, I get an error saying main.cpp:5:9: fatal error: SDL2/SDL.h: No such file or directory.
The command I'm using is i686-w64-mingw32-g++ main.cpp -w -lSDL2 -o main.exe.
https://imgur.com/a/uqcGCoJ
Anyone knows how to fix this? :(
[EDIT]
So now I've tried specifying the directory of the necesary files with this command: g++ main.cpp -I/usr/include/SDL -L/usr/lib/x86_64-linux-gnu -w -Wall -Wextra -std=c++17 -lSDL2 -o main
which worked but when I use it with mingw it doesn't i686-w64-mingw32-g++ main.cpp -I/usr/include/SDL -L/usr/lib/x86_64-linux-gnu -w -Wall -Wextra -std=c++17 -lSDL2 -o main
https://imgur.com/a/sF6CpcP
You need to include the path to SDL's include directory on the command line. However, you need to include the path to the downloaded SDL for mingw32, not /usr/include/SDL2. The difference is the headers in /usr/include/SDL2 are for Linux and libs in /usr/lib are also for Linux, but you need to link to the Windows libraries.
What I usually do is download the development libraries for Mingw32 and put them directly into my project directory. Then all you need to do is add -ISDL2-2.0.8/i686-w64-mingw32/include -LSDL2-2.0.8/i686-w64-mingw32/lib to your command line and it will be able to find the headers and libraries it needs. Finally, make sure you copy SDL2-2.0.8/i686-w64-mingw32/bin/SDL2.dll to your executable directory in the Makefile.
Also, remember to link SDLmain as well. It handles creating a WinMain for you and all that, and then calls your main function.

Running a SFML C++ program with minGW on Windows 10

So I'm trying to run an example SFML program on my Windows laptop. If relevant, the source code is on this page.
So first I make the .o file using this command -
g++ -c a.cpp -ISFML/SFML/include
Where a.cpp is the main file, and my SFML package is located in SFML/SFML.
Then I compile using this command -
g++ a.o -o a -LSFML/SFML/lib -lsfml-graphics -lsfml-window -lsfml-system
When I first ran the program I got the errors about not being able to find certain dlls, sfml-graphics-2 etc. So I found them and put them next to the exe. But now when I run, I get this weird error:
The procedure entry point
_ZNSt7__cxx1112basic_stringSt11char_traitsIcESalcEE7reserveEj could not be located in the dynamic link library.
What is going on here?
As the SFML download page states, You could be using the wrong version of the compiler, other library versions of SFML that you have not removed from your working directory that could mismatch between code and linker. Worst case, if your compiler is not listed there, you have to compile SFML yourself:
Get CMake. Get the source code for 2.4.2 by going to the bottom of the SFML download page. Follow this guide on SFML's GitHub repo. Alternatively, you could use the guide on SFML's page but it is for an older version. It might answer some questions that the first guide misses.
Ones CMake have generated the makefiles, you're on your way to build SFML.
Good luck!
I've had this problem for so long so I just wanted to help someone out who had the same problem. I have a windows 10 FYI and MinGW-w64 8.1.0 (if it doesnt work try a 32 bit mingw instead)
for a debug mode (debug is when your still working on the game and would like command prompt to open whenever you run it)
(make sure your in the right directory first by doing "cd")
g++ -c (file_name).cpp -o (file_name).o -I(path_to)SFML-64-bit/include -g -m64 -Wall &&
g++ (file_name).o -o (game_name).exe -L(path_to)SFML-64-bit/lib -lsfml-graphics -lsfml-window -lsfml-system
The code above when placed in command will compile everything for you if its all in the same directory so make sure you keep an eye out for that
and now for release mode (if you dont want command prompt to show up)
g++ -c (file_name).cpp -o (file_name).o -I(path_to)SFML-64-bit/include -O3 -m64 &&
g++ (file_name).o -o (game_name).exe -L(path_to)SFML-64-bit/lib -lsfml-graphics lsfml-window -lsfml-system -mwindows
Noticed all I added was the -mwindows and the -O3 aswell as removing -g and -Wall which are not necessary since we wont be using command prompt
Make sure to go to SFML/bin and take all the .dlls and put it into the same directory has your .exe sorry xd
Hope this helped.

#executable_path not working

I have installed SDL2. I can link my program with
clang++ -lSDL2 -lv8 main.o test.o -o nano8
However, for distributing reasons, I'd like to give SDL2 away with the binary, and hence i've copied libSDL2-2.0.0.dylib under /myapp/lib/libSDL2-2.0.0.dylib
As for the documentation, #executable_path should allow me to link to that dylib instead of the one in /usr/local/lib, but if I run
clang++ #executable_path/lib/libSDL2-2.0.0.dylib -lv8 main.o test.o -o nano8
I get the error
clang: error: no such file or directory: '#executable_path/lib/libSDL2-2.0.0.dylib'
How to set the search path for a dylib?
This is a pain to get right.
I assume that /myapp can be anywhere in the filesystem, and want to load the library from #executable_path/../lib/libSDL2.dylib?
If so you have to use the Run Path and copy and modify the library during linking (this needs to be done every build):
cp /usr/local/lib/libSDL2.dylib build_dir/lib.
Change the install name of the .dylib using install_name_tool:
install_name_tool -change /usr/local/lib/libSDL2.dylib #rpath/libSDL2.dylib build_dir/lib/libSDL2.dylib
Link against build_dir/lib/libSDL2.dylib and set the rpath during linking:
cd build_dir/bin
clang++ obj_dir/main.o obj_dir/test.o -o nano8 -L ../lib -lv8 -lSDL2 -Xlinker -rpath -Xlinker "#executable_path/../lib"
I have not tested this and there is likely to be errors. Use otool -L to examine the library references in both the binary and the libraries in order to home-in on this issue.
A cleaner way:
Install your binaries into /usr/local/bin and provide any .dylibs to be installed into /usr/local/lib. This is better as /usr/local is the place for user-binaries and you don't need the faff above.

-L option not working for mingw gcc

I am trying to get mingw gcc to work.
I need it to link with libopengl32.a.
Said file exists in C:/mingw/lib.
I used g++ as follows:
g++ -L"C:/mingw/lib" main.o -o test.exe -llibopengl32.a
It has no trouble finding the includes, it just complains that it can't find the library.
It seems unable to find any other library as well.
Also: I installed all the mingw components manually by downloading them from sourceforge, since using the automatic installer produced a broken installation on my system.
The -l flag automatically adds the lib prefix and the .a extension- you want:
g++ -LC:/mingw/lib main.o -o test.exe -lopengl32
Note you don't need the quotes around the path either. You could also just specify the whole library name & path:
g++ main.o -o test.exe C:/mingw/lib/libopengl32.a
As regards your installation problems, use either http://tdragon.net/recentgcc/ or http://nuwen.net/mingw.html - using the MinGW site itself is a recipe for pain.
You need to use -lopengl32 without "lib" and ".a"