-3

Writing a logging system and I just want an array to be stored and modified in a module (let's call it foo.c, with an appropriately-named header file foo.h) while being able to access it's contents in main.c. In foo.c I have:

unsigned char log[4096] = {0};

while main.c is as follows:

#include "foo.h"
int main(){
    extern unsigned char log[4096];

    // code

    return 0
}

Which yields the error:

error: 'log' redeclared as different kind of symbol

Am I doing this right? I tried declaring the extern log as "log[]" and "log" but those were of no avail either. A little enlightenment on the issue would be helpful. Thank you!

Justin
  • 33
  • 1
  • 6
  • Note that you should have the declaration in a header that is used both in the file where the variable is defined and in the file that uses it (`foo.h` for the header; `foo.c` for where it is defined, `main.c` for where it is used). This gives you the necessary cross-checking. You shouldn't write `extern` in an ordinary C source file; it should only appear in headers. Anywhere you feel you need to write `extern` means you aren't using a header you should be using, or the header is incomplete. Both lead to long-term maintenance problems. – Jonathan Leffler Aug 03 '16 at 20:27
  • I wouldn't declate a variable in a header. In every c file you include the header the compiler will alocate that variable. I had a very nasty bug in an oldwr project because a developer declared a global variable in a header and the header was used in two different c files. – Sitram Aug 03 '16 at 21:07

2 Answers2

2

The problem is the log function from <math.h>. The simplest solution is to rename your array.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • That's why it kept telling me it was a function... thank you so much and for a prompt response! Gosh such a simple solution. – Justin Aug 03 '16 at 20:04
  • 1
    Your not the only person to have been bitten by name collision before. It is one sure way to learn that error messages "*really do mean what they say...*". – David C. Rankin Aug 03 '16 at 21:55
0

Try moving the extern declaration outside of main since log is an global variable.

The keyword extern basically tels the compiler to not spit out an error because the symbol is declared in another module. If the symbol is not found at linking, then you will get an error.

Sitram
  • 1,432
  • 1
  • 14
  • 17