How to setup sublime text 3 to run on C++11, but it opens the cmd each run? - c++11

How do I run C++11 in Sublime Text 3?
I found this and this works, but I want it to be able to open cmd each run.
(1)
{
"shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
}
]
}
Like this one:
(2)
{
"cmd":
[
"g++", "-Wall", "-ansi", "-pedantic-errors", "$file_name", "-o",
"${file_base_name}.exe", "&&", "start",
"cmd", "/k" , "$file_base_name"
],
"selector": "source.cpp",
"working_dir": "${file_path}",
"shell": true
}
The problem with (1) is it runs with the console inside Sublime Text 3, I don't want that, unfortunately.
The problem with (2) is it runs well and it opens CMD each time, which is what I need, but it's on C++98. When I need C++11.
So, is there a way to modify any one of these build systems so that I can run C++11 and make it open CMD every run (instead of running it on the console)? Thanks.

I was able to figure it out with a little bit of tinkering involved.
{
"shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}\" && start cmd /k \"${file_path}/${file_base_name}\""
}
]
}

Related

How to fix ignored breakpoints when debugging C in VScode (gdb)?

I am trying to debug my C in vscode using breakpoints, but the debugger seems to skip them everytime i run it (the break points change colours from red to grey). I looked at this question which is essentially the same question i have. I tried all the answers there and none worked (none were set as 'answers' by the person who asked, hence why i am asking this question again). So my question is, how to get vscode breakpoints working in C?
Vscode version: 1.73.1 on windows 10
gdb version: 12.1
launch.json
{
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file",
}
]
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "make",
"args": [
"all"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
makefile
dynamic_array: dynamic_array.c dynamic_array.h
gcc -c dynamic_array.c
test: test.c dynamic_array.h
gcc -c test.c
all: dynamic_array.o test.o
gcc -o test.exe dynamic_array.o test.o
clean:
del -f *.o & del -f *.exe & del -f *.out
Your makefile has a number of problems. First, the target you build is all, which depends on dynamic_array.o and test.o, however, there is no rule for building these, only a rule for building dynamic_array and test.
As such, make will use its default rule for building a .o file, which is:
$(CC) $(CPPFLAGS) $(CFLAGS) -c
This leads to the next problem, which is probably the cause of the breakpoint problems you are seeing - neither the implicit rule, nor the rule you tried to write, include the -g flag for gcc. The -g flag turns on production of debug information.
I would at a minimum, rewrite the makefile as:
dynamic_array.o: dynamic_array.c dynamic_array.h
gcc -c -g3 dynamic_array.c
test.o: test.c dynamic_array.h
gcc -c -g3 test.c
all: dynamic_array.o test.o
gcc -g3 -o test.exe dynamic_array.o test.o
clean:
del -f *.o & del -f *.exe & del -f *.out

Linking mac framework in VScode

I'm struggling to build an openGL project in VScode on Mac. The problem is how to add a framework in the tasks.json file of VScode. I can build the project successfully from the command line.
To build successfully I need the gcc command to include the argument -framework OpenGL. So it should read:
gcc *.cc -std=c++17 -stdlib=libc++ -g -framework OpenGL ...(etc.)
but if I add "-framework OpenGL" to the argument list in tasks.json, VScode parses it as:
gcc *.cc -std=c++17 -stdlib=libc++ -g '-framework openGL' ...(etc.)
Obviously gcc doesn't recognise '-framework OpenGL' in the inverted commas.
How do I add a framework in VSCode on a mac???
My tasks.json is:
"type": "shell",
"label": "C/C++: clang build active file",
// "command": "/usr/bin/clang",
"command": "gcc",
"args": [
"${fileDirname}/*.cc",
"-std=c++17",
"-stdlib=libc++",
"-g",
"-framework openGL",
"-L/usr/local/Cellar/glew/2.1.0_1/lib",
"-L/usr/local/Cellar/glfw/3.3.2/lib/",
"-lglfw",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
Try putting them in separate rows like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-v",
"-I/usr/local/include/GLFW",
"-L/usr/local/lib",
"-lglfw3",
"-framework",
"Cocoa",
"-framework",
"OpenGL",
"-framework",
"IOKit"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
So:
"-framework",
"Cocoa",
Even my answer comes 4 years later I hope will help you.
The solution is to write the c_cpp_properties.json file (found or created by yourself in hidden .vscode folder inside your project) such as:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"intelliSenseMode": "macos-gcc-x64",
"configurationProvider": "go2sh.cmake-integration"
}
],
"version": 4
}
As you can see, you have to add there this magic sentence:
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
Of course this is the path where my system keeps all the frameworks. You have to find and to fill your own system path.

VS code gdb exiting immediately when run as vs code debugger

When I debug my application through a terminal and gdb, everything works as expected, however if I try to use gdb as the debugger for vs code, it exists immediately with output:
Breakpoint 1, main (argc=1, argv=0x7fffffffe638) at /home/kronos/Desktop/voxel-world/source/main.cpp:60
60 {
[Inferior 1 (process 7771) exited with code 01]
The program '/home/kronos/Desktop/voxel-world/build/voxel-world' has exited with code 1 (0x00000001).
This is my configuration file:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/build/voxel-world",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
First I would make sure that the program is not exiting with return code 1 by design (e.g., due to sanity checks etc.).
You could start by setting
"stopOnEntry": true
in config file launch.json, then proceed by debugging stepwise through main().
The solution that worked for me was to recompile everything with no optimization -O0 and with debugging symbols included -g. This is what my Makefile looks like:
all: main
main: main.o flash_simulator.o flash_simulator.h
cc -O0 -g -o main main.o flash_simulator.o
%.o : %.c
cc -O0 -g -c $< -o $#
clean:
rm -f *.o

C++ Build File for Sublime Text 3

I found a build system for c++ for st3 and it is pretty good but there is a small kink it does not compile unless a .exe file with the source file name exists in the directory. Any idea on how to automate it with the build to create a file if it does not exist or continue?
"windows":
{
"cmd": ["g++", "$file_name","-o", "${file_base_name}.exe", "-lm", "-Wall", "&","start", "${file_base_name}.exe"]
},
"selector": "source.c++",
"shell": true,
"working_dir": "${file_path}"
}
That should run regardless, as the build will create a .exe file with the source name in the current directory. If for some reason it isn't working, remove everything on line 2 after "-Wall" so that it looks like this:
"windows":
{
"cmd": ["g++", "$file_name","-o", "${file_base_name}.exe", "-lm", "-Wall"]
},
"selector": "source.c++",
"shell": true,
"working_dir": "${file_path}"
}

Path variable for coffeescript build in sublime on windows

So I have this build file
{
"cmd": ["coffee", "-c", "$file"],
"selector": "source.coffee",
"path": "C:\\Users\\Miles\\node_modules\\coffee-script\\bin",
"working_dir": "$project_path"
}
And it keeps on returning the error
[Error 2] The system cannot find the file specified
I have looked for a few days and and all I can find is unix based paths.
I know this is most likely trivial but it has been bugging be for a while
after a lot of tinkering this works fine.
{
"cmd": ["coffee.cmd", "-c", "$file"],
"selector": "source.coffee",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"path": "$HOME/bin:/usr/local/bin:$PATH",
"working_dir": "$project_path"
}

Resources