1

I am going through a C course and got to the point where we #include "myfile.h" Good news : VSCode finds "myfile.h" and can pull variables from it, such as int myvar=10;

Bad News : VSCode does not seem to identify function definition in the "myfile.c", so extern in myfunction() is seen but not defined. This results in the following

int i = myfunction();

Compilation Error: undefined reference to 'myfunction';

How can I get VSCode to recognize and use "myfile.c"?

greybeard
  • 2,249
  • 8
  • 30
  • 66
user238174
  • 21
  • 2
  • Are you sure the myfile.c has been added to the project? – fpiette May 13 '21 at 07:06
  • Seems like the linker cannot find myfile.c, how do you compile the program? – MrBens May 13 '21 at 07:15
  • @MrBens The linker never use a .c file. The compiler has to compile .c file to an object file (.obj or .o depending on tool chain) and then the linker can link it. The object files can also be grouped in a library file which in turn is used by the linker. – fpiette May 13 '21 at 08:45
  • @fpiette true, I meant myfile.o. – MrBens May 13 '21 at 10:53

1 Answers1

1

Answer found in :: undefined reference error in VScode

Basically, I had to compile all my files at once. This required me going into the tasks.json file and modifying it from ${file} to ${workspaceFolder}\*.c

This is better explained in :: https://code.visualstudio.com/docs/cpp/config-linux enter image description here

user238174
  • 21
  • 2
  • Good job finding your answer. I believe the confusion stems from not understanding that simply including the `"...h"` file does not make the functions it holds the declarations for available. To make the functions available, you must link against an object file holding machine code compiled from the function definitions. Perhaps you already knew. Many times this is compounded by trying to use an IDE without first understanding what options are needed to compile and link the final executable on the command line. (further compounded by needing to hack a `.json` file with VS) – David C. Rankin May 14 '21 at 07:17