1

I want to include a variable declared in an external file in C.

My project structure looks like this.

foo.h
foo.c
variable.c
main.c

What I'm doing right now is

/* main.c */

#include "foo.h"

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


int main() {
    bar();
    printf("%d\n", a);
    return 0;
}

/* foo.h */

#ifndef FOO_H
#define FOO_H

#include "variable.c"

extern const int a;
extern const int b[];


int bar();
#endif

/* foo.c */

#include "foo.h" 
#include <stdio.h>

int bar () {
    printf("tururu\n");
    printf("%d\n", b[0]);
    return 0;
}

/* variable.c */

const int a = 2;
const int b[3] = {1, 2, 3};

The variables I want to define this way are constant (read-only).

I do not know beforehand the size of the array, it depends on the definition in variable.c.

So my question is:

  • Is this the proper way to include some constant variables from an external source?

  • If it is, what am I doing wrong and how could be fixed?

Thank you

EDIT: I have updated my code with an example that can be tested. This code is not compiling because it says 'b' is undeclared in function bar. If I comment out the printf in bar it compiles and run. So the variables can be seen by main but not by foo.c?

EDIT2: Variables included this way are read-only. I have updated the code and add the include of foo.h in foo.c and now the compiler tell that there are multiple definitions of 'a' and 'b'

EDIT3: Clean up the code and the question trying to be more clear.

pepelu
  • 368
  • 1
  • 3
  • 18
  • 2
    possible duplicate of [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) – Chostakovitch Jan 26 '15 at 11:05
  • I have read that before writing my question but it didn't answer mine. My problem is that I can't define and initialize my variable in foo.c I need to to that in an external file and in that question this issue is not observed. But thanks for linking, it is a very useful answer there. – pepelu Jan 26 '15 at 11:55
  • I also have updated the question in order to make clear the differences. – pepelu Jan 26 '15 at 12:14
  • foo.c needs to include foo.h (to make a declaration visible), foo.h should not include variable.h, and for most cases definitions don't go into headers (this header must not be included by more than one file if it defines a variable). – mafso Jan 26 '15 at 12:21
  • Problem 1: it _never_ makes any sense to declare or define non-constant variables inside a h file. Problem 2: it _never_ makes any sense to use global variables. Instead, use file scope static variables and setters/getters, [as demonstrated here](http://stackoverflow.com/questions/23655421/does-c-provide-a-way-to-declare-an-extern-variable-as-read-only-but-define-it/23656041#23656041). Problem 3: your program design is fundamentally broken and needs to be re-made from scratch: it doesn't make any sense to distribute the variables like you do. – Lundin Jan 26 '15 at 12:23
  • They are constant variables, the problem is that I want to separate the variables (which can vary between different implementations of the program) and the algorithm which is always the same. After compiling the variables are constant. – pepelu Jan 26 '15 at 12:28
  • Declare it `const` then, rename variable.h to variable.c (removing the header guards), and include foo.h in it. Changing the values (or the length) then only requires modifying variable.c. As an aside, other files than variable.c don't know the size of the array. – mafso Jan 26 '15 at 12:34
  • I have deleted the definition in foo.h, changed variable.h to variable.c and put in variable.c the variables as 'const int' but the compiler still says that there are previous definitions of a and b – pepelu Jan 26 '15 at 12:44
  • Did you also do `extern const int b[];` in foo.h? (And remove the `#include "foo.h"` in foo.h; what is this supposed to do?) – mafso Jan 26 '15 at 12:45
  • (Yes, sorry that was a mistake while copy pasting, that include is in foo.c not in foo.h) I have updated the code and it is still not working, it is saying there are redefinition of a and b. – pepelu Jan 26 '15 at 12:50

3 Answers3

2

The variables must be defined in a c file, while in the header you can put the extern reference

/* foo.h */
#ifndef FOO_H
#define FOO_H

#include "variable.h"

extern int a;
extern int b[];

#endif

/* foo.c */

int a = 2;
int b[3] = {1, 2, 3};
  • The thing here is that I cannot define the variables in foo.c I need to do that from an external file. – pepelu Jan 26 '15 at 11:51
2

Remove #include "variable.c" from foo.h, and your code should work.

You're basically using extern to tell your compiler that whatever you use in a declaration after the extern keyword will be defined in another .c source file that is linked separately. In your case, this .c file is variable.c.

And yeah, take care to never #include .c files. This can easily lead to the linker going haywire.

1

It is a good practice to declare variable in header file and define in c file

more detail: Variable Definition vs Declaration

In your variable.h, you just define two variable

So, it is not the proper way

Besides the code related to the array is not wrong, you can put it in a .c file

simon_xia
  • 2,394
  • 1
  • 20
  • 32
  • Unless the variable is constant (read-only), it is very bad practice to declare it in a header file. Don't use global variables. – Lundin Jan 26 '15 at 12:17