I am using VSCode and the code runner extension to try to run a simple c++ project with include files.
The project is made of a main.cpp:
#include <iostream>
#include <time.h>
#include "cval.hpp"
using namespace std;
int main(void)
{
putHello();
return 0;
}
A hello.hpp header file:
#ifndef CVAL_H
#define CVAL_H
void putHello(void);
#endif
And the hello.cpp:
#include <iostream>
using namespace std;
void putHello(void){
cout<<"Hello"<<endl;
}
If I define putHello()
in main.cpp, the code compiles. If I include #include cval.cpp
the code compiles.
However, when I only include the cval.hpp
header and use the Code runner extension, I get the following error:
Undefined symbols for architecture x86_64:
"putHello()", referenced from:
_main in main-a07db2.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I understand this is a problem that I am not compiling cval.cpp
so the linker cannot find it and link it properly. However, is there a way to get VSCode to automatically compile files that were included in headers?
I don't want to have to specify it all the time or have to include the .cpp files, and it is able to include these files with out a problem in my C code projects.