• AVR Freaks

Hot!How to use #include

Page: 12 > Showing page 1 of 2
Author
MJordan
Starting Member
  • Total Posts : 48
  • Reward points : 0
  • Joined: 2016/02/17 18:09:49
  • Location: 0
  • Status: offline
2020/06/26 12:57:26 (permalink)
3 (2)

How to use #include

I have a very basic question, which is really about programming in C:
 
I have a function "void foo" in a file called: "myfoo.h". I would like to call "foo" from "main" in "main.c", but I would also like to call "foo" from another file called: "myOtherFile.h".
 
As you probably guessed, when I '#include "myfoo.h"' in BOTH "main.c" and "myOtherFile.h" the compiler gets mad at me. From what I understand this is because of something that sounds like a circular reference or something similar. I get why the compiler is mad, nobody wants to go in circles... 
 
In the past, to get stuff done, I would just copy the function into two places and move on, but this is a basic concept and I really would like to understand it better... SO can someone explain to me how to structure something like this correctly??? 
-thanks for your help
#1

25 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3834
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: How to use #include 2020/06/26 14:29:02 (permalink)
    -1 (5)
    OMG !
    Until now your projects consist of main.c and a bunch of .h files?
    Then you'd be better off getting a nice textbook like "C for Dummies".
    Or find some online lectures about C programming.
    (Sorry, I have next to no links: I'm programming in C/++/# for 30+ years now. The only C book I ever (partly) read was Kernighan/Ritchie "The C Programming Language". (Can't even recall whether I can recommend this one without reserve - too long ago). The rest is mainly self-taught, plus repeatedly seeing code from others and adopting what I considered the best.)
     
    Not sure whether the following one is completely legal: 
    https://www.google.com/url?q=http://www2.cs.uregina.ca/~hilder/cs833/Other%2520Reference%2520Materials/The%2520C%2520Programming%2520Language.pdf&sa=U&ved=2ahUKEwiU0tqztaDqAhVKMewKHT7ZB0UQFjAAegQIBBAB&usg=AOvVaw086M80lv80TOU-1-zSUfOg
     

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    malaugh
    Super Member
    • Total Posts : 412
    • Reward points : 0
    • Joined: 2011/03/31 14:04:42
    • Location: San Diego
    • Status: offline
    Re: How to use #include 2020/06/26 16:04:50 (permalink)
    0 (4)
    Doulble incudes are a common issue in C programming.  The way all C programmers get around this is to start the file with
     
    #ifdef INCLUDE_FILE
    #define INCLUDE_FILE
    ...... include file contents here .........
    #endif
     
    The way this works if if you get in a situation where the same include file is being included twice, in the second include INCLUDE_FILE is already defined, so the file contents are skipped.  You need a different name for the define in each header, most people use the name of the file in some way.  For "myfoo.h" i would use MYFOO_H as the name.
     
     
     
     
     
     
    #3
    ric
    Super Member
    • Total Posts : 27953
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: How to use #include 2020/06/26 16:32:02 (permalink)
    +5 (5)
    But the basic problem is, you MUST NOT put executable code into H files.
    That should all go into C files, and the H files just contain declarations so the compiler knows how to use them.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11929
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: How to use #include 2020/06/26 17:21:53 (permalink)
    +4 (4)
    #ifdef INCLUDE_FILE

     
    I think you meant #ifndef here.
     
    #5
    LdB_ECM
    Super Member
    • Total Posts : 401
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: How to use #include 2020/06/26 22:48:41 (permalink)
    +1 (3)
    google the term "include guard" there are thousands of clear explanations and examples out there.
    #6
    1and0
    Access is Denied
    • Total Posts : 10985
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: How to use #include 2020/06/27 06:59:58 (permalink)
    #7
    crosland
    Super Member
    • Total Posts : 2009
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: How to use #include 2020/06/27 07:13:53 (permalink)
    +2 (2)
    malaugh
    Doulble incudes are a common issue in C programming.  The way all C programmers get around this is to start the file with
     
    #ifdef INCLUDE_FILE
    #define INCLUDE_FILE
    ...... include file contents here .........
    #endif
     
    The way this works if if you get in a situation where the same include file is being included twice, in the second include INCLUDE_FILE is already defined, so the file contents are skipped.  You need a different name for the define in each header, most people use the name of the file in some way.  For "myfoo.h" i would use MYFOO_H as the name.

     
     
    LdB_ECM
    google the term "include guard" there are thousands of clear explanations and examples out there.


    Congratulations!
     
    You just taught an obviously novice C programmer to surround executable code with code guards.
     
    Plonk! as we used t say on usenet.
    #8
    du00000001
    Just Some Member
    • Total Posts : 3834
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: How to use #include 2020/06/27 08:11:22 (permalink)
    +1 (1)
    Plonk!   agreed  Smile

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #9
    LdB_ECM
    Super Member
    • Total Posts : 401
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: How to use #include 2020/06/28 04:45:56 (permalink)
    0
    Feel the #pragma once typical of this forum
     
    So you are you really saying he does not need to learn?
    .. ok so I cant post a link .. weird wonder what is in it
    post edited by LdB_ECM - 2020/06/28 04:49:50
    #10
    LdB_ECM
    Super Member
    • Total Posts : 401
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: How to use #include 2020/06/28 04:50:18 (permalink)
    0
    Okay that one works

    https://riptutorial.com/cplusplus/example/3525/include-guards

     
    Is that not what he needs?
    post edited by LdB_ECM - 2020/06/28 04:51:23
    #11
    ric
    Super Member
    • Total Posts : 27953
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: How to use #include 2020/06/28 04:55:12 (permalink)
    +3 (3)
    The whole negative vibe is because "include guards" are a huge red herring in this case.
    The OP came unstuck because he was putting code into an H file, and including THAT into another H file.
    The tutorial linked to in post#7 hopefully will set them back onto the right path.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    LdB_ECM
    Super Member
    • Total Posts : 401
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: How to use #include 2020/06/28 05:26:37 (permalink)
    0
    I don't get he was putting code in the .h files ... reading original post (even again) I am getting he was duplicating the function locally to both units to prevent circular reference.
     
    If you are right then fine but just saying that isn't how I read it.
    post edited by LdB_ECM - 2020/06/28 05:27:58
    #13
    MJordan
    Starting Member
    • Total Posts : 48
    • Reward points : 0
    • Joined: 2016/02/17 18:09:49
    • Location: 0
    • Status: offline
    Re: How to use #include 2020/06/28 06:01:19 (permalink)
    +1 (1)
    Thanks all. I was really just looking for a best practice outline that works. I don't work collaboratively so I don't always know "best practice", but I always get things done. The best practice seems to be:
    1. Function Prototype in foo.h
    2. Function Code in foo.c
    3. #include "foo.h"
    4. Avoid nesting #include's
    I appreciate all of your replies. I've learned something from each.
    #14
    Jan Audio
    Senior Member
    • Total Posts : 159
    • Reward points : 0
    • Joined: 2018/09/24 08:12:24
    • Location: 0
    • Status: offline
    Re: How to use #include 2020/06/28 09:22:48 (permalink)
    +1 (1)
    You can use extern in the header to make data from the C file visible.
     
    header file :
    extern int i;
     
    code file :
    int i = 0;
    #15
    crosland
    Super Member
    • Total Posts : 2009
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: How to use #include 2020/06/28 09:46:50 (permalink)
    +1 (1)
    LdB_ECM
    I don't get he was putting code in the .h files ... reading original post (even again) I am getting he was duplicating the function locally to both units to prevent circular reference.
     
    If you are right then fine but just saying that isn't how I read it.


    Not even the bit where he clearly says "I have a function "void foo" in a file called: "myfoo.h". "
    #16
    1and0
    Access is Denied
    • Total Posts : 10985
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: How to use #include 2020/06/28 12:23:51 (permalink)
    +1 (1)
    ... and this
    MJordan
    I would also like to call "foo" from another file called: "myOtherFile.h".

     
    #17
    LdB_ECM
    Super Member
    • Total Posts : 401
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: How to use #include 2020/06/29 09:34:16 (permalink)
    0
    Sorry I don't deal with dumb and stupid on a regular basis, I assumed he was just referring to the prototype header declarations. Any dummies guide to C makes all that clear. I shall lower my expectation even further on the forum in future.
    post edited by LdB_ECM - 2020/06/29 09:38:09
    #18
    du00000001
    Just Some Member
    • Total Posts : 3834
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: How to use #include 2020/06/29 10:44:58 (permalink)
    +1 (1)
    LdB_ECM
    Sorry I don't deal with dumb and stupid on a regular basis, I assumed he was just referring to the prototype header declarations. Any dummies guide to C makes all that clear. I shall lower my expectation even further on the forum in future.



    There are 2 sorts of forum users I fear most: "EE Student" and "Experienced Hobbyist". Closely followed by members of all sorts from south/southeast Asia.
     
    Although I have to admit that some hobbyists here are valuable contributors ...

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #19
    MJordan
    Starting Member
    • Total Posts : 48
    • Reward points : 0
    • Joined: 2016/02/17 18:09:49
    • Location: 0
    • Status: offline
    Re: How to use #include 2020/06/29 11:07:47 (permalink)
    +1 (1)
    wow. You must really hate Asian, student-hobbyists. You managed to be both Racist and Condescending in one post. Why do you even bother?
     
    In this thread I've been referred to, or associated with, "Dummies", "Dumb" and "Stupid".
     
    Fortunately there are people on this forum who are willing to just answer a simple question. I guess it makes putting up with the rest worthwhile.
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5