20

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.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DavidColson
  • 8,387
  • 9
  • 37
  • 50
  • 1
    Yes, "low-level" libraries might not depend on other libraries to do their job. For instance, The [C++ Standard Library](http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library) only depends on the core language and nothing else. – Frédéric Hamidi May 27 '12 at 10:24
  • 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 May 27 '12 at 10:29
  • And OS API. The core language doesn't provide IO. – Puppy May 27 '12 at 10:29
  • you might also be interested to know that `c` compilers like `gcc` are written largely in `c` ;) – violet313 May 27 '12 at 10:31
  • @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 May 27 '12 at 10:31
  • 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) `` on top of Win32 `CreateFile`, `ReadFile`, `WriteFile` APIs or even on top of C's `fopen`, `fread`, etc. – CB Bailey May 27 '12 at 10:37
  • @Charles, understood. I stand corrected then. – Frédéric Hamidi May 27 '12 at 10:43
  • @violet313 not anymore, gcc recently switched to c++ for its implementation. – josefx May 27 '12 at 10:43
  • 1
    @josefx: not true, it's C compliable as C++, else `--disable-build-[poststage1-]with-cxx` wouldn't work. –  May 27 '12 at 10:45
  • @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 (http://en.wikipedia.org/wiki/GNU_Compiler_Collection#Development), this makes the gcc source incompatible with c. – josefx May 27 '12 at 11:10
  • 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 May 27 '12 at 11:55
  • Thanks for all the info guys! – DavidColson May 27 '12 at 13:42
  • @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 May 27 '12 at 18:01
  • Also CPU specific ASM for implementing longjmp and context set of C functions – J. M. Becker Sep 27 '16 at 21:57

6 Answers6

21

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
josefx
  • 15,506
  • 6
  • 38
  • 63
8

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?

Yes.

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++.

No.

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:

blah.c:

/*-----------------------------------------------
blah.c
Invokes some assembly
------------------------------------------------*/
void _start() 
{
    char sz[]={"oOoook\n"};
    foo(sz,sizeof(sz));
    byebye();
}

foo.asm:

;----------------------------------------------------------------------------
;Some foo assembly: exposes system write & exit
;----------------------------------------------------------------------------  

    SECTION .text
    global  foo
    global  byebye

foo:
    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
    ret

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

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
oOoook
$


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.

Community
  • 1
  • 1
violet313
  • 1,912
  • 1
  • 15
  • 19
6

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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gnud
  • 77,584
  • 5
  • 64
  • 78
  • And you can't even code for Cocoa in (pure) C++. (Stupid Apple.) – Seth Carnegie May 27 '12 at 14:49
  • @SethCarnegie I think you are a bit mistaken. Take a look at this: http://stackoverflow.com/questions/10289890/how-to-write-ios-app-purely-in-c/10290255#10290255. Obviously for C++ (which allows you to code in C), the idea remains the same. – Richard J. Ross III May 27 '12 at 14:58
  • 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 May 27 '12 at 15:02
3

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
  • 62,238
  • 13
  • 100
  • 144
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Darren
  • 68,902
  • 24
  • 138
  • 144
0

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.

old_timer
  • 69,149
  • 8
  • 89
  • 168