How to organize files to write include?

Discussion in 'C Programming' started by Kevin Handy, Sep 15, 2006.

  1. Kevin Handy

    Kevin Handy Guest

    key9 wrote:
    > Hi all
    >
    > look at the organize tree


    Sounds like a troll already.
    Bad engrish, lack of caps, etc.

    >
    > main.c
    > ------
    > #include lib_adapter.c
    > main()
    > {
    > foo();
    > }
    >
    >
    > lib_adapter.c
    > -------------
    > #include "foo_liba.c"
    > #include "foo_libb.c"
    >
    > void foo(){
    > #ifdef USING_LIBA
    > foo_liba()
    > #endif
    > #ifdef USING_LIBB
    > foo_libb()
    > #endif
    > }
    >
    > void need();
    >
    >
    > lib_a.c
    > =======
    > void foo_liba()
    > {
    > need();
    > }
    >
    > lib_b.c
    > -------
    > void foo_libb()
    > {
    > need()
    > }
    >
    >
    > gcc -o main main.c lib_adapter.c lib_a.c lib_b.c


    some of the problems:

    1. main should return an int.
    2. Include files should not contain executable code.
    You want to end up with one copy of each function,
    and putting code in include files causes the function
    to be expressed every time you include the file,
    causing the linker to be upset about multiple definitions.
    3. Include files should end in a '.h' extension.
    It's a convention, and most C programmers expect it.
    This allows for a 'cc *.c -o xxx' at the command line
    to compile most simple programs.
    4. Since lib_adapter.c, lib_a.c, and lib_b.c are all
    included in main.c, why are you compiling them a
    second time?
    5. main should return a value.
    6. need() is not implementated anywhere.
    7. Neither "USING_LIBA" nor "USING_LIBB" are defined,
    thus foo() is a null function.
    8. need() is prototyped after use, instead of before.
    ....

    > the code can not be complained , what I want to know is


    Why can't it be complained. I see lots of things to complain about.
    What errors do you get? Are we supposed to guess?

    >
    > I can not put need() out side of the lib_adapter.c


    What happens when you try? What error do you get?

    >
    >
    > 1.how to write include structure
    > 2.need I have to write .h file instead of include c file?


    You don't seem to really understand the purpose of
    include files.

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Kevin Handy, Sep 15, 2006
    #1
    1. Advertising

  2. Kevin Handy

    key9 Guest

    Hi all

    look at the organize tree

    main.c
    ------
    #include lib_adapter.c
    main()
    {
    foo();
    }


    lib_adapter.c
    -------------
    #include "foo_liba.c"
    #include "foo_libb.c"

    void foo(){
    #ifdef USING_LIBA
    foo_liba()
    #endif
    #ifdef USING_LIBB
    foo_libb()
    #endif
    }

    void need();


    lib_a.c
    =======
    void foo_liba()
    {
    need();
    }

    lib_b.c
    -------
    void foo_libb()
    {
    need()
    }


    gcc -o main main.c lib_adapter.c lib_a.c lib_b.c
    the code can not be complained , what I want to know is

    I can not put need() out side of the lib_adapter.c


    1.how to write include structure
    2.need I have to write .h file instead of include c file?

    best regards


    your key9
     
    key9, Sep 15, 2006
    #2
    1. Advertising

  3. Kevin Handy

    Thad Smith Guest

    key9 wrote:
    > Hi all
    >
    > look at the organize tree
    >
    > main.c
    > ------
    > #include lib_adapter.c


    Although omitting <> or "" may work on your compiler, it is not conforming.

    > main()
    > {
    > foo();
    > }
    >
    >
    > lib_adapter.c
    > -------------
    > #include "foo_liba.c"
    > #include "foo_libb.c"
    >
    > void foo(){
    > #ifdef USING_LIBA
    > foo_liba()
    > #endif
    > #ifdef USING_LIBB
    > foo_libb()
    > #endif
    > }
    >
    > void need();
    >
    >
    > lib_a.c
    > =======
    > void foo_liba()
    > {
    > need();
    > }
    >
    > lib_b.c
    > -------
    > void foo_libb()
    > {
    > need()
    > }
    >
    >
    > gcc -o main main.c lib_adapter.c lib_a.c lib_b.c
    > the code can not be complained , what I want to know is
    >
    > I can not put need() out side of the lib_adapter.c


    Do you mean that gcc does not give error messages or warnings? If not,
    I can't interpret your comment.

    > 1.how to write include structure


    Questions should have a subject, verb, and end in a question mark, as
    well as ask a question: "How can I include a structure in my program?"
    That might not be the question you intended to ask. I think you are
    asking about how to use #include statements in a program.

    By convention, C programs are separated into code files and declaration
    files. The code files contain function definitions and normally have an
    extension of .c. The declaration files have declarations normally used
    by multiple code files and have an extension of .h. These naming and
    organizational conventions are not requirements of the language, though,
    and compilers don't care what extensions are used on the files.

    Normally .c files are each compiled into a separate object file, then
    the object file are linked into a single executable file. In contrast,
    I have used a C dialect compiler (for small microprocessors) that
    requires all code files to be compiled together -- it cannot compile to
    object files and link. For that compiler, it is typical to have a main
    file include other code files. This is not recommended for normal use,
    though.

    > 2.need I have to write .h file instead of include c file?


    No. In general you should compile code files separately and link them.
    I recommend writing a header file for each code and placing the
    prototypes (and other definitions required to use the code module) in
    the associated header file. Then include the header file in both the
    code file defining the functions and code files using the defined functions.

    I suggest finding a good text book on C.

    --
    Thad
     
    Thad Smith, Sep 16, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TokiDoki
    Replies:
    10
    Views:
    731
    Micah Elliott
    Oct 20, 2005
  2. Andreas Bogenberger
    Replies:
    3
    Views:
    957
    Andreas Bogenberger
    Feb 22, 2008
  3. cnb
    Replies:
    6
    Views:
    281
    jay graves
    Sep 2, 2008
  4. Replies:
    5
    Views:
    372
    Paul McGuire
    Mar 20, 2009
  5. Junhui Liao
    Replies:
    3
    Views:
    173
    Colin Bartlett
    Jul 27, 2010
Loading...

Share This Page