1

Hi StackOverflow wizards:

I have the following three simple C files:

// File 1
#include "mainPgm.h"
void file1() {
  printf("N1 is now %d.\n", n1);                    
}

// File 2
#include "mainPgm.h"
void file2() {
  printf("N2 is now %d.\n", n2);                    
}

// File 3
#include "mainPgm.h"
void file3() {
  printf("N3 is now %d.\n", n3);                    
}

And, of course a main program:

#include <stdio.h>
#include <stdlib.h>
#include "mainPgm.h"

int main() {
  int n1 = 65536,
      n2 = 256,
      n3 = 16;

  file1();
  file2();
  file3();
}

Finally, a header file:

#include<stdio.h>
void file1(), file2(), file3();
extern int n1, n2, n3;

All these are compiled with a simple gcc command:

gcc -std=gnu99 -O2 -o jj -Wunused file1.c file2.c file3.c mainPgm.c

This results in the following errors:

mainPgm.c: In function ‘main’:
mainPgm.c:8:7: warning: unused variable ‘n3’ [-Wunused-variable]
mainPgm.c:7:7: warning: unused variable ‘n2’ [-Wunused-variable]
mainPgm.c:6:7: warning: unused variable ‘n1’ [-Wunused-variable]
/tmp/ccVQjFHY.o: In function `file1':
file1.c:(.text+0x2): undefined reference to `n1'
/tmp/ccZqyI0n.o: In function `file2':
file2.c:(.text+0x2): undefined reference to `n2'
/tmp/ccbpJOpN.o: In function `file3':
file3.c:(.text+0x2): undefined reference to `n3'
collect2: error: ld returned 1 exit status

I thought that defining n1, n2, and n3 in mainPgm.h would serve to declare the variables, and their definition in mainPgm.c would define them. No so! Where did I go wrong?

TIA!

Boffin
  • 161
  • 8
  • See [How do I use `extern` to share variables between source files in C?](http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c/1433387#1433387) – Jonathan Leffler Dec 04 '14 at 16:12
  • You might (or might not) care to note that: (1) Your main program does not use any code from `` or `` (so those includes are unnecessary); (2) The header `"mainPgm.h"` doesn't need to include `` as none of the declarations in it depend on any declarations from `` — but if you removed `` from `"mainPgm.h"`, you'd need to explicitly include `` in each of the `fileN.c` files. In the `mainPgm.c` file, you actually included `` twice. This is harmless; standard C headers are self-contained and idempotent. Your header should be too. – Jonathan Leffler Dec 04 '14 at 16:18
  • I removed the stdio and stdlib declarations from mainPgm.c, and had no compilation problems. – Boffin Dec 04 '14 at 17:24

2 Answers2

3

All of your variables are local variable inside main(), they will never be visible from outside that function.

Move them out so they're global:

  int n1 = 65536,
      n2 = 256,
      n3 = 16;

int main() {
  file1();
  file2();
  file3();

  return EXIT_SUCCESS;
}
unwind
  • 391,730
  • 64
  • 469
  • 606
2

You defined the variables in your main() function, on the stack.

You could define them in the same file but outside of your main function.

Alternatively a common c idiom is to use the preprocessor to declare the variables as extern in all but one instance of your header file.

Simon Elliott
  • 2,087
  • 4
  • 30
  • 39