Setup vscode to compile C files - c++

Im trying to setup vscode to compile and run C files, but I am having problems with setting up the tasks.json file.
I guess what I'm really asking is how to include code from outside the main file. Im trying to include a file "stack.h" from a folder "include" but it's not working.
I get this error in vscode:
ld: can't link with a main executable file 'gcc' for architecture
x86_64 clang: error: linker command failed with exit code 1 (use -v to
see invocation) The terminal process terminated with exit code: 1
My tasks.json file looks like this
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"gcc",
"-o",
"stack_test",
"-I${fileDirname}/../include/",
"${file}",
"${fileDirname}/../src/stack/stack.c",
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
I can compile this file normally through the terminal with the following
gcc -o stack_test -I../include/ stack_test.c ../src/stack/stack.c
What am I missing here?

The problem could be that the compiler is not in the platform path. Visual Studio Code is therefore not able to find it.
See: https://code.visualstudio.com/docs/languages/cpp

Related

C++ and CPLEX and Linux with Visual Studio Code: "No such file or directory" error after adding paths to includePath

I have been trying to run some of the CPLEX C++ examples in Linux without success. Here the steps I followed:
1 - I installed CPLEX studio for Linux (cplex_studio2210.linux_x86_64.bin)
2 - I also installed Visual Studio Code (code_1.74.2-1671533413_amd64.deb) + "C/C++" Extension
Then I tried to run some of the examples in the CPLEX installation path (<my_cplex_path>/CPLEX_Studio221/cplex/examples/src/cpp). So I copied the code of one of the files to a new c++ project in Visual Studio Code. This one to be exact. It contains the following "include" line (All example files contain similar "include" lines so any other one causes the same issue too):
#include <ilcplex/ilocplex.h>
So I addded my cplex paths to includePath in my c_cpp_properties.json like this:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/home/my_user/ILOG/CPLEX_Studio221/cplex/include/**",
"/home/my_user/ILOG/CPLEX_Studio221/concert/include/**"
],
.............
}
],
"version": 4
}
Starting build... /usr/bin/g++ -fdiagnostics-color=always -g
/my_path/example.cpp -o /my_path/example /my_path/example.cpp:1:10:
fatal error: ilcplex/ilocplex.h: No such file or directory
1 | #include <ilcplex/ilocplex.h>
| ^~~~~~~~~~~~~~~~~~~~ compilation terminated.
Build finished with error(s).
This is something I did not expect because IntelliSense correctly recognizes the .h files:
What configuration am I missing?
The Makefile in the examples folder under the cplex installation directory gave me a hint about how to solve it: I finally solved adding a few parameters in the args section in my task.json.
I modified this original task.json:
{
"type": "cppbuild",
"label": "C/C++: cpp build active file",
"command": "/usr/bin/cpp",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
....
to this:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-I/my_installation_directory/ILOG/CPLEX_Studio221/concert/include",
"-I/my_installation_directory/ILOG/CPLEX_Studio221/cplex/include",
"-L/my_installation_directory/ILOG/CPLEX_Studio221/cplex/lib/x86-64_linux/static_pic",
"-L/my_installation_directory/ILOG/CPLEX_Studio221/concert/lib/x86-64_linux/static_pic",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lconcert",
"-lilocplex",
"-lcplex",
"-lm",
"-lpthread",
"-ldl"
],
.....
what makes Visual Studio Code to execute a command similar to the following when I click the run button:
g++ -m64 -fPIC -fno-strict-aliasing -fexceptions -DNDEBUG -I/my_installation_directory/ILOG/CPLEX_Studio221/concert/include -I/my_installation_directory/ILOG/CPLEX_Studio221/cplex/include -L/my_installation_directory/ILOG/CPLEX_Studio221/cplex/lib/x86-64_linux/static_pic
-L/my_installation_directory/ILOG/CPLEX_Studio221/concert/lib/x86-64_linux/static_pic
-g /my_installation_directory/examples/cutstock.cpp -o /my_installation_directory/examples -lconcert -lilocplex -lcplex -lm
-lpthread -ldl

Visual Studio Code C++ Build finished with error(s)

I'm trying to run a simple hello world application. Whenever I try to build using g++ it gives me the following error. I can't generate the .exe file or anything. I re-installed mingw using msys2. Walked through tutorials etc.. same problem occurs. The paths provided below are supposed to be correct made sure of it
* Executing task: C/C++: g++.exe build active file
Starting build...
C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g3 -Wall H:\VSCode\CPP\helloworld.cpp -o H:\VSCode\CPP\helloworld.exe
The system cannot find the path specified.
Build finished with error(s).
* The terminal process failed to launch (exit code: -1).
* Terminal will be reused by tasks, press any key to close it.
I walked through tutorial to build and debug c++ from the beginning to the end still the same problem. tasks.json code is below. I sticked with default "args" but same problem as well. I copied the tutorials args same problem.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g3",
"-Wall",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
// "${fileDirname}" also gives the same problem.
"cwd": "C:\\msys64\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
},
{
"type": "shell",
"label": "Run C/C++: g++.exe",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe -g3 -Wall \"${file}\" -o \"${fileDirname}\\${fileBasenameNoExtension}.exe\"",
"options": {
"cwd": "C:\\msys64\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
}
]
}
edit:
helloworld.cpp
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World!"<<endl;
return 0;
}
When I run it using Run Code using CodeRunner extension it runs normally, but Run/Debug C++ Code gives the error provided.
Whenever I try to see the errors, it shows nothing.
Another Edit:
when running C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g3 -Wall H:\VSCode\CPP\helloworld.cpp -o H:\VSCode\CPP\helloworld.exe in cmd it build successfully and generate the .exe file.

VS Code G++ intellisense settings not working and compiler not running to completion

So I'm trying to use vs code as my ide on linux for the GNU c/c++ toolset.
Thus far not with a great amount of success.
The first part is intellisense.
I am using the Microsoft c/c++ extension for this.
I followed what common sense told me and then I also followed what the extenstion told me to do which ended up being the same thing.
See below. Even after reloading the window intellisense is not working.
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/Data/Documents/CensusProject/gdallib/src/compiled/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
The next part is building.
To build from the commandline I have to do the following:
PKG_CONFIG_PATH=/Data/Documents/CensusProject/gdallib/src/compiled/lib/pkgconfig
LIBRARY_PATH=/Data/Documents/CensusProject/gdallib/src/compiled/lib
LD_LIBRARY_PATH=/Data/Documents/CensusProject/gdallib/src/compiled/lib/
C_INCLUDE_PATH=/Data/Documents/CensusProject/gdallib/src/compiled/include/
CPLUS_INCLUDE_PATH=/Data/Documents/CensusProject/gdallib/src/compiled/include/
export PKG_CONFIG_PATH
export LIBRARY_PATH
export LD_LIBRARY_PATH
export C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH
So I placed these in tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}",
"env":{
"PKG_CONFIG_PATH":"/Data/Documents/CensusProject/gdallib/src/compiled/lib/pkgconfig",
"LIBRARY_PATH":"/Data/Documents/CensusProject/gdallib/src/compiled/lib",
"LD_LIBRARY_PATH":"/Data/Documents/CensusProject/gdallib/src/compiled/lib/",
"C_INCLUDE_PATH":"/Data/Documents/CensusProject/gdallib/src/compiled/include/",
"CPLUS_INCLUDE_PATH":"/Data/Documents/CensusProject/gdallib/src/compiled/include/"
}
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/g++"
}
]
}
Running the task lead to
Starting build...
/usr/bin/g++ -g /Data/Documents/CensusProject/gdallib/src/compiled/GdalTest/test.cpp -o /Data/Documents/CensusProject/gdallib/src/compiled/GdalTest/test
collect2: fatal error: cannot find 'ld'
compilation terminated.
Build finished with error(s).
The terminal process terminated with exit code: -1.
which is a bit of an improvement, as before the compiler errored about not finding the header files etc... its sort of an improvement.
in the shell this works fine.
Is anyone else familiar with these problems ?
Thanks in advance.
In your case, the directory where the "ld" program was located had not been defined in the PATH environment variable. Firstly you should add it.
Example:-
export
PATH=$PATH:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin

Why does debugging c++ using gdb in vscode create a an executable called launch.exe despite me specifying a name?

When debugging a cpp file in Visual studio code the launch.json seems to be making a launch.exe even though I specified in the tasks that the output file should be called "main.exe" and on top of that there's a build error yet the executable made runs correctly.
All files are in the same directory and the only folder in this directory is the automatically generated .vscode folder
My task.json :
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\main.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {"kind" : "build","isDefault": true},
"detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe\""
}
]
}
My 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++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
And then the actual cpp files :
main.cpp
#include "Log.h"
int main()
{
Log("Hello World");
}
Log.cpp
#include "Log.h"
#include <iostream>
void Log(const char* message)
{
std::cout << message << "!\n";
}
And a header file
Log.h
#pragma once
void Log(const char* message);
What I expect to happen is that the tasks.json gets all the cpp files compiled and linked then creates an executable called main.exe.
This does happen when I just run the build task through Terminal > Run Build Task... but when I try debugging by pressing F5 when on my main.cpp file, there is a build error and, in my directory, a launch.exe executable is created. I am then prompted that there were build errors and after clicking "debug anyway", a prompt appears showing that "main.exe" does not exist.
The build error :
C:\Users\Me\AppData\Local\Temp\ccUAuPNe.o: In function `main':
c:/Users/Me/Projects/test/main.cpp:7: undefined reference to `Log(char const*)'
collect2.exe: error: ld returned 1 exit status
The terminal process failed to launch (exit code: -1).
My platform is windows and
I believe I have all the necessary extensions installed correctly so I don't really understand what causes this issue.
It does debug correctly when I change the "program" value in launch.json to "${fileDirname}\launch.exe" (although there is still the build error) but why is launch.exe being made and why are there build errors when debugging but no build errors when going through Terminal > Run Build Task... ?

Visual studio code, compiling and running in single command

I have specified a pre-launch task to compile c++ code before launching in the launch.json file.
The build command is outlined in my tasks.json file:
"version": "0.1.0",
"command": "bash",
"isShellCommand": true,
"args": ["-c"],
"showOutput": "always",
"tasks": [
{
"taskName": "g++",
"isBuildCommand": true,
"args": [
"g++ /Users/user/OneDrive/Programming/TicTacToe/TicTacToe.cpp -o /Users/user/OneDrive/Programming/TicTacToe/a.out"
],
"showOutput": "always"
}
]
The output of this when I run this is that I receive a clang error:
clang: error: no input files
I'm not sure why this is happening as when I copy paste this bash command to terminal it works, but for some reason I am getting clang error from visual studio. Can anyone familiar with clang figure out what's wrong here?
The problem was that the string passed included the taskname for some reason.
It worked after I addded
"suppressTaskName": true
to tasks.json