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.

C-Code #define in main.c and call in other files

Genius 4170 points


HEllo,

 

i get an constant error message in my actual c-code. Because i am like a beginner in coding i wouldnt be suprised if i am doing something awefully wrong, so i am sure someone can easily help me:

 

The basic trouble is i have some unresolved symbols in my error replies:

unresolved symbol Temp_Array, first referenced in ./StatusAnzeige/StatusAnzeige.obj

 

What i do is the following: i have of course  a main.c in there i have few defines:

#define TEMP_NREGS 150

 

and some global static declarations:

static USHORT    Temp_Array[TEMP_NREGS];

 

Now i am working in another fule calles StatusAnzeige.c ( including the header of this file in my main.c of course)

extern USHORT  Temp_Array[TEMP_NREGS];

 

 

FIRST QUESTION: In this other file i cannot use the TEMP_NREGS define from my main-routine, but i would really like to use it, am i doing something wrong in my c-usage, i only can include part headers form other files into my main, but in my main of course there is no main header files with declarations i could use in my other code files???

As i am new to C, I dont know i simply dont see a solution, or if it is really the c-programming style which supresses my inner creativity of how i would like to use the language :) This often happens also when i am talking, language simply doesnt fit me :)

 

But enough of that, the next thing with the unresolved symbol is more important, as i cannot use my Temp_Array in other files as the main.c.

Another thing is, that when i am not even declaring this array as extern, i get other error messages, so i think the extern usage is kind of right, but something must be missing, so my compiler or the whole c -code tells me i am not allow to use the external defines Array in any other but my source file main.c

 

 

I am kind of confused and dont know any other answer but asking for Your help.

 

So thanks a lot in advance.

Best wishes.

Seb

 

 

 

  • Hi Seb,

    I think the "static" keyword might be your problem.  When used as a global (i.e. not within a function) it identifies to the compiler that the variable (or function) will only be used within that obj file - hence it is not made available when trying to build the StatusAnzeige.obj.

    You will probably find the next problem will be that the defined symbol TEMP_NREGS will not be accessible to the other files where Temp_Array is used.  I think you might be able to get away with not stating the size in the extern declarations as the size is only important when allocating the memory (the compiler doesn't check for running outside of arrays) but I don't know how that fits with the C standards.  Personally I would make the definition more accessible and include it in the extern declarations.  You could put the definition in a header file that all of the files that need it can include.  I normally put the "extern..." declarations in header file(s) rather than typing into multiple locations where it is needed (makes it easier if anything needs to change in the future) so you might want to consider that too.

     

    Regards,

    Chris.

  • Ah the fun of a newly learned programming language, or is it?

    Now to help you out a bit (I don't say this is best practice to do it like that, it's just how I work my way around the C/C++ specifics):

    The #define is only visible in the file it is defined, so if you want to use a #define in your whole project you should put it into a file and include that file in every source file. So for example you create a file and call it "defines.h" or "constants.h" something like this and there you put your #define TEMP_NREGS 150. Then you include that file in your main.h and in your statusAnzeige.h and you have the define available in both. And well you are including the same header file more than once, so be sure to use these "strange" constructs, to prevent defining stuff more than once:

    #ifndef STATUS_H_
    #define STATUS_H_

    /* i'm a status header file */

    #endif /* STATUS_H_ */

    But, for constants I would suggest you to use the const keyword and declare a variable for that like: const unsigned int TEMP_NREGS = 150u;
    This has the benefit that the compiler can do a check for type safety and generate warnings and errors, in case of a define there can be cases where you run into trouble. The compiler will optimize const variables like a define (at least that's what they said over at embeddedgurus.com (which is by the way a good resource for embedded programming)).

     

    The unresolved symbol is a problem related to how the keyword static is handled in C. It's more of a synonym to local at least outside of functions. That is, if you declare a global variable as static in main.c, the compiler and linker will think it is only used by main.c and thus are able to perform some optimizations based on that.

    Of course you don't want to have that, as statusAnzeige.c uses the Temp_Array as well.  So what you have to do is simple: remove the static keyword.

    Static inside a function does indeed what you expect it to do - the variable keeps the value between function calls.

    Hope that helped a bit.

     PS: beaten by Chris...

  • Hello,

     

    You are completly right, just deleted the static and looked up the definition of a global static, it works now, thanks a lot for that input. Wouldnt have figuerd that out in hours.

    I think i should do as you told me with the defines all together in one file available to include in every file i want to.

     

    The main problem is that i am not writing my codes all for myself but copy and pasting a lot from examples and code-snippets around the internet... but thats the fastest way to a working code.

     

    Thanks again,

    Seb

**Attention** This is a public forum