0

I am writing a C++ kernel for my own hobby operating system. I work on a mac, and compiled a working (until now) cross-compiler out of gcc version 4.8.2.

Currently I have a kernel.cpp file, containing only a main() function and when I compile and link only this file, everything works fine. However, when I add a class, in a different file, the linker starts complaining:

/opt/cross/gcc-i686-elf/lib/gcc/i686-elf/4.8.2/../../../../i686-elf/bin/ld: cannot find -lstdc++
/opt/cross/gcc-i686-elf/lib/gcc/i686-elf/4.8.2/../../../../i686-elf/bin/ld: cannot find -lm
/opt/cross/gcc-i686-elf/lib/gcc/i686-elf/4.8.2/../../../../i686-elf/bin/ld: cannot find -lc
/opt/cross/gcc-i686-elf/lib/gcc/i686-elf/4.8.2/../../../../i686-elf/bin/ld: cannot find -lc

I found out that these are all missing standard libraries, right?

One interesting detail is, that when I add the same class to the kernel.cpp instead of to a new file, it works like a charm.

Can anyone help me with where I should find these libraries, and how I can get them if they are not available? Should they have come with the cross-compiler or should they have already be available on my macOS?

This is how I build my code:

i686-elf-g++ -Wall -ffreestanding -m32 vga/vga.cpp kernel.cpp -o kernel.bin

Any help is welcome, thanks!

Pieter
  • 103
  • 4

1 Answers1

1

If you write your own kernel you are on your own, that's sort of the point. You'll need to write system calls before you can write (or port) a standard library.

Add a -ffreestanding flag to tell the compiler that you don't have a standard library.

Edit: add -nostdlib to tell the linker that you don't have a standard library and -lgcc as the compiler might make calls to libgcc (this library is provided by your cross compiler). -m32 is unnecessary as your cross compiler is made with a 32 bit target

  • Thanks Sjur, I knew I was on my own. I already pass the --ffreestanding flag to the compiler. I updated the question with the commands I execute to build and link the code. – Pieter Jan 15 '17 at 13:56
  • [Found this as osdev wiki.](http://wiki.osdev.org/Why_do_I_need_a_Cross_Compiler%3F#Options_that_you_should_pass_to_your_Compiler) You should also add -nostdlib and -lgcc. – Sjur Nilsen Haga Jan 15 '17 at 19:45
  • Thanks, -nostdlib did the trick! Also thanks for pointing out this page on OSDev. I have seen a lot of their pages in the search for an answer, but I did not find this one. – Pieter Jan 16 '17 at 06:58