Here is a test reproducing the problem:
$ echo \"void whatever() {}\" > prog.c
$ gcc prog.c
This produces the following error on GCC 4.8.4:<
Instead of compiling the code directly, go through all the stages of compilation to figure out where the error is arising (as far as I know, such errors occur during linking). Following gcc
arguments will be helpful:
-E
Preprocess only; do not compile, assemble or link-S
Compile only; do not assemble or link-c
Compile and assemble, but do not linkNow:
gcc -E prog.c
gcc -S prog.c
gcc -c prog.c
With the program/code you have mentioned, all these steps are working perfect with gcc 4.8.4. But, during linking, when you compile using gcc prog.c
, the compiler is unable to link with respective library, as it was not mentioned. Also, we have no main
function in the prog.c
file. So, we need to indicate -nostartfiles
switch. Hence, you can compile prog.c
as:
gcc prog.c -lc -nostartfiles
This produces the warning:
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000004002a3
This is because of the sequence. i.e., init
calls _start
function and the _start
function calls main
function. This warning means that the _start
function is unable to locate main
function, where the init
call is unable to locate _start
. Please note that this is just a warning. In order to avoid this warning, we need to change the command to compile without warnings as follows.
gcc prog.c -lc --entry whatever -nostartfiles
With this command, we are instructing the kernel to compile prog.c
using gcc
by linking the libc.so
library with the starting point as the function whatever
, where this code contains no main
function.
This is the context with gcc 4.8.4, which I've compiled on.
Coming to the case of gcc 6.2.0, I think all these linking stuff is taken care by the compiler itself. Hence, you can simply mention the compiling command as shown below.
gcc -c prog.c -nostartfiles
If it produces any other errors or warnings, you can use the switches mentioned above.
Also, note that crt0
through crtN
(N
depends on the ELF file) are executed before the init
calls _start
, which gives the metadata about the memory and other machine dependent parameters. The linking errors shown as
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
do not provide complete information for rectifying the issue, as machines are not as smart as human beings in identifying the point of error.
This produces a complete executable file. Please do notice that such code (without main function) is used when we are working on libraries/modules within a project.
All the data provided is done with step-by-step analysis. You can recreate all the steps mentioned. Hope this cleared your doubt. Good day!