Thread: #include

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    #include


    If I want to divide a program into multiple source files, say main.c search.c and interface.c, and each of the files for search.c and interface.c include <gtk/gtk.h>, when compiling each of the object files separately before linking the final program, does that mean that the entire GTK library is included in each object file? What happens when it gets linked? It seems like there would be several copies of the library and make the file size much bigger.

    main.c:

    Code:
    #include <gtk/gtk.h>
    #include "search.h"
    #include "interface.h"
    ...
    
    int main()
    {
       GtkWidget *wid = ...
    
       ...
    search.c:

    Code:
    #include <gtk/gtk.h>
    #include "search.h"
    
    void search(...)
    {
       GtkWidget *wid = ...
    
       /* other functions from GTK */
    interface.c:

    Code:
    #include <gtk/gtk.h>
    #include "interface.h"
    
    void build_interface(...)
    {
       GtkWidget *wid = ...
    
       /* other functions from GTK */
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > does that mean that the entire GTK library is included in each object file?
    No.

    > What happens when it gets linked?
    Lets say you have a number of calls to gtk_widget_new scattered through all your code. The linker would first load main.o, search.o and interface.o, and see say three unresolved references to gtk_widget_new.

    It would then start to search the gtk library. When it finds the definition of gtk_widget_new, it adds this to the output executable and updates all the unresolved references.

    When the linker gets to the end of all the libraries it has been given to search, hopefully there are no more unresolved references, the linker will write out the executable program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo