I am trying to break my code into multiple files in c++. My main file is base.cpp in which I am trying to import foo.cpp. But it shows the following error during compilation.
Undefined symbols for architecture x86_64: "foo()", referenced from: _main in base-a3f2f3.o ld: symbol(s) not found for architecture x86_64
My base.cpp file is :
#include<bits/stdc++.h>
#include "foo.h"
using namespace std;
int main(){
foo();
cout<<tot<<endl;
cout<<"bye"<<endl;
}
My foo.cpp is :
#include<bits/stdc++.h>
using namespace std ;
void foo(){
cout<<"hello world"<<endl;
}
int tot = 90;
My foo.h file is:
#ifndef FOO_H
#define FOO_H
int tot;
void foo();
#endif
I use the following command to compile my c++ files successfully : g++ -std=c++14 filename.cpp && ./a.out
I use Mac. My code editor is VS Code.
My cpp configuaration is as follows :
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
Thanks.