Thread: Multiple C files, One H file

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

    Multiple C files, One H file

    Good afternoon.
    I am a hardware guy who is learning code. Can you please help me identify a problem...Using CCS and an MSP430 part. I have a rc.c (contains main) and a functions.c....I have a .h file which contains some #defines, a few const unsigned int and declarations. I also have a wrapper around all code within this file #ifndef RC_H_, #define RC_H_, #endif. Finally I have #include of the header file in both main and functions.

    I am continually getting the error during compile times showing
    #10056 symbol x redefined: first defined in "./functions.obj"; redefined in "./rc.obj"

    Any help would be appreciated....Do you have to compile files in a certain order? Do I need to do something specific in CCS? I have found that if I exclude functions.c from the build and copy all the definitions into the header file all compiles and works...

    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Post the header file. You likely have something wrong it it.
    As in that part needs rewritten or move to one of the C source files.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Code:
    #include <msp430.h>
    #include <stdlib.h>
    #include <stdint.h>
    /*
     * rc.h
     *
     *  Created on: Mar 19, 2020
     *      Author: ridge
     */
    
    #ifndef RC_H_
    #define RC_H_
    
    //macros, etc.
    const unsigned int Customer_ID = 0xa316;
    const unsigned int CRC_Init = 0xFFFF;
    
    #define PAYLOAD_SIZE 3 //does not include CRC
    #define TENTHSEC 3277
    
    #define st(x)      do { x } while (0)
    
    #define IOconfig st(P1DIR = 0x01; /*input*/ \
                           P1OUT = 0x03; P1REN = 0xFE; /*pulldowns..p1.1 - pullup*/ \
                           P1IES = 0x02; P1IE =0xFE; /*interrupts*/ \
                           P2SEL1 |= BIT5 | BIT6; P2SEL0 &= ~(BIT5 | BIT6); /* USCI_A0 UART operation*/ \
                           P3DIR |= BIT4; P3SEL1 |= BIT4; /*output 3.4 with SMCLK*/ \
                           PJSEL0 |= BIT4 | BIT5; /*LFXT*/\
                           PM5CTL0 &= ~LOCKLPM5; P1IFG = 0x00; /*disable default Hi-Z, Must clear IFG here else P1.1 will interrupt*/)
    
    enum switches{SW1 = 1,SW2,SW3 = 4,SW4 = 8,SW5 = 16,SW6 = 32,ALL = 64}; //ALL will flip the state of all switches
    
    //declarations
    void initClockTo16MHz(void);
    void initUART(void);
    unsigned int CRC(char *payload);
    void initTimerB0(void);
    void resetTimerSeq(void);
    //unsigned int CCITT_Update(unsigned int init, unsigned int input);
    
    #endif /* RC_H_ */

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The header looks OK. I do think the two defines IOconfig and st are not normal but they should be OK.

    Post the two lines of code the Compiler is complaining about.

    Edit: I think this line may be causing the error.
    Code:
    #define st(x)      do { x } while (0)
    Tim S.
    Last edited by stahta01; 03-28-2020 at 01:49 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Hi Tim....
    Well I would love to but....things have gone down hill quickly...I will have to get back to you on this when the error comes back...At the moment the EVM has decided it won't try and load code....

    MSP430: Error initializing emulator: A firmware update is required for the MSP430 Debug Interface (MSP-FET430UIF / MSP-FET / eZ-FET). Click the "Update" button to update the firmware and launch your debug session (this may require several update steps). DO NOT UNPLUG THE INTERFACE DURING THE UPDATE.
    MSP430: File Loader: Verification failed: Values at address 0x00200 do not match Please verify target memory and memory map.
    MSP430: GEL: File: D:\Customers\Firmware\DSP-RC\Debug\DSP-RC.out: a data verification error occurred, file load failed.

    Sorry for the inconvenience....Hoping I can resolve this
    Steve

  6. #6
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Tim...
    I think I am back in business....It is not complaining so much about a line of code....Compiler spits this out

    Description Resource Path Location Type
    #10056 symbol "CRC_Init" redefined: first defined in "./functions.obj"; redefined in "./rc.obj" test C/C++ Problem
    #10056 symbol "Customer_ID" redefined: first defined in "./functions.obj"; redefined in "./rc.obj" test C/C++ Problem

    Steve

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that these are defined in the header:
    Code:
    const unsigned int Customer_ID = 0xa316;
    const unsigned int CRC_Init = 0xFFFF;
    You would need to move them to a source file, replacing them with forward declarations in the header:
    Code:
    extern const unsigned int Customer_ID;
    extern const unsigned int CRC_Init;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Thank you....I just ran across this....
    So basically I use extern in the .h file and in the appropriate .c file I define it? Then if I do this will the other .c files be able to use this value assuming of course I put it at the top of the .c file such that it is global?

    Thanks
    STeve

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It doesn't really matter where you put it in the source file, so long as it is only one source file (so they are only defined once) and at file scope (so that they match the forward declarations).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Do I need to do the same with function definitions? Meaning I have a .c file with function definitions only. My .h file contains the declarations but I do NOT have extern before each declaration....

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, but you don't need the extern for function declarations as they have external linkage by default.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merging multiple text files to a new file
    By aengland643 in forum C Programming
    Replies: 1
    Last Post: 07-28-2017, 11:17 AM
  2. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  3. single File Pointer to Multiple Files
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 12:49 AM
  4. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM

Tags for this Thread