Thread: How do headers and other c files work

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    How do headers and other c files work

    I am trying to figure out how headers and other c files besides main work so i can reduce my the size of my main.c code for better readability and error fixing. Also i want to use some external code that which has custom headers and their accompanying c files. I just am not understanding the underlying processes here. Im working on an embedded system that responds to the modbus protocol and am trying to get freemodbus working but in coming into typedef errors, etc. and would ultimately like to make my own stack but am getting lost in understanding the code. I am an amature at c but have been given an opportunity at work to learn an new discipline, so if there are books and other resources you can recommend to a noob that would be greatly appreciated.

    Thanks!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just make a file called something.h. Any time you want to use what's in it, or referenced by it, include it:
    Code:
    #include "something.h"
    Typically things like structure definitions, function prototypes, macro constants, that sort of thing are good candidates for .h files.

    If you have multiple .c files, any function in one file that you want to be available to another, needs to be included via header. Like so:
    Code:
    ...foo.h...
    void myfun( void )
    
    ...foo.c...
    void myfun( void )
    {
        ...do something...
    }
    
    ...main.c...
    #include "foo.h"
    int main( void )
    {
        myfun();
    
        return 0;
    }
    Now just compile and link them correctly.

    gcc -o myprog foo.c main.c


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    if you want to know how actually you C code is compiled and works with the processor ..

    I would study assembly language and also some digital logic to get the feell of how its all working ...

    its a very tedious work ..

    A good book on assembly that teahes you digital logic , architec ture disgn ..for both CISC IA32 and RISC PPC , ARM and MIPS ..

    Amazon.com: Introduction to Assembly Language Programming: For Pentium and RISC Processors eBook: Sivarama P. Dandamudi: Kindle Store

    and

    Amazon.com: Fundamentals of Computer Organization and Design (9780387952116): Sivarama P. Dandamudi: Books

    and

    Amazon.com: Guide to RISC Processors: for Programmers and Engineers (9780387210179): Sivarama P. Dandamudi: Books

    After you are done with those books ..which give you the basics .. i am currently on chapter 6 of the first book as posted above ..

    I can write a simple C code ..say just something with int main and then watch its assembler file and see how the compiler actually works with it ..further more ..

    i can add if else , structures , points and so own and graduly study the asm code .to see how the compiler does its thing and what intructions are being used to make you C logic ..usefull to the CPU ...

    i myself would like to get into digital electronics and embedded ..
    If you need to find where to get these books for very cheap prise .. send me a message and then we can talk about it ..

    by any chance heard of Reverse engieering ?that could help you alot right now ..

    I am quite proud to say that i ahve done asm code..that for example doing matric multiplication , get binary value convert to hex .. hex to decimal .. string to decial ..atc in ASM ..

    if you need a helping hand .. i am around and i can teach you enought to get threw ..anyways .. let me know .. i am always here to help ..
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  4. #4
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Thanks for the useful information!!!!!!

Popular pages Recent additions subscribe to a feed

Tags for this Thread