1

When installing i386-elf-gcc using aur. Install goes smoothly and no errors when I build. I added /usr/local/i386elfgcc/bin to my path and I am able to run the command. When I run a simple command such as i386-elf-gcc -g "kernel.cpp" -o "kernel.o", I am getting errors:

/usr/local/i386elfgcc/lib/gcc/i386-elf/10.2.0/../../../../i386-elf/bin/ld: cannot find crt0.o: No such file or directory
/usr/local/i386elfgcc/lib/gcc/i386-elf/10.2.0/../../../../i386-elf/bin/ld: cannot find -lg
/usr/local/i386elfgcc/lib/gcc/i386-elf/10.2.0/../../../../i386-elf/bin/ld: cannot find -lc

Reinstalling has not worked. What can I do to fix this? I am using Arch.

codenaem
  • 59
  • 8
  • Does it come with any instructions about adding library path environment variables? Also, standard AUR usage will normally make a `.pkg` file you can install, which should get things added to any necessary paths. Are you trying to use it from its build directory without installing the `.pkg`, or did you choose this path when building via AUR? – Peter Cordes Dec 04 '22 at 18:20
  • I did yay i386-elf-gcc and just installed. Should I download PKGBUILD and do that instead? – codenaem Dec 04 '22 at 18:25
  • Shouldn't you be building this as freestanding (compile with `-ffreestanding` since the C runtime and C library won't be available in your kernel unless you provide your own? – Michael Petch Dec 04 '22 at 19:09
  • The entire command I use is ```i386-elf-gcc -ffreestanding -m32 -g "kernel.cpp" -o "kernel.o"``` but even with that I get the errors – codenaem Dec 04 '22 at 19:15
  • what if you add `-nostdlib` (since you are compiling and linking all in one step which is a bit unusual. Usually you compile with `-c` to generate `.o` files and then link those to a final executable. – Michael Petch Dec 04 '22 at 19:23
  • It works! Can you explain why? – codenaem Dec 04 '22 at 19:24
  • Because when linking by default the cross compiler is still assuming that there are C libraries and C runtimes available. That doesn't happen to be the case, which is why `-nostdlib` exists. `-nostdlib` will tell the linker that it shouldn't include the C runtime or C startup files. – Michael Petch Dec 04 '22 at 19:26

1 Answers1

0

The original command was i386-elf-gcc -ffreestanding -m32 -g "kernel.cpp" -o "kernel.o" I needed to add -nostdlib
Final command: i386-elf-gcc -nostdlib -ffreestanding -m32 -g "kernel.cpp" -o "kernel.o"

codenaem
  • 59
  • 8
  • It is also a bit peculiar to have CPP (C++) files processed with GCC. If you are using C++ you'd compile with G++. If all your code is straight C then I'd rename `.cpp` to `.c`. – Michael Petch Dec 04 '22 at 19:37
  • `i386-elf-gcc -ffreestanding -m32 -g "kernel.cpp" -o "kernel.o"` doesn't create an object file called `kernel.o`. It creates an ELF executable called `kernel.o`. You should be using the `-c` option to compile `.c` files to `.o` then use GCC (or LD directly) to link all the object files to an ELF executable – Michael Petch Dec 04 '22 at 19:42