I'm trying to get started with using VS Code with WSL. I can use g++ to compile just fine in the terminal, but when I try to set this up using tasks it can't find the source files.
For illustration here's the output when I try with a simple hello world C++ program. I've shown a simple build task as well as an ls to show that the file is there. Both of these tasks were run while my focus was on the source code file window.
Executing task: g++ helloworld.cpp <
>g++: fatal error: no input files compilation terminated.
>Terminal will be reused by tasks, press any key to close it.
>Executing task: ls <
>helloworld.cpp
>Terminal will be reused by tasks, press any key to close it.
I guess I must just have some fundamental misunderstanding? Needless to say when I close the terminal created by the task and try to just build from the command line it works.
testvscpp$ ls
helloworld.cpp
testvscpp$ g++ helloworld.cpp
testvscpp$ ls
a.out helloworld.cpp
Any help would be appreciated.
EDIT: The build task was as follows.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "Simple build",
"type": "shell",
"command": "g++",
"args": [
"helloworld.cpp"
]
},
{
"taskName": "Show ls",
"type": "shell",
"command": "ls"
}
]
}
Which is only slightly different from the version found in the documentation - however I also tried this exactly and it didn't work either.
Under "args":, the argument -g is required:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "Simple build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"helloworld.cpp"
]
},
{
"taskName": "Show ls",
"type": "shell",
"command": "ls"
}
]
}
Related
I was trying to create a new OpenGL project with glfw and glad using vs code on an m1 Mac, and setup the files such that I have the include folder and lib folder which contains the necessary headers and library (libglfw3.a) files and the src folder that contains the glad.c and main.cpp, all in my workspace folder.
I have modified the tasks.json file like such:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"-std=c++17",
"-I${workspaceFolder}/include",
"-L${workspaceFolder}/lib",
"${workspaceFolder}/src/*.cpp",
"${workspaceFolder}/src/glad.c",
"-llibglfw3",
"-o",
"${workspaceFolder}/game"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/clang++"
}
]
}
However, I get the following error:
ld: library not found for -llibglfw3
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I thought that I had properly included the path to the glfw library, but it says that it could not find the library. What am I doing wrong?
As a side-note, I have followed this tutorial to setup the program, but this post assumes that you have a windows machine, so I had downloaded/changed some things to my knowledge of what would work with the m1 mac.
Normally when you give libraries to link with the -l flag, you omit the lib prefix. For example: "-lglfw3" links the file "libglfw3.a". It looks like you should change your -llibglfw3 option.
I am trying to run the debugger for a simple "hello world" like program but am running into linking errors.
I only have 3 files, Log.h, Log.cpp, Main.cpp:
Log.h
#pragma once
void InitLog();
void Log(const char *);
Log.cpp
#include "Log.h"
#include <iostream>
void InitLog()
{
Log("Initializing Log");
}
void Log(const char *message)
{
std::cout << message << std::endl;
}
Main.cpp
#include "Log.h"
// #include "Log.cpp"
int main()
{
int var = 9;
char x = 'a';
Log("hello world!");
}
I changed my code-runner settings because I was getting a linker error that indicated the two functions Log() and InitLog() were declared but not defined.
Undefined symbols for architecture x86_64:
"Log(char const*)", referenced from:
_main in main-f30cc3.o
"InitLog()", referenced from:
_main in main-f30cc3.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Switching code-runner settings to compile every file in the directory worked:
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
I mention this because potentially I have wired something incorrectly that I am unaware of (should I have even needed to update code runners functionality?)
While this works for compiling and linking the code, I am unable to debug the code because of the same linking error. My CPP/.vscode/launch.json is as follows:
{
// 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"
}
]
}
I can see that the same linking error occurs, and playing around with the program and args values did not work for me. My error is:
Terminal Error:
> Executing task: C/C++: g++ build active file <
Cannot build and debug because the active file is not a C or C++ source file.
The terminal process failed to launch (exit code: -1).
Terminal will be reused by tasks, press any key to close it.
There is a simple solution to this, I can just #include "Log.cpp" (the part I have commented) and the debugger works!
> Executing task: C/C++: g++ build active file <
Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g CPP/07Debugging/Main.cpp -o CPP/07Debugging/Main
Build finished successfully.
Terminal will be reused by tasks, press any key to close it.
However, I do not want to import the .cpp files into my Main.cpp everytime I need to debug - am I missing something here? Is there a way to compile and link all cpp files in my current directory in a way that I am unaware of? Searches for people running into the same debugger linker errors has proven fruitless, so I assume I am missing something obvious. I am running VSCode on Mac and compiling the c++ with g++
Here is a solution I found for not needing to `#include "Log.cpp"
In CPP/.vscode/ there needs to be a task.json file, and it was in this file that I could alter the g++ command in the same way that I did to update the code-runner.
Old
{
"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"
}
New
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"*.cpp", //this is what changed
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
I guess there is no need to touch the launch.json which I incorrectly assumed I had to initially. I would still like to know if anyone has other approaches because once a project is including headers from different directories I would not be surprised if all of the .cpp files failed to be compiled and linked.
I've been trying to compile and run a simple c++ program with Wxwidget in Linux but when I build it this is what I got when I try to build :
Executing task: g++ -c $(find /home/sopheak/Documents/WXWIDGET/ -type f -iregex '.*\.cpp') -g -D__WXGTK__ -D_FILE_OFFSET_BITS=64 -DWX_PRECOMP -fno-strict-aliasing -pthread -I/usr/local/lib/wx/include/gtk3-unicode-static-3.1/** -Iusr/include/** -I/usr/include/gtk-3.0/** -I/usr/include/at-spi2-atk/2.0/** -I/usr/include/at-spi-2.0/** -I/usr/include/dbus-1.0/** -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include/** -I/usr/include/gio-unix-2.0/** -I/usr/include/cairo/** -I/usr/include/pango-1.0/** -I/usr/include/fribidi/** -I/usr/include/harfbuzz/** -I/usr/include/atk-1.0/** -I/usr/include/pixman-1/** -I/usr/include/uuid/** -I/usr/include/freetype2/** -I/usr/include/libpng16/** -I/usr/include/gdk-pixbuf-2.0/** -I/usr/include/libmount/** -I/usr/include/blkid/** -I/usr/include/glib-2.0/** -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/** -I/usr/include/gtk-3.0/unix-print/** -Wall
zsh:1: no matches found: -I/usr/local/lib/wx/include/gtk3-unicode-static-3.1/**
The terminal process "zsh '-c', 'g++ -c $(find /home/sopheak/Documents/WXWIDGET/ -type f -iregex '.*\.cpp') -g -D__WXGTK__ -D_FILE_OFFSET_BITS=64 -DWX_PRECOMP -fno-strict-aliasing -pthread -I/usr/local/lib/wx/include/gtk3-unicode-static-3.1/** -Iusr/include/** -I/usr/include/gtk-3.0/** -I/usr/include/at-spi2-atk/2.0/** -I/usr/include/at-spi-2.0/** -I/usr/include/dbus-1.0/** -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include/** -I/usr/include/gio-unix-2.0/** -I/usr/include/cairo/** -I/usr/include/pango-1.0/** -I/usr/include/fribidi/** -I/usr/include/harfbuzz/** -I/usr/include/atk-1.0/** -I/usr/include/pixman-1/** -I/usr/include/uuid/** -I/usr/include/freetype2/** -I/usr/include/libpng16/** -I/usr/include/gdk-pixbuf-2.0/** -I/usr/include/libmount/** -I/usr/include/blkid/** -I/usr/include/glib-2.0/** -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/** -I/usr/include/gtk-3.0/unix-print/** -Wall'" failed to launch (exit code: 1).
Here my task.json :
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Compile",
"linux": {
"command": "g++",
"args": [
"-c",
"$(find",
"${workspaceFolder}/",
"-type",
"f",
"-iregex",
"'.*\\.cpp')",
"-g",
"-D__WXGTK__",
"-D_FILE_OFFSET_BITS=64",
"-DWX_PRECOMP",
"-fno-strict-aliasing",
"-pthread",
"-I/usr/local/lib/wx/include/gtk3-unicode-static-3.1/**",
"-Iusr/include/**",
"-I/usr/include/gtk-3.0/**",
"-I/usr/include/at-spi2-atk/2.0/**",
"-I/usr/include/at-spi-2.0/**",
"-I/usr/include/dbus-1.0/**",
"-I/usr/lib/x86_64-linux-gnu/dbus-1.0/include/**",
"-I/usr/include/gio-unix-2.0/**",
"-I/usr/include/cairo/**",
"-I/usr/include/pango-1.0/**",
"-I/usr/include/fribidi/**",
"-I/usr/include/harfbuzz/**",
"-I/usr/include/atk-1.0/**",
"-I/usr/include/pixman-1/**",
"-I/usr/include/uuid/**",
"-I/usr/include/freetype2/**",
"-I/usr/include/libpng16/**",
"-I/usr/include/gdk-pixbuf-2.0/**",
"-I/usr/include/libmount/**",
"-I/usr/include/blkid/**",
"-I/usr/include/glib-2.0/**",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include/**",
"-I/usr/include/gtk-3.0/unix-print/**",
"-Wall"
]
},
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "MoveObjects",
"linux": {
"command": "mv",
"args": [
"${workspaceFolder}/*.o",
"${workspaceFolder}/"
]
},
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [],
"dependsOn": [
"Compile"
]
}
]
}
I'm using Kali Linux and I've been trying looking for ways to build run wxwidgets library for weeks but I still couldn't find any good answer.
Thanks you for helping in advance !
I think when using wxWidgets in VS Code on Linux, the easiest thing is to use CMake. To get started, you'll need both the CMake and CMake Tools extensions:
To get started, open your project in VS Code:
Then open the command pallet and select "CMake:Quick Start":
Then enter a name for the project that will be used in the CMake files and select executable for the project type. I used "cmakewx" for the project. After you select executable, a "CMakeLists.txt" file and a build folder will be created.
Open the CMakeLists.txt file. In the middle of the file there should be a line looking something like:
add_executable(cmakewx main.cpp)
In my case "cmakewx" is the name I entered above for the project name. In your case, it will be the name you entered instead. Change this to
find_package(wxWidgets REQUIRED COMPONENTS net core base)
include(${wxWidgets_USE_FILE})
add_executable(cmakewx main.cpp)
target_link_libraries(cmakewx ${wxWidgets_LIBRARIES})
But replace "cmakewx" with the name you chose for the project.
Finally, open the command pallet again and select "CMake:Configure"
Now the project is ready to go. You can use buttons on the status bar for various project tasks. This area: can be used to change the project's configuration (debug/release/etc). This area will build the project, and these buttons will debug and run the project.
This may seem like a lot of steps, but when your familiar with the process it only takes about 30 seconds to get a project ready to go.
Intellisense should start working after you configure the project. But I have seen that sometimes it doesn't. I'm not sure why. If this happens, closing and reopening the project should get intellisense working.
I want to use some C++17 features that Mac's clang doesn't currently support, so I use
brew install gcc --HEAD
to install the g++ 10.0.1 version. Codes run well in terminal by directly calling
g++-HEAD -std=c++17 test.cpp
I also created a link ln -s g++-HEAD g++ in bash, and added an alias alias g++='g++ -std=c++17' in .bash_profile, so that
g++ test.cpp
will do the same job.
I want to run C++ code in Visual Studio Code - Mac version. After installing its C/C++ Extension by Microsoft, and Code Runner Extension, I set up the settings.json file in VSCode to include compiler argument:
{
"C_Cpp.default.cppStandard": "c++17",
"C_Cpp.default.compilerPath": "/usr/bin/g++",
"C_Cpp.default.intelliSenseMode": "gcc-x64",
"C_Cpp.default.compilerArgs": [
"-std=c++17"
]
}
Then I tried to run the same code. However, I got warning:
[Running] cd "/some directory/" && g++ test.cpp -o test && "/some directory/"test
warning: fold-expressions only available with '-std=c++17' or '-std=gnu++17'
Clearly, it means the g++ compiler ran in VSCode is not the one I manually set with alias. More interestingly, if I run directly in VSCode TERMINAL, my previous code
g++ test.cpp -o test
works.
I'm confused by the setup in VSCode: why doesn't the runner use the same g++ compiler argument as used in VSCode's own terminal? Also, how should I modify the settings.json file or some other files in VSCode so that I can correctly add the -std=c++17 argument?
Assuming you use the C/C++ extension, create a task.json file, which will allow you to change settings such as the path to the compiler, include paths, C++ standard, optimization (default is C++17), and more.
From the code tutorial:
From the main menu, choose Terminal > Configure Default Build Task. A dropdown appears showing various predefined build tasks for C++ compilers. Choose C/C++: g++ build active file.
This will create a tasks.json file in a .vscode folder and open it in the editor.
Your new tasks.json file should look similar to the JSON below
My file looks like
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-std=c++17",
"-ggdb",
"-Og",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
Here is my solution to this predicament. I link it with my projects Makefile.
First, you'll need to build a Makefile, if your using GCC using a Makefile on its own is... controversial to say the least. But I think it serves the purposes of this example. As you may know, running "make" instead of g++ in the current directory will parse the Makefile and run the corresponding commands. But in your case, your Makefile may look similar to:
#You're gonna wanna make it think that your executable doesnt exist, otherwise,
#because the executable exists, make will assume its the most recent build.
.PHONY: debug
#using g++ and the flags inline like this, is generally seen as bad practice, it might be worth
#looking into using Makefiles to make this more acceptable, but this will get you started.
debug:
g++ -g -o debug main.cpp -std=c++17
clean:
rm debug
#if you copy this exactly, make you you replace the spaces with proper tab
#characters otherwise it will error out.
The Juicy part is in VS code; It has a very power feature known as tasks. Tasks are their very own special rabbit hole, but to put it bluntly, you add a task to the "tasks" array in a generated tasks.json file in your workspace, if you're not familar with how that might look, here is the syntax for a task:
{
"label": "[name of task]",
"type": "[type of task, usually its a shell command]",
"command": "[the actual command to call]"
}
There are many more features that tasks can offer, but for making a build tool this will be all you need, for me, this resulted in a file that looked like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build_debug",
"type": "shell",
"command": "make"
},
{
"label": "clean",
"type": "shell",
"command": "make clean"
},
{
"label": "build",
"dependsOn": [
"clean",
"build_debug"
],
"problemMatcher": [
"$gcc"
]
}
]
}
Why do we need to have a final build call? because your launch.json object can take a "preLaunchTask" that will automatically call prior to debug. You can slap in that final build call, and it will compile, debugger, and then run your app. it will even integrate GDB breakpoints and memory tracking into the workspace. My launch.json looks like this:
{
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/runnable",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
]
}
]
}
Sorry for the long replay, i hope it helps :)
I have installed Visual Studio Code on Ubuntu, but I can't seem to get it to compile. I have looked at https://code.visualstudio.com/docs/languages/cpp and https://code.visualstudio.com/Docs/editor/tasks and have tried all the examples but I keep getting the error "No build task defined. Mark a task with 'isBuildCommand' in the tasks.json file." The command I would like to run is g++ Main.cpp Classes.cpp -o Planets -lGL -lglut
I once encountered this problem. What I learned from that are these:
Make sure your Main.cpp, Classes.cpp are at the $workspaceRoot or use relative path.
Make sure your tasks.json file does not contain any error.
Make sure the tasks.json has a version number at the top of the code.
Make sure the type of the task is correct. (e.g. For a console app, it should be "shell")
I have created a tasks.json file for you. It works with my Main.cpp(put a Hello world program in it) and Classes.cpp(A simple class). Compare yours to mine to see if you did something wrong.
{
"version": "2.0.0",
"tasks": [
{
"taskName": "Build",
"type": "shell",
"command": "g++",
"args": [
"Main.cpp",
"Classes.cpp",
"-o",
"Planets",
"-lGL",
"-lglut"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
}
]
}