Thread: Linker quetion

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Linker quetion

    Excuse me if this is a question for the Devc++ forum,

    I"m currently working a project in DevC++. I have four files in the project.

    main.c
    functions.h
    function1.c
    functions2.c

    obviously main calls the functions header but also calls <stdio.h>.

    The functions.h only has the prototypes for the functions1 and functions2.c respectively. <stdio.h> is included again only in function1.c

    My questions is two fold. in General how does the linking of different source files work? to my understanding the functions.h called from main is essentially bringing the functions into main. So why can I not have only one <stdio.h> in main. Why is necessary to include it in my function1.c file.
    If becomes more confusing when I attempt to remove it from the function.c files and simply put it once in the header file. Also If i remove the <stdio.h> from fucntion1.c and put it in function2.c. i get an error saying my functions are undeclared, as if i never included <stdio.h> in any file.

    Not coming from a linux background i'm not quite shure i have the know how in understanding this, since Dev automatically creates a makefile for me. again if this is the wrong forum let me know.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by caroundw5h
    Excuse me if this is a question for the Devc++ forum,

    I"m currently working a project in DevC++. I have four files in the project.

    main.c
    functions.h
    function1.c
    functions2.c

    obviously main calls the functions header but also calls <stdio.h>.

    The functions.h only has the prototypes for the functions1 and functions2.c respectively. <stdio.h> is included again only in function1.c

    My questions is two fold. in General how does the linking of different source files work? to my understanding the functions.h called from main is essentially bringing the functions into main. So why can I not have only one <stdio.h> in main. Why is necessary to include it in my function1.c file.
    If becomes more confusing when I attempt to remove it from the function.c files and simply put it once in the header file. Also If i remove the <stdio.h> from fucntion1.c and put it in function2.c. i get an error saying my functions are undeclared, as if i never included <stdio.h> in any file.

    Not coming from a linux background i'm not quite shure i have the know how in understanding this, since Dev automatically creates a makefile for me. again if this is the wrong forum let me know.

    There are two separate considerations: compiling and linking.


    First: compling generates code from a given source file and saves it in an "object file" to be linked with other object files that you create now and with library files containing previously compiled object units.

    Each source file is its own "compilation unit". If you use a function, say printf(), in a given compilation unit, a prototype for printf must have been made known to the compiler before that function is called. So, you #include <stdio.h> in that file.

    If you have another compilation unit (another source file) that uses printf, that file is compiled separately, so the fact that printf() was known in the other compilation unit is not relevant when the compiler gets to printf() in this file. So, you must #include <stdio.h> in this file as well.

    Now if you have a reference to an external function or a variable that is not in a given source file, the compiler creates a place holder in the object file for the address of the variable or function. When the linker puts together all of your object files to get an executable, it looks to fill in the addresses of all of these functions and variables.

    If you have declared a function prototype in one of your source files (or an included header file), the linker tries to find the function in that object file, then in other object files then in libraries (using compiler-dependent paths to find library files).

    Note that unless you declare functions in a given source file to be static, they are available for linking with other object files in your compilation. (Assuming that their prototypes have been declared in the other file.) For variables declared outside of functions, unless you declare them to be static they are also available for linking to other object files in your compilation. (Assuming that they have been declared extern in the other compilation units.)

    Regards,

    Dave
    Last edited by Dave Evans; 10-25-2004 at 06:16 PM.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    This might give you an idea of what the linker does: http://webplus.vss.company.org.uk/tu...20linker%20do?

    Basically all you're doing when you're doing #include <stdio.h> is showing your program the prototypes for all the functions declared in stdio.h. This happens at the preprocessing stage...before anything is even compiled. The code for the functions is actually in a library file sitting somewhere.

    The prototypes in stdio.h just tell the compiler "Hey. I'm using this function and I know it's not in this file I'm programming in, but here's how the function works and you can let the linker worry about how to call it."

    Then the compilation finished and the linker kicks in. It runs through and looks at all of the unresolved symbols (Functions you try to call but don't actually exist) and checks against all the libraries you're linking to to see if they exist there somewhere.

    So you really have 3 steps in the compilation process:
    1) Preprocessing - Stuff like #include and #define takes place. So if you #include <stdio.h> then the compiler knows how a function like printf() works (it returns this type and takes these parameters).
    2) Compilation - Turns your source code into object file(s).
    3) Linking - Links function calls across different files into a neatly bundled executable file. This is where the stuff happens like "He's calling a function called printf() but he didn't define it. See if it's in any of the other object files or libraries."

    EDIT: Beaten to the punch.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thanks Guys,
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker problem... no idea
    By cyreon in forum C Programming
    Replies: 2
    Last Post: 04-03-2009, 02:53 PM
  2. linker
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2008, 01:25 AM
  3. I am gonna kill myself...linker error !!!
    By roalme00 in forum C++ Programming
    Replies: 1
    Last Post: 09-02-2007, 11:34 AM
  4. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 07:42 AM
  5. Linker errors with Visual C++
    By codegirl in forum C++ Programming
    Replies: 4
    Last Post: 09-11-2003, 09:20 AM