5 ms·
The actual entrypoint to your program is defined in the elf header of whatever binary you execute (doesnt matter if it is C or not). Usually this entrypoint poi
by Skunkleton 6y ago
The actual entrypoint to your program is defined in the elf header of whatever binary you execute (doesnt matter if it is C or not). Usually this entrypoint points to a function in libc, which after some setup calls the user's main function.
> ... why does C (and C derived languages) require a main function?
It doesn't actually. You just need an entrypoint defined for the loader. In practice most people don't want to go around libc, or fiddle with internals. This is much less true in embedded development, where main functions are sometimes missing.
Why doesn't C have global code? Probably because it would require abstracting further away from the generated assembly. If there is code that is not inside a function, how can it be called? Remember that the entry point to all programs is a function.
- semi-extrinsic 6y agoFortran, which predates C by a decade, does not require a main function either. The following is a completely valid .f90 file that will compile and run: program p do i=1,10 print *, "Hello World ", i end do end program
- Skunkleton 6y agoI would guess if you looked at the assembly for that fortran program, there is some function getting generated. Care to post a disassembly?
- Skunkleton 6y agoI ran the test. The assembly for that program creates a "main" function, which contains fortran init code, and a MAIN function that contains the user code.
- shakna 6y agoBut it does require a program. Which is then placed inside a main function. I compiled the above program with gfortran, and then objdump -d on it, and found this line: 0000000000001283 <main>: Under which the cli flags are setup, and then the program is called with: callq 1199 <MAIN__> Which is where you'll find the "true" main function.
- Skunkleton 6y agoYou beat me to it. I was just looking at that.
- semi-extrinsic 6y agoIn this regard, isn't there also a "true" main function somewhere if you look at the Python interpreter? Wouldn't that count as well? To my mind, the question is "does the programmer have to write a main function", not "does the toolchain create a main function somewhere".
- Skunkleton 6y agoDisclaimer: I know very little about python internals. There is a "true" main function for the interpreter itself. From the perspective of the operating system and CPU the python code isn't executing, the interpreter is. But conceptually, the python code probably doesn't have main function. Python code is so far abstracted from how the CPU works that it likely doesn't need one.
- semi-extrinsic 6y agoJust seems kinda odd to me to distinguish Fortran and Python here, based on how many levels of abstraction there are between the user code and the main function. How would you place Bash scripts then? Or a JITed language like Julia?
- shakna 6y agoPython doesn't require you to have anything around a series of statements. Whereas Fortran requires at least one top-level program block. That program is a top-level function, which directly corresponds to MAIN__ in compiled code.
- Skunkleton 6y agoI only mention python because people were asking about it. There are really two unrelated questions here: 1) How does a computer start executing your code? 2) What kind of abstractions does a programming language provide? Having anything other than a function as a top level most likely implies an abstraction. The second question is pretty much irrelevant when you are talking about interpreted languages. These aren't programs at all from the perspective of the CPU. JIT is a different story, and one that I know very little about on a technical level. What happens when pypy generates some native code? Does it generate a function and call it? Does it have some other convention? I have no idea.