Thread: Program structure (#include)

  1. #1
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131

    Program structure (#include)

    Hello.

    I was having a problem with a project of mine. I am new to makefiles and I haven't used complicated #include systems, but there appeared a problem with a system of mine:

    main.cpp def.h net_funcs.cpp are the files

    main.cpp includes def.h
    net_funcs.cpp includes def.h

    def.h contains some definitions
    (main.cpp and net_funcs.cpp both need some of the definitions in the def.h file)

    now I have this makefile:

    main.exe : main.o net_funcs.o
    g++ main.o net_funcs.o -Lc:/cpp/include -lws2_32
    main.o : main.cpp
    g++ main.cpp -c
    net_funcs.o : net_funcs.cpp
    g++ net_funcs.cpp -c

    when I run make, this will be done:

    main.cpp will be compiled (main.o will contain the definitions from def.h)
    net_funcs.cpp will be compiled(net_funcs.o will contain the definitions from def.h)
    Thus, both files contain the definitions.
    when the compiler tries to link them together ( g++ main.o net_funcs.o -Lc:/cpp/include -lws2_32 ) - a clash will occur(double definitions).

    So, do you have any ideas of how I should structure my program to make a nice #include system. Is there an universal good style to avoid clashes?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    put definitions in the c file
    put extern declarations in h-file

    (even better - try to avoid global variables)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    is there another way? I wouldn't like to use the extern keyword, but if there's not a better way, I'll use it. Thanks anyway.

    I need to use global variables like:
    const string servername = "www.google.com";

    defining them separately in every function that uses the servername, is bothersome

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    no there is not (except as I said - not using globals)
    if you still want global variable
    you put
    int i = 0; in c-file (there will be 1 instance of this variable)

    you put
    extern int i;
    in h-hile
    so every c-file including this h-file knows that somewhere else a global int i variable is defined
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    what about function declarations?

    the extern thingy seemed to work for variables.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in h- file goes

    int Myfunc(int,char*,double);

    in c-file goes
    Code:
    int Myfunc(int len,char* buffer,double val)
    {
       return _snprintf(buffer,len,"%f",val);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C interview questions
    By natrajdreams in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 12:40 PM
  2. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-05-2009, 11:05 PM
  3. How to include in c program ?
    By deepakwar in forum C Programming
    Replies: 5
    Last Post: 11-15-2007, 06:13 AM
  4. Header file include order
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2006, 03:22 PM
  5. Newbie Again
    By christianne in forum C Programming
    Replies: 14
    Last Post: 04-06-2006, 12:39 AM