3

I have defined function A in main.c file. I have created three libraries which use the function A without importing anything. The code works but I have only one warning: implicit declaration of function 'A' [-Wimplicit-function-declaration].

How is it possible that the function A works in a function B defined in a separate file without importing it? How is it possible that I have only one warning when function A is called by other functions except function B?

Badda
  • 1,329
  • 2
  • 15
  • 40

3 Answers3

5

Global non-static symbols (variables and functions) have by default external linkage meaning they can be accessed from other translation units.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • This would also explain why you can use printf without including stdio.h. – cs95 Jun 16 '17 at 09:58
  • @Coldspeed Ehr, not really. That has more to do with the implicit declarations done by the compiler (which was marked as obsolete in C99 and removed in C11 I think). Because the C compiler infers the declaration from the first use of the function, if you try to call `printf` with other arguments later the compiler will think it's wrong. – Some programmer dude Jun 16 '17 at 10:01
2

In C, we don't "import" functions. We compile individual translation units to object files and then link all of them together to form the binary / executable.

In the linking phase, linker checks the object files for required symbols and references and links them together to produce the single executable (thus making the function call possible at runtime).

In your case, the compiler does not "see" the function declaration at the time of the call (so, it does not have any idea of the function signature, which can be a potential pitfall, that is why you have the "warning"), but in the linking phase, linker is able to find the reference to the function (assuming both the translation units are being linked together to form the binary) and creates the binary.

FWIW, implicit function declarations are non-standard as per the latest C standards. You must forward declare the function (provide a prototype) before you can actually use the function. Quoting C11, Foreword,

Major changes in the second edition included:

[....]

— remove implicit function declaration

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Compiling:

  • During compilation each file is compiled separately and at last a .o file is generated from a .c file.

  • For each function called in the file compiler expect's the function definition or at least the function's declaration.

  • In case of missing the definition or declaration you get a warning from the compiler like implicit declaration of function 'A'
    [-Wimplicit-function-declaration].

  • In your case as the function definition is in another file you must at least include the function declaration in your include file.

Linking:

  • Linking refers to the creation of a single executable file from
    multiple object files. In this step, it is common that the linker
    will complain about undefined functions.

  • As the function A in main.c is globally defined it will be used by the library.