I am taking a C++, intro to CS class, and we are now starting to implement data abstraction for OOP. I am required to hide the class definitions in a separate .cpp file and to use a header file. Until now, I have been using the default settings in VSCode in my Macbook pro for creating single file programs, so I decided to create a test program to make sure the compilation, include and link would work properly before creating my entire project, but now when I try to build the program, I am receiving an error. I have tried for days to resolve the problem using potential solution without success. I am hoping someone here can lead me in the right direction.
I have included the contents of the 3 test files (test.cpp, test.h and testImp.cpp), the c_cpp_properties.json, launch.json, settings.json and task.json files and the content of the error message.
Any assistance setting up VSCode to complete including header files would be greatly appreciated since I will be having more projects with this requirement.
Thank you, James.
test.cpp
#include <iostream>
#include "test.h"
using namespace std;
int main()
{
cout << testCalculation(5, 7) << endl;
cout << endl;
return 0;
}
test.h
#ifndef test_h
#define test_h
int testCalculation(int, int);
#endif /* test_h */
testImp.cpp
#include "test.h"
int testCalculation(int x, int y) {
int sum = int();
sum = x + y;
return sum;
};
c_cpp_properties.json
"configurations": [
{
"name": "Mac",
"includePath": [
"${default}",
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/g++",
"cStandard": "c17",
"cppStandard": "c++11",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: g++ build active file"
}
]
}
setting.json
{
"files.associations": {
"iostream": "cpp"
}
}
task.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
ERROR
Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g /Users/jamesreal/cpp/cs-2/project-1/test.cpp -o /Users/jamesreal/cpp/cs-2/project-1/test
Undefined symbols for architecture x86_64:
"testCalculation(int, int)", referenced from:
_main in test-5cbf98.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)