This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

include file

hello everyone,
i am writing a program for c8051f020,in which i have 4 ".c" files: main c file,keyroutine file, general routines files,confguration file

i separated bit addressable, data addressable, functions, #defines in separate header files

i included these header files in respective ".c" file

but i am getting following error in linking

1. public refers to ignored segment
2.address space overflow both for data as well as bit memory
3.multiple public definitions

i have put all ".c" files for build
shall i continue or main c file should be put for building

if i do so then for debugging i will not be able to put breakpoint in other c files

can you tell me how to include c file in another c file

for further details pls let me know

thank you all
take care

  • can you tell me how to include c file in another c file

    i is be answereding the qeusetion.

    #include "file.c"
    

  • Get a textbook on C. This is not a Keil problem but a beginners errors using C.

  • Yes, you answered the question - unfortunately, it was the wrong question.

    While it is true that you can include one 'C' file within another like that, it is almost certainly not a good way to proceed.

    As already noted, get a 'C' textbook; this is standard 'C' programming practice - nothing specifically to do with Keil.

  • In this case look for the the difference between a definition and a declaration. And the Keyword "extern"

  • hello everyone,
    thanx to all for reply
    my problem is solved and as you said, including .c file is not good programming practice
    i have not included .c file
    but i separated repective variable of respective .c files
    and declared these variables as extern in main .c file
    i was including header files again and again and due to that i got error

    thanx a lot for suggestions
    take care

  • Best practices for header files says that you should always guard them from multiple inclusion:

    charlie.h:

    #ifndef _CHARLIE_H
    #define _CHARLIE_H
    ...
    #endif // _CHARLIE_H
    

  • #ifndef _CHARLIE_H
    #define _CHARLIE_H
    ...
    #endif // _CHARLIE_H
    

    Beware! From the standard:

    "All identifiers that begin with an underscore and either an uppercase letter or another underscore are reserved ..."

  • Yes - I was actually waiting for that remark :)

    I normally have a long prefix with company name etc at the start of the symbol, to make sure that it will be unique both compared to the standard and in relation to potential 3part libraries.

    Many IDE has a feature to automatically add this inclusion guard, including some form of random number in the symbol name to make sure that no collision can happen.

  • "Yes - I was actually waiting for that remark :)"

    I see, you were testing us then.

  • I saw the code-name "CHARLIE" and immediately fell for it (it had to be a redhead!). Got me, Per!

  • hello,

    I do like it,
    include <main.c> and <global.c> in your project, but not the <global.h> and <constant definition.h>

    <main.c> : // main program
    #include <global.h>
    #include <constant definition.h>

    <global.c> : //variable declaration and initialisation
    #include <constant definition.h>
    unsigned char variable1 = 5;
    unsigned char variable2 = constante2; (= 5)

    <global.h> : //must be include in all c file of your project but not in <global.c>
    extern unsigned char variable1;
    extern unsigned char variable2;

    <constant definition.h> :
    constante2 5

  • This is not valid code:

    constante2 5
    

    Some alternatives:

    const int constante2 = 5;
    
    enum {
        constante2 = 5
    }
    
    #define constante2 5
    

    But I'm not so fond of constants that doesn't stand out as constants.

    I prefer to use all caps, just as the normal convention for #define constants.

    Note that 'const int' may affect the code generation for your 8-bit processor.

  • <global.h> : //must be include in all c file of your project but not in <global.c>
    

    Actually it is advantageous if you do include global.h in global.c - that way the compiler can warn you of any inconsistencies!

    :-)

    But I don't think that simply dumping all your globals into a single file is a good idea...

  • Sorry to take you to task Mr. Westermark, but I simply must disagree on the issue of "Best Practices" with respect to that type of #ifndef #define #endif usage.

    I do not rely on such a methods that intrinsically asks:

    #ifndef DRINK_C_COOLAID   // "Gee, did I already declare that?"
    #define DRINK_C_COOLAID 9 // "I didn't?" ... "I guess I better do it now."
    #endif                    // "I really hope no other file has that #define too"
    


    The very use of this construct implies that the source code is not tightly engineered. (I consider that a bad thing).

    Don't get me wrong there Per, *they* all do it that way. It is all too common in the Main Stream C world.

    I don't do that construct. I like total control [and domination] over my source code (most likely due to my 'ego trouble' ;-).

    Cheers,

    --Cpt. Vince Foster
    2nd Cannon Place
    Fort Marcy Park, VA

  • I agree that you should not need to protect a #define with a conditional.

    But you may have to protect a complete header file.

    Let's say that it contains definitions for uint8, uint16 etc. All source files should have it. But none twice.