0

Recently I've been poking around and trying to figure out how C compilers work.

I've noticed that different computers seem to exhibit different behaviours for the following actions:

// a.c

// #include"oui.h"
int main() {
  oui();
}
// oui.h

void oui();

gcc -c a.c

On one computer, I am warned that function oui in a.c is undefined. This makes sense to me, and uncommenting the include fixes the problem.

On another computer, however, even with the include commented out, the compiler produces the object a.o without complaint. This does not make sense to me, because isn't oui undefined without the header file?

Which is the normal behaviour? Are some settings on one of my computers messed up?

Don't bother with the following questions if you don't want to, they just popped into my mind and I'll make another thread if need be :).

Side question: The -c flag produces object files but does not link, so is there a special "link" flag afterwards to put the object files together, or is it just gcc? Why not just gcc everything together at the start?

Side question #2: if I do gcc filea fileb filec, is filea inherently special for being the first argument? Or does the order of gcc not matter because the compiler finds whichever file has main by itself?

  • What are the exact error messages or warnings? An error about `oui` being **undefined** would be strange. An error about it being **undeclared** or **implicitly declared** is what I would expect. – Nate Eldredge Jul 11 '20 at 15:42
  • The distinction between **definition** and **declaration** is critical here. You might like to read https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration. – Nate Eldredge Jul 11 '20 at 15:43

1 Answers1

1

It's maybe a version difference between the two computers.

Which is the normal behaviour? The compiler should warn, if you use a function, that isn't declared.

Are some settings on one of my computers messed up? I don't think so, that you have one newer gcc and a older one.

Side question #1: If you use gcc without -c, it will try to invoke the linker. In this example, the linker will fail, as oui is undefined. -c is often used in Makefiles/for bigger projects, as you can recompile faster, because the compiler doesn't have to compile every source file again.

Side question #2: The order of the source files doesn't matter, as the linker finds main by itself.

JCWasmx86
  • 3,033
  • 2
  • 6
  • 24