How are low-level libraries made?



When I go and make a C++ application I generally use libraries like SDL or WxWidgets and so on. But if I were to make a library would I need to use a library to make a library? Or can I make the entire library out of core C++ code, is this even possible?

My point is that there must be a point were a library has nothing to base itself on and so the only things it can use are core C++.

Am I right with this theory? If not, how are low-level libraries really made?

(I know this is a broad question, but I am a very curious person that needs answers and this is something that has bothered me.)


Posted 2012-05-27T10:14:42.183

Reputation: 3 255


Yes, "low-level" libraries might not depend on other libraries to do their job. For instance, The C++ Standard Library only depends on the core language and nothing else.

– Frédéric Hamidi – 2012-05-27T10:24:17.550

12@FrédéricHamidi: That's not quite true. Some parts of the C++ standard library depend on operating system facilities so depend on OS provided APIs. These facilities are effectively providing an abstraction for an OS service (e.g. std::fstream, std::exit). – CB Bailey – 2012-05-27T10:29:34.897

And OS API. The core language doesn't provide IO. – Puppy – 2012-05-27T10:29:40.090

you might also be interested to know that c compilers like gcc are written largely in c ;) – violet313 – 2012-05-27T10:31:11.597

@Charles, ah, good point, I didn't think about that. But can system calls actually be considered as library dependencies? I mean, you pretty much always need them... – Frédéric Hamidi – 2012-05-27T10:31:21.820

2@FrédéricHamidi: Well, they are a dependency even if not a library dependency but the standard C++ library doesn't have to be built directly on system calls. You could build (for example) <fstream> on top of Win32 CreateFile, ReadFile, WriteFile APIs or even on top of C's fopen, fread, etc. – CB Bailey – 2012-05-27T10:37:26.823

@Charles, understood. I stand corrected then. – Frédéric Hamidi – 2012-05-27T10:43:29.880

@violet313 not anymore, gcc recently switched to c++ for its implementation. – josefx – 2012-05-27T10:43:52.247

1@josefx: not true, it's C compliable as C++, else --disable-build-[poststage1-]with-cxx wouldn't work. – Fanael – 2012-05-27T10:45:19.377

