how does Fiasco achieve C++ support

Adam Lackorzynski adam at os.inf.tu-dresden.de
Fri Jun 26 00:43:18 CEST 2015


On Thu Jun 25, 2015 at 16:58:04 -0400, Yuxin Ren wrote:
> My big question is how Fiasco can support C++?
> I think in order to build Fiasco, we need some kind of c++
> cross-compiler (in this case, it is gcc), this is because we build
> Fiasco and its applications on Linux.
> But on the other hand, I did not see any special configuration or
> modification of gcc when I build Fiasco.
> I want to know how Fiasco is build without a cross-compiler.

You do not need a different compiler. One of C++'s features is that it
does not require a run-time, it can run bare metal. Restrict the
features you use and it has the same requirements as plain C, i.e. few
to none (i.e. except a few libc functions e.g.).

> In particular, I know gcc uses some its internal libraries to support
> exception handing, threads/locking and STL in C++. And those libraries
> may include libgcc, libstdc++.
> But I also do not find that hose libraries are ported to Fiasco.
> Without poring those libraries, how to support C++ in Fiasco?
> As those libraries may use some lock mechanisms like mutex and C
> library functions like malloc, I cannot understand how
> Fiasco provides such support (locks, C library) to those libraries.

Functionality such as STL, threads, mutexes etc indeed require
additional libraries such as libstdc++. But if you do not use this
functionality you do not need libstdc++. Just try it. Write a simple C++
program t.cc like this

#include <cstdio>

class Foo
{
public:
  Foo() { printf("Hi\n"); }
};

int main(void)
{ 
  Foo foo;
  return 0;
}

and compile it with this. The -nostdlib is the key here to prevent
defaults being used.

g++ -static -O2 -nostdlib $(gcc -print-file-name=crt1.o) $(gcc -print-file-name=crti.o) t.cc -lc -lgcc -lgcc_eh -lc $(gcc -print-file-name=crtn.o)

That should work.

Now change to some more C++'ish thing:

Foo *foo = new Foo();

Doesn't compile/link anymore, because there's no new and delete
operator. Now we could implement them ourselves (as Fiasco does). Or we
just use libstdc++:

g++ -static -O2 -nostdlib $(gcc -print-file-name=crt1.o) $(gcc -print-file-name=crti.o) t.cc -lstdc++ -lc -lgcc -lgcc_eh -lc $(gcc -print-file-name=crtn.o) 

and it works again.




Adam
-- 
Adam                 adam at os.inf.tu-dresden.de
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/




More information about the l4-hackers mailing list