Libraries such as OpenGL access the graphics card and can produce graphics programs, how does these libraries access the graphics card since they are implemented using C. According to what i've heard, C and C++ do not provide graphics features built in the language and producing graphics requires libraries. How then are these libraries written in C? The same question applies for sound also?
Are additional features to C/C++ languages such as graphics, sound, internet access written in lower level languages and then provided to the C/C++ using libraries?
I would be thankful for any summary which correct my concepts, or any suggested readings on the web or books.
If I am not mistaken, you question boils down to how does one use C to access hardware. The answer, in a very general and hand wavy fashion, is that peripheral hardware is often mapped into the address space and accessed as if it is normal memory with the caveat that it can change unexpectedly.
C does a great job of accessing memory so it is totally suitable for reading and writing directly with hardware. The
volatile
keyword is there to explicitly stop the compiler from taking short cuts and force querying the addresses in question. This is because memory mapped IO addresses can change unexpectedly and do not behave like normal RAM.On a simple system this low level memory access to set addresses will be abstracted into a more easily usable library. On modern operating systems, the kernel must mediate access to shared resources like a graphics card. This means that the kernel will implement the memory mapped IO but also put in place a series of system calls (often using Swiss Army Knife constructs like
ioctl
) so that a user process can gain access to the peripheral. This in turn will be wrapped in a nice user-friendly library like OpenGL.OpenGL is not really a library. It's a specification. The opengl32.dll or libGL.so you have on your system are merely very thin layers, that communicate with the GPU driver.
The operating system offers functions to talk to the hardware. A driver uses those facilities to drive (hence the name) the hardware. For example writing a sequence A, B, C, D may make some particular GPU draw a triangle to the framebuffer.
I explained those things already in On Windows, how does OpenGL differ from DirectX? and here How does OpenGL work at the lowest level? (I'm including a verbatim quote here):
Update
To answer, how one interfaces the actual hardware from a C program say a OS kernel and/or driver written in C:
The C standard itself treats addresses as something purely abstract. You can cast a pointer to uintptr_t, but the numerical value you get is only required to adhere to pointer arithmetic if cast back to the pointer. Otherwise the value may be unrelated to address space. The only safe way to implement hardware access to C is by writing the lowest level stuff in assembly, following the ABI of the used C implementation and system.
That's how all proper OS do it. Never addresses are cast into pointers in C! The operating system has implemented this in assembler, matched to the C compiler of the system. The code written in assembly is exported as functions callable in C. The kernel provides those functions then to the GPU driver. So the chain is:
Application Program → [OpenGL API layer → Vendor OpenGL implementation] → GPU driver → OS kernel → Hardware.