For a C or assembly program that does not require any other library, will linking be necessary? In other words, will conversion from C to Assembly and/or from Assembly to an object file be enough without being followed by linking?
If linking is still needed, what will it do, given that there is just one object file which doesn't need a library to link to?
Relatedly, how different are object files and executable files, given that in Linux, both have file format ELF?
Are object files those ELF files that are not runnable?
Are there some executable files that can be linked to object files? If yes, does it mean dynamical linking of executables to shared libraries?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
First of all, it's really hard for any non-trivial program to 'not require any other library'. Remember that glibc and the startup code calling
But yes, even in this case you need the linker, just because the compiler/assembler typically don't handle ELF format (or any executable format). Precisely because usually you'll have to link with some libraries, so why should it bother to compile to ELF? It's better to focus on linkable-code formats and leave the executable formats to the linker.
PS: I forgot that
Yes, it's quite possible for a single tool to compile directly to executable, but that just means that it includes both the compiler and the linker in a single command. Why would it consider the (very) special case when you won't add any other code? It's far more logical to do it in the linker, after all you'll need it in %99.9 cases.
It depends on the operating system. In general, though, you need something to convert the object file into an executable file. Object files are usually designed to be the input into the linker, and not to be run directly.
There are very few, if any, useful programs that don't need to have libraries linked in with them. (Maybe static, maybe dynamic, but without the library infrastructure, you're not going to get much useful work done.)
Your program will (at the minimum) need to link to a loader in order run. Consider the following trivial code:
Now we compile it:
And now we examine what the linker did:
Of interest is the fact that
Let's statically link this, shall we?
I haven't eliminated the linking dependency here, all I've done is taken the loader code and system C library and made them part of the executable. That seems somewhat silly, because my executable no longer uses a perfectly good copy of that code which is already in memory.
You could write your own loader code and not use what's provided by the system C library, but you aren't going to lose the dependencies on the kernel loader itself.
This is a very Linux oriented example, but it's all I have available to me to illustrate the point :)