1

extrn.c

#include <stdio.h>

extern int var;

int main()
{
    printf("%d", var);
    return 0;
}

var.c

int var = 5;

I go to file extrn.c and I run the code and I get this:

undefined reference to `var'

and this is what my output is looking like:

[Running] cd "/home/buff/Documents/Coding/C/C_programming_NESO/" && gcc extrn.c -o extrn && "/home/buff/Documents/Coding/C/C_programming_NESO/"
/usr/bin/ld: /tmp/ccoKgi02.o: in function `main':
extrn.c:(.text+0xa): undefined reference to `var'
collect2: error: ld returned 1 exit status
[Done] exited with code=1 in 0.093 seconds
Shrayansh
  • 13
  • 4

1 Answers1

1

Compile your both C files together to fix this undefined reference error.

For GCC
gcc extrn.c var.c -o main
For clang
clang extrn.c var.c -o main
Darth-CodeX
  • 2,166
  • 1
  • 6
  • 23
  • not working... ... – Shrayansh May 23 '22 at 19:53
  • @Shrayansh You are just running `gcc extrn.c -o extrn` run instead `gcc extrn.c var.c -o main`. ***Accept my answer if it worked*** – Darth-CodeX May 24 '22 at 05:27
  • buff@d605:/media/buff/92AA5210AA51F0E9/Documents/Coding/C/C_programming_NESO$ gcc extern.c var.c -o main gcc: error: extern.c: No such file or directory – Shrayansh May 25 '22 at 04:36
  • @Shrayansh Use `cd` command and go into that directory in which both files are stored. – Darth-CodeX May 25 '22 at 04:54
  • buff@d605:~/Documents/Coding/C/C_programming_NESO$ pwd /home/buff/Documents/Coding/C/C_programming_NESO buff@d605:~/Documents/Coding/C/C_programming_NESO$ gcc extern.c var.c -o main gcc: error: extern.c: No such file or directory Brother, thank you for your effort, it is not much of an issue, leave it, and thanks again. – Shrayansh May 26 '22 at 09:20