Cross compiler not recognizing freestanding library

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Kaye
Posts: 2
Joined: Mon Jul 23, 2012 7:30 pm

Cross compiler not recognizing freestanding library

Post by Kaye »

Hey all,

This is my first shot at OS dev, so hopefully this questing hasn't been answered a thousand times before (I searched and was unable to find anything, both in the forums and on Google) even though I'm sure it's a relatively quick fix.

I'm trying to follow the first bare bones tutorial just to get a simple kernel up an running to use as a starting point as I try to learn more about OS development and what I'll need to do to build my own OS. I'm having a problem compiling the actual kernal.c file using the "naked" cross compiler that I built follow the instructions from the GCC Cross-Compiler wiki page. The command I'm attempting to run is right off of the bare bones wiki page:

Code: Select all

i586-elf-gcc -o kernel.o -c kernel.c -Wall -Wextra -Werror
    -nostdlib -fno-builtin -nostartfiles -nodefaultlibs
And the error message I'm receiving is as follows:

Code: Select all

Jordan@Jordan-PC ~/Kernel
$ $TARGET-gcc -o kernel.o -c kernel.c -Wall -Wextra -Werror -nostdlib -fno-builtin -nostartfiles -nodefaultlibs
In file included from kernel.c:1:0:
/usr/local/cross/lib/gcc/i586-elf/4.7.1/include/stdint.h:3:26: fatal error: stdint.h: No such file or directory
compilation terminated.
From the research I was able to do, I think that the problem is in my cross compiler, but I can't figure out where. I built it using GCC 4.7.1 and binutils 2.22 which should work together according to the chart on the wiki, and I really don't have the experience necessary to solve this (probably trivial) problem at this point. Does anyone have any insight into this (and perhaps good methods as to how I should go about solving issues like this in the future - it's not really like debugging code :P )

Thanks.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Cross compiler not recognizing freestanding library

Post by Love4Boobies »

See this. I can see a lot of the posts in that thread were deleted but you can still get the information you want.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Cross compiler not recognizing freestanding library

Post by Owen »

You need a magic incantation: -ffreestanding. GCC stdint.h looks for __STDC_HOSTED__, and if it exists, forwards on to your C library

-nostdlib, -nostartfiles and -nodefaultlibs are applicable only when using GCC to link your binary (and I'd assume you're using ld for that, since for a kernel you generally want the extra control).

-fno-builtin is unnecessary generally, and inhibits optimizations. Without it, if you call "memset(&x, 0, sizeof(x))" and sizeof(x) is 4 or such, can replace that with a "mov [x], 0" or similar instruction saving quite a bit of overhead.

You may wish to link against libgcc (the correct one of which can be determined by invoking "gcc -print-libgcc-file-name" with the same CFLAGS as you compile your sources with). This provides various support routines that GCC will emit calls to for various operations. These operations are rare on x86 (the main one you're likely to encounter is a 64-bit division on ia32), but quite common on other architectures; libgcc also provides exception handling support (relevant if you're building a C++ kernel)
Kaye
Posts: 2
Joined: Mon Jul 23, 2012 7:30 pm

Re: Cross compiler not recognizing freestanding library

Post by Kaye »

Thanks for the replies! The -ffreestanding flag solved my problem. I also omitted the flags you had mentioned just to be thorough, everything has worked fine without them as you said.

Time to move on to the linker!
Post Reply