dummy.h
#ifndef dummy_h
#define dummy_h
extern const int dummy;
#endif
dummy.c
#include "dummy.h"
const int a = 384; //I modify the question to reflect that
const int b = 1; //dummy is the addition of a and b. I cannot
const int dummy = a + b; //change a and b. Have to stay like this.
//const int dummy = 385; //define dummy in dummy.c to avoid duplicated symbol issue.
foo.h
#ifndef foo_h
#define foo_h
#include "dummy.h"
extern const int foo;
#endif
foo.c
#include "foo.h"
const int foo = dummy + 1; //<--- The problem is that dummy is not a compile-time constant
main.c
#include <stdio.h>
#include "foo.h"
int main() {
printf("Hello World. foo = %d\n", foo);
return 0;
}
makefile
all:
cc main.c dummy.c foo.c -o dummy
clean:
rm dummy
This is a snapshot of a much larger program that I am currently working on. I understand why I can't use dummy in foo.c. However, I am not sure what will be the more elegant solution to it.
Basically, I want to define dummy variable in dummy.c and foo in foo.c, and at the same time, I need to find a way to initialize foo with dummy in foo.c.
Wonder if this can be done?