Thread: Can someone please tell me how to have these four files compile without errors??

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    43

    Can someone please tell me how to have these four files compile without errors??

    junk.h
    Code:
    #ifndef JUNK_H_#define JUNK_H_
    
    
    char r_getVersion[TRANSMIT_SIZE] = {'E','R','_','C','M','D','#','T','3'};
    
    
    struct s_rf{
        char command[10];
        boolean ack;
    };
    
    
    void configRadio(void);
    
    
    #endif /* JUNK_H_ */
    junk2.h
    Code:
    #include <stdint.h>#include <stdlib.h>
    
    
    #ifndef JUNK2_H_
    #define JUNK2_H_
    
    
    #define TRANSMIT_SIZE 9
    
    
    typedef enum {F, T} boolean;
    
    
    #endif /* JUNK2_H_ */
    junk.c
    Code:
    #include <string.h>#include "junk.h"
    #include "junk2.h"
    
    
    
    
    void config(void)
    {
    
    
        extern char length;
        extern char *cmd;
        extern struct s_rf s_LPRS;
    
    
        length = sizeof(r_getVersion);
        cmd = memcpy(s_LPRS.command, r_getVersion, length);
        s_LPRS.ack = T;
    }
    main.c
    Code:
    #include <string.h>#include "junk2.h"
    #include "junk.h"
    
    
    struct s_rf s_LPRS, *pyld;
    char *cmd, length;
    
    
    int main(void)
    {
        cmd = memcpy(s_LPRS.command, r_getVersion, sizeof(r_getVersion));
        s_LPRS.ack = T;
        pyld = &s_LPRS;
        config();
    }
    I get undefined TRANSMIT_SIZE and boolean when I compile....If I include junk2.h within junk.h I then get r_getVersion has been redefined error....defined in junk.obj then redefined in main.obj. Can someone please show me how to set up these four files to be error free? Also I was under the impression that you should NOT include a .h file within another .h, is this correct?

    Thank you

  2. #2
    Registered User
    Join Date
    Mar 2020
    Posts
    43
    junk.h file is really void config(void)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,216
    Code:
    char r_getVersion[TRANSMIT_SIZE] = {'E','R','_','C','M','D','#','T','3'};
    This should be in a source file.

    This should be in your header file.
    Code:
    extern char r_getVersion[];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2020
    Posts
    43
    But the char r_getVersion.....is one of MANY commands that I will be sending to a piece of hardware...I don't want to clutter my source file with these constants....Is there a way to keep these in the header file....maybe they shouldn't be defined as type char??? but if I #define them then I cannot use them in memcpy and sizeof functions??

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    972
    It might be a good idea to use defines in a header file.
    Code:
    // header file
    #define Cmd_GetVersion "ER_CMD#T3"
    #define Cmd_GetPower   "ER_CMD#P?"
     
     
    // code file
    strncpy(s_LPRS.command, Cmd_GetVersion, TRANSMIT_SIZE);
    I'm assuming TRANSMIT_SIZE is the size of command.
    Last edited by john.c; 04-03-2020 at 12:36 PM.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Errors, Please Help
    By jiahwa008 in forum C++ Programming
    Replies: 3
    Last Post: 03-17-2011, 03:58 AM
  2. Compile Errors, Please Help
    By jiahwa008 in forum C++ Programming
    Replies: 11
    Last Post: 03-11-2011, 05:23 AM
  3. Classes in header files - compile-time errors.
    By kbro3 in forum C++ Programming
    Replies: 23
    Last Post: 08-05-2009, 10:44 AM
  4. Compile errors in stl header files
    By Swordsalot in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2008, 02:41 AM
  5. Dev-c++ 4.0 compile errors
    By DigitalNOISE in forum C++ Programming
    Replies: 9
    Last Post: 07-12-2002, 04:06 AM

Tags for this Thread