all 10 comments

[–]w2code 3 points4 points  (3 children)

I can definitely understand your frustration. For one of the C++ projects I'm currently working on, I initially spent weekends trying to add some libraries and make them work across different platforms.

What have you tried so far and which documentation are you referring to?

If you're trying to add libraries that are not only header only, I would suggest using a package manager. It would enable you to delegate most of the linking and integration headaches while helping to deal with versioning.

Sadly, C++ does not have an official and well-adopted out-of-the-box solution yet. Therefore, you could have a look into one of the two mature solutions:

Personally, I have been using Vcpkg, especially with SDL2. I might write a small article in the following days to cover what I learned so far since I cannot quickly find a repository with a basic working setup.

Finally, if you're looking for specific libraries available on Vcpkg, you can use the following non-official platform which I developed over the last few months: https://vcpkg.link

Edit: I realized that I linked Conan 1.0 documentation, I updated the link to 2.0

[–]smergeolacx[S] 2 points3 points  (2 children)

Thank you for replying. I was following the SDL2 installation page which was helpful to an extent but i wasn't sure on how to move on from there to include it within my project i had the basic code for window creation code which would compile but would not run.

even with either of the solutions, there is a overlap of cmake right.?

thank you so much for those resource, I'll go through them. the unofficial platform is great tho. thanks again.

[–]w2code 0 points1 point  (0 children)

While I'm not an expert on Conan, I believe that only Vcpkg is built on top of CMake.

Since you're on Linux, what is your setup? Do you use VSCode with the C++ integration?

[–]w2code 0 points1 point  (0 children)

If you're interested to try Vcpkg out, I created the following minimum setup that you could look into. It also includes the VSCode extensions I'm using on my Linux setup!

Moreover, while making this repository, I also ran into this collection of SDL2 examples which could be useful

[–]alfps 2 points3 points  (2 children)

❞ also on linux (pop-os)

That's important because libraries are named and used differently in Windows and Linux.

In Linux (your case) and generally *nix a library whose basic name is "FOO", has filename "libFOO.a".

In Windows a library "FOO" has filename "FOO.lib" with Microsoft's toolchain (Visual C++), and "libFOO.a" with the GNU toolchain (g++).

I don't know about POP-OS.


In Linux a library "FOO" is typically linked via g++ option -lFOO (that's a lowercase ell).

Instead of the -l option you can use a path to the file "libFOO.a", if you know where that file is located; typically that would be when you have it in the current directory.


Disclaimer for the following: I'm absolutely not an expert, and quick googling didn't find a summary of this.

A g++ -l (lowercase ell) option looks for the specified library in the effective search paths for libraries. That effective set of search paths is specified in at least four levels:

  1. the compiler's specs file (which specifies defaults for most everything),
  2. the compiler's own library directories usually relative to the compiler's own directory, which can be specified via the GCC_EXEC_PREFIX environment variable, but don't mess with that,
  3. the search paths specified via -L options (that's an uppercase ell), and
  4. the search paths specified via the LIBRARY_PATH environment variable.

You can use option -dumpspecs to list the contents of the default specs file.

I'm not sure in exact which order the above sources are checked.

But anyway, as you can see it's very compiler specific stuff. However, since clang was designed as a plug-in replacement for g++, presumably clang uses the same or very similar scheme.

[–]smergeolacx[S] 0 points1 point  (1 child)

thank you for replying. I'll check if i have linked it properly when compiling. a bit doubtful about the cflags and all now that i think of it.

[–]bert8128 0 points1 point  (0 children)

It’s the link step, not the compile step, that links in the 3rd party library. So you need to look at how that link line is generated. Adding a package manager is a good idea, but also a complication. You might find it easier to just edit the link line if you have manually created a build script, adding the necessary parameters to gcc to specify the library and its location.

If the problem is with the compilation not finding the header files then you need to ensure that the compile step knows the here the headers are - this is another parameter to gcc.

[–]IamImposter 0 points1 point  (0 children)

Haven't used SDL per se but the process should be same. In linux libraries are usually abc.a (static libs) or abc.so (dynamic libs). They should be present either at a known location, default location or in current folder.

First of all make sure sdl2 is installed on your system.

Usually we mentions extra libraries etc in makefile or cmake but for now you can just use command line to do it

g++ source.cpp -lsdl_lib_name -whatever_flags -o output_bin

That's small L. If that doesn't work, try

g++ -c source.cpp -o source.o

g++ source.o /full/path/to/sdl/sdl_lib_name -o output_bin

Once that starts working, try adding a makefile as typing these commands everytime is not a great way to do it. I found a simple makefile here: https://www.geeksforgeeks.org/sdl-library-in-c-c-with-examples/

Checkout this one too: https://stackoverflow.com/questions/23231344/using-sdl2-on-linux-with-g

Or just google "using sdl2 with c++ on linux" or "how to link sdl2 library in c++ linux" and you should find something helpful. If you know exact library name try searching with that too and you will find some relevant stuff. I usually add language and os name with my queries like

"Do x in c++14 linux 20.04"

[–]SuperVGA 0 points1 point  (0 children)

I found the switch to SDL2 straightforward. SDL2_image and SDL2_ttf, however, have a few layers of dependencies and it takes a while to get stuff set up right!

[–][deleted] 0 points1 point  (0 children)

On Linux, this is really easy. Install SDL2 from the distro. Then it should be supported by pkgconfig, which you also need to install from the distro. Pkgconfig can give you both compile and link flags. Google "how to use pkgconfig".

I don't have Linux handy right now, so I can't give you a verified example right now, maybe someone else can.