0

I would like to know , if I have source file like this

#include <stdio.h>
int main()
{
   printf("Hello world");
}

as I know header files contains only prototypes of functions. If it so how can my source file get the function printf ? If I don't include the source file where it has been declared? thank you

1
  • 5
    The actual function gets linked from the libraries when you compile the code. Commented Oct 28, 2013 at 7:05

5 Answers 5

3

The question is not clear, but there are 2 things which happen before a program gets created,

  1. Compiling (requires prototypes / declarations)

  2. Linking (requires definitions).

Header information is needed for knowing prototypes. Even this would compile fine:

int printf ( const char * format, ... );
int main()
{
   printf("Hello world");
}

On linking there will be no issues because the printf function is found in the C standard library, so on linking it will look into the standard directories (of the compiler where the library is kept - bin/lib folder) and link the function.

The source only needs to know the prototype. The problem a programmer will have in this case:

int my_printf ( const char * format, ... );
int main()
{
   my_printf("Hello world");
}

The above will compile, but when linking my_printf your code will not have a definition so it will give an error on linking.

Sign up to request clarification or add additional context in comments.

2 Comments

and if I create definiton in my_printC.c source file how can I make linker to link when I use my_printf function?
Pass all the .c files to the compiler together (as a single translation unit), e.g., gcc -Wall -c main.c my_printC.c - you could compile them individually as well like gcc -c my_printC.c, gcc -c main.c and link the object files created like gcc my_printC.o main.o -o prog. Note -c switch for compiling and -o switch for linking
2

Header file has the definitions declarations of the functions ( stdio.h has definition declaration for printf ). The actual function exists in the libraries and gets linked when you compile the code.

4 Comments

okay and if I'll create my header file with a function for example void say(void) , and I write the definiton of the function in def.c source file, does it automaticly get linked to the def.c source file when I include the header file in my program?
Header file has the definitions of the functions - This is so wrong. The C standard library and C++ standard library traditionally **declare** their standard functions in header files.
@luka then you just have to include that header file and you code will compile and work
One correction? *.h has "declaration" for printf, and as you already mentioned the libraries have the definition.
0

When it comes to using libraries, you include the header of the library in your code and you instruct the linker to link using the code files of that library(usually object files). For the standard libraries, the IDE usually instructs the linker to link to them by default.

3 Comments

The IDE does not link (nor does it pre-/compile). The linker links. The latter migth have been invoked by some other tool like the IDE.
@alk The IDE provides the command line for the Linker, my friend, when you use an IDE, that is.
Anyway, for those that have a difficulty to comprehend what an IDE is and how it "links", I edited my answer to make it more clear.
0

Assuming you're using gcc as a compiler the standard libraries are linked by default and that is where the function definitions lie. If you'd like to see exactly which libraries are being linked you can pass gcc the -v option which will cause it to dump information about the default options it will use including the library paths and default libraries and object files that will be linked in.

If you give the -Wl,--verbose option, gcc will pass the --verbose to the linker which will dump exactly where it's looking for libraries, including both failed and successful searches

gcc -v foo.c -Wl,--verbose

Comments

0

The header file stdio.h would declare printf as an extern function, i.e., it is defined elsewhere. The compiler is happy as long as functions you use have a declaration. The linker is the one that resolves these dependencies.

A very useful thing to do when you start asking good questions like this is to play with some linker commands.

Assuming you're on *nix, once you have your executable file (lets call it foo), do:

ldd foo

You should see a list of libraries that were linked with while creating foo.

libc.so should be one among those. It contains the definition for printf among other things!

You can refer to this link to learn more

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.