Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

differences between C++ libraries (namespaces, linking)

I'm just learning C++ and started using different libraries, like Boost and SDL. After first struggling with configuring the paths, now everything seems fine, but I still have some questions about why different libraries work differently.

  1. Why is it that while many libraries (like Boost) are separated in their namespace, there are others (like SDL) that aren't? For me, it makes more sense to keep everything separated, as one library cannot know what functions are used in other libraries. But then why is SDL not like this?

  2. Why do I have to manually set the linker settings (.lib files), for almost every library, while it's automatic for others (like Boost)? Is it because I used the BoostPro installer what somehow made this linking search automatic? Or is there some other setting what I missed? Is is possible to make looking for .lib files automatic for other libraries?

like image 247
hyperknot Avatar asked Feb 22 '26 06:02

hyperknot


2 Answers

  1. SDL was written in C, and the C language doesn't natively support namespaces. Boost is a C++-only library, and took full advantage of C++ namespaces.

  2. Boost is a mostly header-only library, so there aren't actual binaries to link most of the time. When there are binaries to link (like Boost.Thread), the headers may take advantage of compiler-specific directives that can command the linker to link to certain libraries (like VC++'s #pragma comment(lib, ...)). This feature is called "auto link" in the context of Boost. Otherwise, libraries have to be specifically mentioned to the linker.

like image 162
In silico Avatar answered Feb 23 '26 20:02

In silico


  1. Technically, SDL is a C library. Since C doesn't have namespaces, SDL doesn't use them. The fact that C++ is (mostly) backwards-compatible with C means you can use SDL in C++ anyway. Additionally, SDL utilizes the C "equivalent" of namespaces: all its functions start with SDL, effectively creating some sort of namespace.

  2. AFAIK, most boost "libraries" don't require linking, as they're header-only. I could be wrong on this though.

like image 41
You Avatar answered Feb 23 '26 19:02

You



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!