What libraries are accepted by default when compiling a C project using GCC

I have a simple application:

#include <stdio.h>
int main( int argc, char ** argv )
{
    printf( "hello");
    exit( 0 );
}

When I compile it with the command

gcc -c count_words.c

I have a warning:

warning: incompatible implicit declaration of built-in function โ€˜exitโ€™ [enabled by default]
     exit( 0 );

I tried to find where the function is defined exit(). And found that he is defined in stdlib.h. But it is not included in my project and no additional libs defined in the compilation command.

Correct me if I am wrong, but it looks like it gccaccepts some libraries by default. What are these libraries and is it possible to tell gcc not to include them?

Why is the compiler not satisfied with what exit(0)it assumes is somehow turning stdlib.hon by default?

+4
source share
4 answers

:

#include <stdio.h>

int main( int argc, char ** argv )
{
    printf("hello\n");
    exit(56);
}

:

~$ gcc test.c -o test
test.c: In function โ€˜mainโ€™:
test.c:6:5: warning: implicit declaration of function โ€˜exitโ€™ [-Wimplicit-function-declaration]
     exit(56);
     ^
test.c:6:5: warning: incompatible implicit declaration of built-in function โ€˜exitโ€™
test.c:6:5: note: include โ€˜<stdlib.h>โ€™ or provide a declaration of โ€˜exitโ€™

, , , :

~$ ./test
hello
~$ echo $?
56

@Mat , , :

, .

C - . . , C ( , -nostdlib ) ( loader vdso).

~$ ldd test
    linux-vdso.so.1 (0x00007fff1b128000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f804389f000)
    /lib64/ld-linux-x86-64.so.2 (0x0000557744537000)

. exit libc.so.6 . . -v --verbose :

gcc test.c -o test --verbose

, , :

#include <...> search starts here:
/usr/lib/gcc/x86_64-redhat-linux/5.3.1/include
/usr/local/include
/usr/include

, , stdlib, , . , , . printf stdio.h, exit.

:

/usr/libexec/gcc/x86_64-redhat-linux/5.3.1/collect2 ... -lc ...

collect2 gcc, lc, C. , : . .

, gcc -M, . , , , stdio.h, stdlib.h:

$ gcc -M test.c
test.o: test.c /usr/include/stdc-predef.h /usr/include/stdio.h \
 /usr/include/features.h /usr/include/sys/cdefs.h \
 /usr/include/bits/wordsize.h /usr/include/gnu/stubs.h \
 /usr/include/gnu/stubs-64.h \
 /usr/lib/gcc/x86_64-redhat-linux/5.3.1/include/stddef.h \
 /usr/include/bits/types.h /usr/include/bits/typesizes.h \
 /usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h \
 /usr/lib/gcc/x86_64-redhat-linux/5.3.1/include/stdarg.h \
 /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h

, -E gcc:

$ gcc -E test.c

- . , , .

+5

, - stdlib.h ?

. .

, exit, , . , , exit, .

GCC , , .

stdlib.h, .

+1

"", , - gcc/builtins.def gcc.

, . , .

uneclared exit() :

int exit(int);

" " gory. , , GCC exit() builtins.def:

void exit(int) __attribute__((nothrow,noreturn));

These ads do not match, and about what you are warned.

+1
source

Your code works with a warning not with an error. If you add

#include <stdlib.h>

then no more warnings;)

-5
source

Source: https://habr.com/ru/post/1623540/


All Articles