0

I am currently learning about header files in C, and i keep getting the "undefined reference" error message.

My source code:

main.c:

#include <stdio.h>
#include "answer.h"

void main()
{
printf("The answer is %f\n", WhatIsTheAnswer().answer);
}

answer.c:

#include "answer.h"

struct Answer WhatIsTheAnswer()
{
struct Answer TheAnswer;
TheAnswer.answer = 42.0;
return TheAnswer;
}

answer.h:

#ifndef ANSWER_H
#define ANSWER_H

struct Answer {
float answer;
};

struct Answer WhatIsTheAnswer();

#endif

This code works in VS, but not in VS code.

I know one possible solution is using #include "answer.c", but its problematic and i hope to find a better solution.

  • How have you configured building in VSCode? By default, if you don't have a proper `tasks.json` configuration then only the currently open source file will be built. Assuming Windows and MinGW then [the documentation](https://code.visualstudio.com/docs/cpp/config-mingw) should help. – Some programmer dude Mar 18 '23 at 18:33

0 Answers0