jpmelos
jpmelos

Reputation: 3562

Weird linking situation

After some talking about linking in ##C on irc.freenode.net, I went to test some concepts I learned, and came up with this situation.

I have this file, named main.c:

int main(void) {
    func();

    return 0;
}

And this file, named test.c:

#include <stdio.h>

void func(void) {
    printf("Hello.\n");
}

There's no test.h file.

I do this:

$ gcc -c main.c
$ gcc -c test.c
$ gcc main.o test.o
$ ./a.out
Hello.
$

and that works. Shouldn't gcc complain, on its first call, about not knowing function func() that is called in the main.c file? I didn't include any file with its prototype or implementation, and yet gcc can compile an object code and make a sane executable. What happened there that I'm missing?

Thank you.

Upvotes: 1

Views: 93

Answers (2)

Vitor
Vitor

Reputation: 2792

It works because all the argument types are matching (since you don't have anyone). You can make gcc complain by calling it gcc -c -Wall test.c

Upvotes: 2

Seth Robertson
Seth Robertson

Reputation: 31441

Turn on a few warnings and you will be made painfully aware of the problems.

> gcc -Wall -c main.c
main.c: In function ‘main’:
main.c:2:5: warning: implicit declaration of function ‘func’

C will by default assume things about unknown functions. Good? Probably not. Historical.

Also gcc -std=c99 will throw the warning as well.

Upvotes: 8

Related Questions