I installed Visual Studio Code on Mac with Catalina in order to learn C++. Installed extensions C/C++
, C/C++ Extension Pack
, C++ Intellisense
, CMake Tools
and Code Runner
.
To test VSCode I tried running the following code:
bye.cpp:
#include <iostream>
void tryMe(int s) {
std::cout << "ok";
}
bye.h:
void tryMe(int s);
hello.cpp:
#include <iostream>
#include "bye.h"
int main() {
tryMe(3);
return 0;
}
But it doesn't run as it results on compiling error:
$ cd "/Users/x/Workspace/LearnCPP/" && g++ hello.cpp -o hello && "/Users/x/Workspace/LearnCPP/"hello
Undefined symbols for architecture x86_64:
"tryMe(int)", referenced from:
_main in hello-ef5e99.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 why the problem is happening: the compilation is not including the bye.cpp
file so it doesn't recognise the function. If I compile through the Terminal using g++ hello.cpp bye.cpp -o hello
it compiles good and runs as expected.
c_cpp_properties.json:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
I've searched and seen some articles referring to a "task" file but couldn't understand how to implement it or from where does it come from.