0

I know that these are required to compile a C++ app but what I don't know is how do I build my app so that other users won't need them. I tried to use -static flags to build but it still won't work when I remove mingw\bin\ and msys2\usr\bin\ from my path or when my friends who don't have a C++ compiler try to run it. For the record, I did include every library for the project when I asked for friends to run it.

Here's my Makefile :

rtx.exe: base.o objects.o rtx.o
    g++ -O3 base.o objects.o rtx.o -o rtx -pthread -Lsrc\lib -lsfml-graphics -lsfml-window -lsfml-system -ljsoncpp -static -static-libgcc -static-libstdc++

rtx.o: rtx.cpp
    g++ -Isrc\include -O3 -c rtx.cpp -static -static-libgcc -static-libstdc++

objects.o: objects.cpp objects.hpp
    g++ -Isrc\include -O3 -c objects.cpp -static -static-libgcc -static-libstdc++

base.o: base.cpp base.hpp
    g++ -Isrc\include -O3 -c base.cpp -static -static-libgcc -static-libstdc++

clean:
    -rm *.o $(objects) rtx.exe

And here's one of the pop-op I (and my friends withour a C++ compiler) get :

There are 4 pop-ups, 2 of them being this one and the other says the same thing for libstdc++-6.dll.

I tried a bunch of things, including compiling objects.o and base.o into a library using the ar ru command but it gives the same pop-ups.

My guess is that one of the libraries I'm using, either jsonCpp or SFML is not built statically, but I couldn't find anything about how to fix it.

Leroy
  • 62
  • 8
  • `-static` is a good start. `-static-libgcc -static-libstdc++` are unnecessary, since `-static` implies them. The fact that you mention `msys2\usr\bin` is very suspicious, since it shouldn't be involved in your build process at all. It's only use is for MSYS2's simple utilities ported from Linux, like `grep`, etc. Same for `mingw\bin` - is it from some other MinGW version? Don't mix them with MSYS2. – HolyBlackCat Jul 11 '22 at 19:56
  • The problem is with the 3rd-party libraries you use: SFML and Jsoncpp. You shouldn't be hardcoding the flags for them, get them from `pkg-config --libs --static sfml-all jsoncpp`, then everything should just work. That's assuming you installed them from MSYS2 packages. – HolyBlackCat Jul 11 '22 at 19:57
  • @HolyBlackCat Well, when I remove both these directories from my path, I can't build and I can't run the exe, that's all I know. – Leroy Jul 11 '22 at 20:05
  • And about MSYS2 packages... For SFML I can't remember because I installed it one year ago, but I know I built jsoncpp from source. The command you gave returns ` Package sfml-all was not found in the pkg-config search path. Perhaps you should add the directory containing `sfml-all.pc' to the PKG_CONFIG_PATH environment variable Package 'sfml-all', required by 'virtual:world', not found Package 'jsoncpp', required by 'virtual:world', not found ` Do you happen to have a link to some sort of tutorial by any chance? – Leroy Jul 11 '22 at 20:06
  • FYI, I can create a C++ file without those DLL files, using Visual Studio (Microsoft's compiler). – Thomas Matthews Jul 11 '22 at 20:06
  • Start from scratch. Remove all MinGW or MSYS2 versions you might have, and you self-built libraries. Install MSYS2 as described [here](https://stackoverflow.com/q/30069830/2752075). Pay special attention to what executable you use to open MSYS2 terminal as described in the link. Install packages `mingw-w64-x86_64-jsoncpp mingw-w64-x86_64-sfml`, in addition to what's described in the link. Then it should work. Building libraries yourself is also an option, and pkg-config should work there too, assuming you set some of its env variables to point at the build location, but it's trickier. – HolyBlackCat Jul 12 '22 at 04:01
  • Thanks, that might be the most complicated fix I'll ever had to do... – Leroy Jul 12 '22 at 17:00

2 Answers2

0

If you want to get rid of shared runtime libraries, you have to ensure that all your dependencies have linked the runtime statically.

SFML is by default not built with static runtime libraries, so you'll certainly have to rebuild SFML with SFML_USE_STATIC_STD_LIBS set in CMake. Similarly I assume for jsoncpp.

Lukas
  • 2,461
  • 2
  • 19
  • 32
0

So, as advised by HolyBlackCat, I fully reinstalled MSYS2 and downloaded SFML and jsonCpp with mingw and after a bit of research and trial and error I ended up with this makeFile :

rtx.exe: base.o objects.o rtx.o
    g++ -O3 base.o objects.o rtx.o -o rtx -pthread -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lopengl32 -lwinmm -lgdi32 -ljsoncpp -static

rtx.o: rtx.cpp
    g++ -Isrc\include -O3 -c rtx.cpp -DSFML_STATIC -static

objects.o: objects.cpp objects.hpp
    g++ -Isrc\include -O3 -c objects.cpp -DSFML_STATIC -static

base.o: base.cpp base.hpp
    g++ -Isrc\include -O3 -c base.cpp -DSFML_STATIC -static

clean:
    -rm *.o $(objects) rtx.exe

And now, it does make a static build.

Leroy
  • 62
  • 8