cannot find crt0.o: No such file or directory
Asked Answered
R

2

15

I'm trying to learn operation system using i386-elf-gcc on my mac pro. Os version is Sierra. I use Macports to install the compile environment by typing sudo port install i386-elf-gcc in bash. But when I compile a helloworld program it goes wrong.

gcc hello.c

/opt/local/lib/gcc/i386-elf/4.7.2/../../../../i386-elf/bin/ld: cannot find crt0.o: No such file or directory
collect2: error: ld returned 1 exit status

then I use sudo find / -name "crt0.o", but in my mac there only has crt1.o, crt3.o and so on. And I use -nostdlib and -nostartfiles it still goes wrong:

gcc -nostdlib -nostartfiles hello.c

/opt/local/lib/gcc/i386-elf/4.7.2/../../../../i386-elf/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000008048054
/var/folders/wc/0c_zn09x12s_y90ccmvjb6900000gn/T//cc384VWr.o: In function `main':
hello.c:(.text+0x11): undefined reference to `puts'
collect2: error: ld returned 1 exit status
Rotate answered 13/1, 2017 at 18:14 Comment(5)
If you don't use crt0.o, then you have to provide your own startup code defining _start (that's for the second problem).Moravian
Looks like your installation of gcc is broken. Did you see any error messages during install? And do not use -nostdlib and -nostartfiles unless you have a really good reason (which happens in .000001% of cases).Mediatory
@EugeneSh. However.. Is there any tutorials tell me how to write... 'Cause I transplanted some crt0.o from my Linux virtual machines but It can't recognize and tell me wrong. So I beg there are differences between cross compilers? And I am a novice in gcc...Rotate
It seems odd to be using an ELF-based compiler on macOS Sierra; the native object file format is Mach-O 64-bit object x86_64 or Mach-O object i386, neither of which is self-evidently ELF-compatible. What are you expecting to happen?Autochthonous
Add the final result. The gcc is still that problem. What I can do is use Docker to make a ubuntu vm and install gcc inside. It is not a good solution but it works... Maybe on macOS it's totally incompatible.Rotate
S
1

ASSUMPTION: If you'e learning operating systems, I assume you are trying to write a bootable code of some sort, and that would be the reason why you're trying to pass -nostdlib and -nostartfiles.

My answer lies on that assumption.

One of the problems in the second output (the -nostdlib -nostartfiles command) is that you are trying to use a libc function puts() and probably hasn't implemented it. So the linker cannot find its byte code to generate the final executable.

While I cannot provide an explanation for the first message with the information available, I can speculate that something is missing or wrong with your libc. It's either not matching your compiler somehow, or absent, or something like this.

Furthermore, if you're writing bootable code you are likely going to need a linker script. Your firmware has specific standards on where in memory the entry point of your program will be loaded and where it needs to start execution, and you'll need your linker script to organize the final binary according to this layout.

Slipknot answered 13/12, 2023 at 18:39 Comment(6)
Seems like a dodgy assumption. To me it looks like he has a messed-up compiler and no idea how to fix the problem. Too bad I don't either. It's also a question from 2017 that got woken up by something. OP probably isn't here to clarify.Dantzler
@Joshua, I don't think so. The OP literally started the post with "I'm trying to learn operating systems", and tried to use the -nostdlib flag. I might be wrong, but I cannot think of some other possibility that would explain these two facts.Wetzell
You normally start learning operating systems by writing low level multi-threaded C code in userspace. You also don't start by trying to compile with no options and then adding -nostdlib -nostartfiles after getting an error about a missing startup file.Dantzler
In any case OP is very mixed up.Dantzler
Well, that depends a lot on your literacy level and access to information/education. When I had no access to formal education and there was no proper material in my language, I took myself lots of weird paths on my own journey. I'm not saying it's the OP's case, but I totally see myself in those long gone times, totally lost and trying to do something like that totally on my own.Wetzell
And please notice, when you say "You normally start learning operating systems by <..." , you're also relying on a giant assumption that matches the experience of a much more privileged group of people, with access to education, proper teaching, etc.Wetzell
F
0

I was also facing the same issue on my Mac. The problem was linking with the dynamic library. So instead of compile it dynamically compile it statically.

Just try this:

i386-elf-cc -nostdlib -static hello.c -o hello

Or if you want you can also do this:

i386-elf-cc -nostdlib -nostartfiles -static hello.c -o hello
Frankhouse answered 11/7 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.