Reputation: 489
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.
Upvotes: 5
Views: 7465
Reputation: 127
Building on @Ping34 's answer, if you want this to work with any folder (relative path), not just Visual Studio Code's default workspace path, one thing needs to be tweaked.
In "tasks.json"
, inside "tasks": []
, inside "args": []
change
"${file}"
to
"*.cpp"
instead of
"${workspaceFolder}/*.cpp"
Notes
According to Visual Studio Code documentation, "${workspaceFolder}/*.cpp"
"will build all .cpp files in your current folder." This is rather misleading. It seems to build all .cpp files in your Visual Studio Code workspace folder, not necessarily the same path where the .cpp file you're currently trying to build is.
Visual Studio Code predefined variable definitions:
${workspaceFolder}
- "the path of the folder opened in VS Code"
${file}
- "the current opened file"
I would assume the reason why "*.cpp"
works is because ${file}
only opens one single file, so linking fails because linking requires multiple files, so "*.cpp"
is essentially a wildcard for multiple .cpp files.
Upvotes: 1
Reputation: 1
you can try adding your command line to tasks.json file. To be able to create tasks.json file you can type and select "tasks:Configure Default Build Task" and then create tasks.json from template. Open the tasks.json file and adapt the following lines:
"label": "The label of your program"
command: your terminal command that works
As an example:
"command": "g++",
"args":[
"-o",
"hello", => The executable name
"hello.cpp",
"bye.cpp" => all the cpp files that needs to build
]
Leave the rest of the file as it is.
Upvotes: 0
Reputation: 216
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.
I had this problem as well and found this Visual Studio Code Doc showing that in the task.json
, one of the command args has to be changed from "${file}"
to "${workspaceFolder}/*.cpp"
in other to build all the .cpp
files.
Upvotes: 4