This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Variable declared but never referenced warning

Other Parts Discussed in Thread: CC1200

I am using SmartRF to generate configurations for mi RF chip. It generates a .c and here is an example what is in the .c file:

#include "CC1200_address.h"

static const registerSetting_s slowTransmitSettings[] =
{
  {CC1200_ADDRESS_IOCFG3,              0x30},
  {CC1200_ADDRESS_IOCFG2,              0x02},
};

This array has a lot of elements but i deleted them to not clog the question.

Now I want to use that array in my main. I just did:

#include "CC1200_slow_transmit_config.c"

//extern const registerSetting_s slowTransmitSettings[];

void main(void)
{
// Code in which I use the variable and send it to some functions. So it is definitely used.
}

I constantly get the warning #179-D variable "slowTransmitSettings" was declared but never referenced

The commented section in the main file is somethign I tried to resolve the warning but it didn't work. Can someone help?

I am using the TI 15.12.3 LTS compiler. Not GCC.


Thanks in advance.

  • I'll bet you are including CC1200_slow_transmit_config.c file in another .c file (let's call it foo.c), but foo.c does not use slowTransmitSettings. Please tell us the name of the file containing main, and show us the exact compiler warnings. Cut and paste them from the build console window.

    The recommended way to deal with variables that need to be shared is to define them in a C file and declare them in a .h file that you can include everywhere. It is not recommended to include .c files.

    Create a file named CC1200_slow_transmit_config.h with this declaration:
    const registerSetting_s slowTransmitSettings[];
    Add the file CC1200_slow_transmit_config.c to your project, but do not include it in any other files.
  • I am not including it in any other file. I checked it now to be sure. 

    There is only one warning, one line and this is it:

    Description Resource Path Location Type
    #179-D variable "slowTransmitSettings" was declared but never referenced | CC1200_slow_transmit_config.c | /rf_Rx_spi_test | line 16 | C/C++ Problem

    The variable is in my code on line 16. I deleted a bunch of comments in my first post so it doesn't clog it.

    I will try that method that you suggested.

  • Well, I tried a few things.

    1) I didn't include the CC1200_slow_transmit_config.c in ANY file and I still did get the warning. I double checked everything. I think it's because of the compiler.
    2) I made a CC1200_slow_transmit_config.h file and made a declaration in it. Then I included CC1200_slow_transmit_config.c.h in the .c file with the same name. In the main.c I included CC1200_slow_transmit_config.h and I get the error that slowTransmitSettings is redefined.

    Nothing works like it should. If you want I can attach my whole project.

    EDIT1: I just searched online. In the .h file I need to write extern const ...... That was the error.

  • I'm not sure whether that fixed your original warning or the redefinition error. If you are still having trouble, yes, please attach your project.
  • Some observations:

    jasaleja said:

    static const registerSetting_s slowTransmitSettings[] =

    Notice the static keyword on the variable.  That means this variable has internal linkage; in other words, it is probably only meant to be used within that same .c file.  If that is true, then it would actually be improper to declare it (extern or no) in the .h file.  You mentioned that you wanted to use the slowTransmitSettings variable in your own code.  The internal linkage would seem to indicate to me that the designer of that software module didn't intend for external consumers to need to access it to achieve the behavior the module was intended to do.  Perhaps you can look at the API of the module and see if there is a different way to get the information you need.  If there is not, then you might consider removing static symbol, and doing what you did (that is creating a .h file with the interface you want to interact with, extern declaration, etc.)

    Another possibility is that the designer of that module did intend for the CC1200_slow_transmit_config.c file to be #included in your own .c file(s).  As Archelogist indicated, this would be a less common design, but depending on the other content of the file, it might be OK.

    As for the warning itself, does CC1200_slow_transmit_config.c reference the slowTransmitSettings symbol?  If it does not, then when CC1200_slow_transmit_config.c is added as a source file to your project and you do a build, that file will be compiled and it is proper for the warning to be generated.  If this is the case, then I would say the problem is not with the compiler, but with the auto-generator that created the file and put in it a symbol that is apparently not needed.  If CC1200_slow_transmit_config.c does reference slowTransmitSettings and you still get a warning, then there might be a compiler problem.

    Another possibility again refers to what I above called the "less common design".  It is that the designer did not intend for CC1200_slow_transmit_config.c itself to be added as a source file directly to the project, and only #included in your .c file(s).  In this case, CC1200_slow_transmit_config.c would never be compiled by itself, and only it would only be included into your code, which could then reference slowTransmitSettings, and the warning would go away.

    As a disclaimer, I mostly work in C++, so I may be incorrect on some of the C rules (linkage, etc.)

  • Had to reread that a few times but I think I got everything except: 

    As for the warning itself, does CC1200_slow_transmit_config.c reference the slowTransmitSettings symbol?
    How d oyou mean does it reference the symbol? I know about declaration and definition, but what is the symbol?

    As for the other things you suggested. I practically gave the whole CC1200_slow_transmit_config.c  file. I just shortened the array. The designer of that .c file is SmartRF studio, a program we use for generating RF configurations, made by TI. I don't know why they put static prefix because it means that the variable is only gonna be used in that file, but I left it there because I didn't know if there was something else to it. And yes, extern and static can't be used at the same time, the compiler gives and error. Static would be something like private and extern would be something like public in C++. I even think this was the way they made objects in the early C++.

    Now, another thing. I know that people mostly don't include .c files but I wanted to try it. When I erase the static prefix, the compiler would always give me the error:

    Description Resource Path Location Type
    #10056 symbol "slowTransmitSettings" redefined: first defined in "./CC1200_slow_transmit_config.obj"; redefined in "./main.obj" rf_Rx_spi_test C/C++ Problem

    When I added the static prefix, the compiler gave another warning:

    Description Resource Path Location Type
    #179-D variable "slowTransmitSettings" was declared but never referenced CC1200_slow_transmit_config.c /rf_Rx_spi_test line 16 C/C++ Problem

    This was very weird to me and that is why I posted this question in the first place. Going old school did solve this warning and I am happy because of it, but I didn't know why these warning were happening. The weirdest part was when I didn't include the .c file anywhere(the static prefix was present at that time) and the compiler still gave the warning even though the file was never used.

  • jasaleja said:

    How d oyou mean does it reference the symbol? I know about declaration and definition, but what is the symbol?

    Sorry, my phrasing wasn't the best.  I meant, does any code in CC1200_slow_transmit_config.c actually use slowTransmitSettings, more than just the variable defintion?  From your description, it sounds like it does not.

    When CCS does a build of a project, it does a compile of all the individual source files for that project; that is, any .c files that you see under the project inside Project Explorer will be compiled.  And it looks at each source file one at a time, individually.  When it gets to CC1200_slow_transmit_config.c, it compiled it, and it sees a variable slowTransmitSettings that isn't used anywhere in that file, and further it sees that variable has internal linkage (static), so it knows it can't be used by any other source file either, so it issues the warning.

    So at this point I think there are some ways you could fix it.

    Method A

    1. Create the header file CC1200_slow_transmit_config.h, with the interface you want (extern const registerSetting_s slowTransmitSettings[];, etc.).  This will make it so your other source files can know about the existence of such a variable.

    2. Remove static from the variable definition in CC1200_slow_transmit_config.c.  This will make it so there is only one slowTransmitSettings object in your program.  That object will have external linkage so your other source files are allowed to use it.

    3. #include the header file you created in step 1 in your main.c (or whichever of your source files you want to access slowTransmitSettings).

    Method B

    1. #include CC1200_slow_transmit_config.c in your main.c (or whichever of your source files you want to access slowTransmitSettings).  In this manner, your main.c will have its own copy of slowTransmitSettings.  Why its own copy?  Because slowTransmitSettings is declared static.  Thus you could #include CC1200_slow_transmit_config.c in multiple files, and they will all have their own version.

    2. Configure the project so that it does not build CC1200_slow_transmit_config.c by itself.  You can do this by Project Explorer > right-click CC1200_slow_transmit_config.c > Exclude from Build.  This will prevent CCS from trying to compile CC1200_slow_transmit_config.c and thus avoid running into the case where it sees a variable that is not used.  Note, functionality you need will still be present when it compiles your main.c, because you #included CC1200_slow_transmit_config.c into there.

    Method C

    1. Rename CC1200_slow_transmit_config.c to CC1200_slow_transmit_config.h

    2. #include CC1200_slow_transmit_config.h in your main.c (or whichever of your source files you want to access slowTransmitSettings).  In this manner, your main.c will have its own copy of slowTransmitSettings.  Why its own copy?  Because slowTransmitSettings is declared static.  Thus you could #include CC1200_slow_transmit_config.h in multiple files, and they will all have their own version.

    Methods B and C are basically the same.  And the only difference with Method A is that in A there is only once instance of slowTransmitSettings, where as in B and C, there will be one instance each time you #include.

  • That's it. That the was the answer I was searching for. I knew there was some catch with copies of the same variable but I didn't know when that happens or why. I heard only once in one class while I was studying but it was a few years ago.

    Thank you very much user347219. You could get a nice username for change. :)