@Fanael I do not know what that option does or when it can be used. However from what I have read gcc code may contain a specific set of c++ features (, this makes the gcc source incompatible with c.

– josefx – 2012-05-27T11:10:39.660

3@FrédéricHamidi: In fact, C++'s I/O subsystem is based on C's stdio library (since the C library is part of the C++ library, you might argue that this is not actually a dependency on a different library). The usual call sequence for I/O is thus C++ → C → operating system, where the "operating system" part is C stdio → POSIX I/O functions → system call → kernel on Linux and C stdio → Windows API → Native API → system call → kernel on Windows. – Philipp – 2012-05-27T11:55:15.793

Thanks for all the info guys! – DavidColson – 2012-05-27T13:42:21.427

@Fanael I checked again and it appears you are right, at least the functionality required for the c compiler does not use c++.(In my defense there where several articles claiming that gcc "moved" to c++ when in reality they only allowed use of c++ code) – josefx – 2012-05-27T18:01:23.130

Also CPU specific ASM for implementing longjmp and context set of C functions – J. M. Becker – 2016-09-27T21:57:04.003



Low level libraries access hardware and system resources through libraries provided by the operating system. The operating system itself and the drivers loaded by it use assembly and reads/writes of predefined memory addresses to modify CPU state and communicate with hardware.

A library that only depends on C++ can only be a utility library as any communication with the hardware or user would involve either assembly or an additional library. An example for a pure C++ library without dependencies would be a math library, as it doesn't require I/O or hardware access.


Posted 2012-05-27T10:14:42.183

Reputation: 13 184


When I go and make a C++ application I generally use libraries like SDL or wxWidgets and so on. But if I were to make a library would I need to use a library to make a library? Or can I make the entire library out of core C++ code, is this even possible?


My point is there must be a point were a library has nothing to base itself on and so the only things it can use are core C++.


C & C++ are examples of third-generation languages. As such they attempt to hide many system-wise implementation details. Underlying the third-generation programming language (3GL) layer is a second-generation programming language (2GL) such as x86 assembly; below this we have machine code instructions.

As has already been pointed out by josefx, you can indeed implement a C++ library in pure C++. However, system libraries (especially those for controlling devices) can be and often are written in assembler. And in general, a library could also be written in COBOL or Fortran or even Brainfuck.

It's the assembler/compiler/linker processes that actually convert 2GL/3GL codes into a library that can be linked to in C++.

Here is a small example of a C program, blah.c, that links to no standard runtime libraries, but it still manages to print stuff out using a function in a *proprietary library, libfoo.a, written in x86 assembler:


Invokes some assembly
void _start() 
    char sz[]={"oOoook\n"};


;Some foo assembly: exposes system write & exit

    SECTION .text
    global  foo
    global  byebye

    push    ebp
    mov     ebp,esp

    mov     edx, [ebp+12]
    mov     ecx, [ebp+8]
    mov     ebx,1
    mov     eax,4               ;syscall for __NR_write
    int     0x80                ;service interrupt               

    mov     esp,ebp
    pop     ebp

    mov     eax,0

    mov     ebx,0
    mov     eax,1               ;syscall for __NR_exit
    int     0x80                ;service interrupt               

And here's how it's built:

$ nasm -felf -ofoo.o foo.asm
$ ar rs libfoo.a foo.o
$ gcc  -L. -oblah blah.c -lfoo -nostartfiles -nostdlib -nodefaultlibs
$ ./blah

As can be seen, the library makes use of system calls (in this case to the linux kernel); which incidentally, may also be implemented by third-parties.


Posted 2012-05-27T10:14:42.183

Reputation: 1 482


SDL and wxWidgets are libraries that abstract the details of the underlying system(s).

To provide similar functionality (graphics, sound, input), you'd need to code directly against the Windows API on Windows, Cocoa on Mac OS X, and on Unix, directly against the POSIX API and X11 (using XCB, Xlib or the raw X11 protocol).


Posted 2012-05-27T10:14:42.183

Reputation: 60 023

And you can't even code for Cocoa in (pure) C++. (Stupid Apple.) – Seth Carnegie – 2012-05-27T14:49:03.363

@SethCarnegie I think you are a bit mistaken. Take a look at this: Obviously for C++ (which allows you to code in C), the idea remains the same.

– Richard J. Ross III – 2012-05-27T14:58:45.503

2@RichardJ.RossIII I mean there's no first-class interface. Like the guy in the comments said, "in order to avoid learning Objective-C, you now have to learn the implementation details and C-level API of the Objective-C runtime", which is rubbish. – Seth Carnegie – 2012-05-27T15:02:12.903


There are three kinds of libraries:

  1. libraries that does system calls
  2. libraries that implements foreign function call
  3. libraries that can be written or rewritten in core C++ (e.g. excluding asm extension)

Libraries that does system calls generally can't be written in the language itself, as it is dependant on the particularities of the system call convention, which usually involve making a trap or interrupt to the kernel, to cause a switch into a lower privilege ring. There is no facility in pure C++ to generate a trap/interrupt; the OS API typically uses assembly extensions to do so.

The second category is libraries that makes or receives a foreign calling conventions; arguably system call is a subcategory of this, but system call is distinctly different in that system call requires privilege ring switch, while foreign calls do not. Foreign calling convention is also normally written in assembly.

All other libraries that doesn't involve ring switch or foreign calling convention can always be written in C++ itself; although sometimes they aren't, usually for performance purpose (e.g. the math library falls under this category).

In any case, libraries that falls under the former two categories usually also have a large body of code written in pure C++ that does nothing but wraps the API of the system call/foreign call into a form that is more familiar to C++ tools and programmers (e.g. header files, and other convenience utilities).

Lie Ryan

Posted 2012-05-27T10:14:42.183

Reputation: 43 931


At the very core, libraries are just collections of classes. They can be based upon other libraries to extend them or provide the core to a new library entirely. You could create your own library just using C++ code, for example, a graphics library that takes advantage of the Windows API, a chat/communications library that extends the socket libraries available, etc. You are correct in saying that the libraries use core C++ functionality.


Posted 2012-05-27T10:14:42.183

Reputation: 52 929


Libraries are nothing more than other code you can link your code with. there is nothing magical or special about them. You can take functions that you have written and turn them into a library. So the question really is can you do low level stuff only C++ or do you have to so something else? Well that is where Lie Ryan's answer comes in. You can write your program, do your thing, with just C++ code only. You can make system calls, and you can tie in other languages (assembly for example) that do things you cant do in C++ but need to do.

In the end this all boils down to machine code and hardware. Any language or method desired or required to get that machine code or something functionally the same, is within the realm of possibilities. perhaps not always the smartest thing to unnecessarily use asm or not use a system call, etc.

There are large quantities of open source libraries that are similar to the ones you are talking about where you can simply just look at the code to see how they did it. And there is nothing that says the way one entity implemented something is the only way to do it.


Posted 2012-05-27T10:14:42.183

Reputation: 48 041