I am trying to separately compile each of the c file and then link them together as a single executable file. Following are the 2 c files :
file1.c
#include <stdio.h>
void display();
int count;
int main() {
printf("Inside the function main\n");
count = 10;
display();
}
file2.c
#include <stdio.h>
extern int count;
void display() {
printf("Sunday mornings are beautiful !\n");
printf("%d\n",count);
}
But when I try to compile them , I get some errors :
When I compile file1.c
gcc file1.c -o file1
/tmp/ccV5kaGA.o: In function `main':
file1.c:(.text+0x20): undefined reference to `display'
collect2: ld returned 1 exit status
When I compile file2.c
gcc file2.c -o file2
/usr/lib/gcc/i686-redhat-linux/4.6.3/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/cczHziYd.o: In function `display':
file2.c:(.text+0x14): undefined reference to `count'
collect2: ld returned 1 exit status
What mistake am I committing ?
gcc file1.c file2.c -o outputshould do it. - chris