Brain Buster

I wonder something, and I am more then sure that most of you wonder this also.

How are libraries created, Obviously with header file's and .cpp file's, classes. But is that all to it?. Libraries like OpenGL or SMFL do not seem to be that simple. Meaning that libraries have something more to them. Is it a hybrid between c++ and .NET or some other language that enable's them to manipulate c++ at such a high level?. For example SMFL has the ability to deal with sound and 3d thing's. Is that all just c++ libraries and function's compiled in a certain manner or is there something more to it?.

Please any information on understanding this conundrum would help.
Last edited on
Basically it is a bunch of ifdefs that compile to different things on different systems. For example, on Windows they will use Window's functions and API calls to do their work, but if you compile on something else will use that system's API calls etc to do it.

Basically they are just wrapping the code up for you so you don't have to care which system you are doing it on.
Im confused.
So it's all c++ and API?
So Development on thing's like OpenGL on a windows pc with a IDE of c++ would use libraries that translate the c++ code into some sort of interface with Windows 32 API?.
No, it's called conditional compilation. On Windows, only the code that uses Windows functions gets compiled while on other systems the Windows code doesn't get compiled.
I don't think that you and those who have responded so far are looking at the same page from the same angles...


Yes, a C++ library is really just header and .cpp files.


No, a C++ library is not an OS. Everything you can do--for example, play a sound--is only doable because there exist OS functions you can call to do that. Every OS is different, and some are easier to do things with than others. The C++ libraries (like SMFL) are designed to hide all that from you and make life easier.

For example, in my "Clear the Screen" Article, I present two ways to clear the screen: one for Windows and one for POSIX (Unix, Linux, Mac OS X).
http://cplusplus.com/articles/4z18T05o/#OSSpecificWays

Obviously, if I wanted to create a library to do this stuff, the user won't want to have to do anything to it to make it compile equally easily on Windows and on Linux. It should "just work".

Making it "just work" is where all the macros and makefile stuff comes in, so that choosing to compile and link with the correct code is done automatically for the people using your library.

The comments at the end of the Article address this very issue, by saying to compile a different .cpp depending on what OS you are compiling.
http://cplusplus.com/articles/4z18T05o/#HeaderFiles

You can also use macros to identify what code to compile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// ClearScreen.hpp
// #include <disclaimer.h>

#ifdef _WIN32

  // Windows version here
  #include <windows.h>

  void ClearScreen()
    {
    // Windows code goes here
    }

#else

  // POSIX version here
  #include <unistd.h>
  #include <term.h>

  void ClearScreen()
    {
    // POSIX code goes here
    }

#endif 

When compiling under a Windows environment, the _WIN32 macro is automatically defined, so only the stuff on lines 5..13 get compiled. Otherwise only the stuff on lines 15..24 get compiled.

Hope this helps.
Topic archived. No new replies allowed.