I'm learning C++ from scratch, and currently I'm practicing how functions can be separated to their own files using separate header files and cpp-files. I'm trying this out with a really simple example where I'm trying to print "Hello" with a function called printHello() declared in a separate file. This function is included to main code with header file printHello.hpp.
However, visual studio does not build my code and throws an error:
Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Build finished with error(s). The terminal process terminated with exit code: -1.
Here's my main file, function cpp-file and header file:
Main:
#include <iostream>
#include "printHello.hpp"
using namespace std;
int main(){
cout << "Testing the header files." <<endl;
printHello();
return 0;
}
printHello.hpp:
#ifndef PRINTHELLO_HPP
#define PRINTHELLO_HPP
void printHello(void);
#endif
printHello.cpp:
#include<iostream>
#include"printHello.hpp"
using namespace std;
void printHello(void){
cout << "Hello"<<endl;
}
Interestingly, if I change #Include "printHello.hpp"
to #Include "printHello.cpp"
everything works without any problems.
I'm wondering if this error could be due to wrong arguments in tasks.json file. Currently, I have the following args defined:
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
]
}
Has anyone had the same problem?