-2

I'm trying to figure out how to create a makefile that will create object files and then compile them into an executable but when I try to make an object file that uses a variable that was declared in another file it won't compile to an object file. In other words if I have main.c that has a global variable var and I have another file called other.c which uses var but doesn't declare it like main.c did. Is there a way to compile them both to object files and link them so other.c sees the declaration of var in main.c? I was under the impression there was but I can't figure it out because when I do gcc -c other.c I get an error about var. (I know you can make var extern to fix this issue but I need to able to do this without doing that)

Here's the error messages I get.

   foundations.c: In function ‘FoundationC’:
   foundations.c:2:2: error: ‘DAYS’ undeclared (first use in this function)
  DAYS=DAYS+10;
  ^
  foundations.c:2:2: note: each undeclared identifier is reported only once  for each function it appears in
foundations.c:3:2: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
  printf("The FoundationC Contractor completed the project - DAY %d\n",DAYS );fflush(stdout);
  ^
foundations.c:3:85: error: ‘stdout’ undeclared (first use in this function)
  printf("The FoundationC Contractor completed the project - DAY %d\n",DAYS );fflush(stdout);

So DAYS is a variable that was declared in main.c not foundations.c

EDIT: Solved I added the needed stl header file and global variable to the other file and then it was able to compile as an object file. Sorry about this question. I made it thinking that object file linking had the ability which would make that unnecessary but I guess not.

codehelp4
  • 132
  • 1
  • 2
  • 15
  • [This answer](http://stackoverflow.com/a/32760257/841108) has links to other answers with `Makefile`-s. Your question is unclear. Show your makefile, and have some *common* header file (e.g. `myheader.h`) which would be `#include`-d in every `*.c` translation unit. Read documentation of [GNU make](http://www.gnu.org/software/make/) and of [GCC](http://gcc.gnu.org/) – Basile Starynkevitch Nov 26 '15 at 08:05
  • ***WHY*** must you not use `extern`? This is exactly the situation that calls for it. – Beta Nov 26 '15 at 13:57
  • because for my homework I'm not suppose to use extern the code with the global variable was given to me and I'm not suppose to edit it – codehelp4 Nov 26 '15 at 23:35

1 Answers1

1

...but when I try to make an object file that uses a variable that was declared in another file it won't compile to an object file.

This has nothing to do with a makefile. Commands in the makefile will for example start the compiler on a .c file to produce an object file. Either this compilation succeeds, or it does not in which case the compiler gives you one or more error messages.

The problem you describe of a variable declared in another .c file will NOT halt the compiler; it will just compile the .c file and will at most give warning messages that a variable is not declared. But it will produce an object file.

At link time, the linker must be given all object files so produced (a command in the makefile starts the linker with the list of names on the command line) and then will produce an executable. If now the variable cannot be found in the object files, the link process will fail and no executable is produced. But all object files will have been produced (if there aren't errors in the compilation step).

Maybe his helps to re-formulate your question.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • well it didn't produce and object file for me. It just gives me these messages. I've added the error messages to the main question. – codehelp4 Nov 26 '15 at 23:39
  • It seems your compiler is flagging some warnings as errors (some compilers do this when the error checking level is set high). Formally, the compiler should flag `DAYS` being undeclared as a warning, assuming it will be an `int`, which is correct in this case. Declaring `extern int DAYS;` will solve the problem (you explicitly tell the compiler the variable does not exist in this compilation unit). – Paul Ogilvie Nov 27 '15 at 09:46