I'm using Sublime Text 3 for C programming. I want to compile and run my programs in the gnome terminal using a keybinding or something like that instead of the ST3 console (like CodeBlocks does). How can I do it?
Click Tools->Build System->New build system to add a new build system, copy following code:
{
"cmd" : ["gnome-terminal -x bash -c \"gcc $file_name -o ${file_base_name} -lm -Wall; ./${file_base_name}; exec bash\""],
"selector" : "source.c",
"shell":true,
"working_dir" : "$file_path"
}
Save this file.
Then click Tools->Build System to choose the newly stored build system. Every time you press "Ctrl+B", the keybinding of Tools->build, it will run gnome-terminal and compile&run your program automatically.
Related
This question already has answers here:
run a program with more than one source files in GNU c++ compiler
(3 answers)
Closed 4 years ago.
I want to compile multiple files together located in the same folder
(main.c function.c function.h)
where function.h is included in main.c like #include "function.h"
-in codeblocks it works fine
-in sublime text 3 there is a problem that it compiles files individually and pops errors.
can anyone help !
Undefined reference is a linker error, not a compiler error. You may have compiled all the necessary modules, but you then need to execute the linker to link those modules and any library code and run-time start-up. It looks you are compiling and linking main.o on its own.
The Code::Blocks IDE will have performed complete the link by virtue of all your modules being part of your project because it is an IDE - it integrates an editor, compiler, linker and debugger. Sublime Text is only a text editor, you have to tell it exactly what command is required to build your code. For a multi-module build, you may want to consider using a makefile or similar.
I am already using a build system for compiling c codes but it seems that it works for individual files not multiple files at same time >> do you know a build system for compiling multiple files at the same time as in functions and header files??
{
"cmd": ["gcc", "-Wall", "-ansi", "-std=c11" , "-pedantic-errors", "$file_name", "-o", "${file_base_name}.exe", "&&", "start", "cmd", "/k" , "$file_base_name"],
"selector": "source.c",
"working_dir": "${file_path}",
"shell": true
}
When building a working ".c" code file I experience an error building with the menu icons to build(build system GCC)
When I navigate: tools -> build (with GCC selected as build system)
'main.exe' is not recognized as an internal or external command,
operable program or batch file.
[Finished in 0.1s with exit code 1]
[cmd: ['main.exe']]
[dir: C:\Users\erik\Documents\Carleton University\Sysc 2006\Lab 11\Recursion\Recursion]
[path: C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin;C:\Perl64\site\bin;C:\Perl64\bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\MinGW\bin;C:\MinGW\mingw32\bin;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\PharosSystems\Core;C:\Python35-32;C:\Python35-32\Lib\site-packages\;C:\Python35-32\Scripts\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Users\erik\AppData\Local\bin\NASM;C:\Program Files\Git\cmd;C:\Users\erik\AppData\Local\Microsoft\WindowsApps;C:\altera\13.0sp1\modelsim_ase\win32aloem;C:\Users\erik\AppData\Local\GitHubDesktop\bin;]
But if i navigate: tools -> command palette -> build with:GCC
[Finished in 0.5s]
Following this beloved visual above I would then navigate: tools -> build (with GCC selected as build system), OR tools -> command palette -> build with:GCC -RUN
***Expected working output***
This is my GCC.sublime-build file, I suspect this is the file that is causing issues.
// Put this file here:
// "C:\Users\[User Name]\AppData\Roaming\Sublime Text 3\Packages\User"
// Use "Ctrl+B" to Build and "Crtl+Shift+B" to Run the project.
// OR use "Tools -> Build System -> New Build System..." and put the code there.
{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}.exe"],
// Doesn't work, sublime text 3, Windows 8.1
// "cmd" : ["gcc $file_name -o ${file_base_name}"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path",
// You could add path to your gcc compiler this and don't add path to your "PATH environment variable"
// "path" : "C:\\MinGW\\bin"
"variants" : [
{ "name": "Run",
"cmd" : ["${file_base_name}.exe"]
}
]
}
Although the issue may be here, I am very concerned it may also be elsewhere as I have been struggling with implementing libraries.
A second side question would be why is my "Path:" variable so long, is it unnecessarily long? many of those would not be needed in sublime correct?
This will allow you to compile and run c++ programs in sublime as well as command prompt also
{
"shell_cmd": "g++ -Wall -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}.exe\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[ // to run in the sublime text console
{
"name": "Run",
"shell_cmd":"\"${file_path}/${file_base_name}\""
},
// to run in the Windows command line
{
"name": "Run in cmd",
"shell_cmd": "start cmd /k $file_base_name "
}
]}
I'm working with a Mac and I want to compile and run my C files from the Terminal using a script. I've found a solution that apparently works in Linux. Here is the article in Spanish (http://ayudasprogramacionweb.blogspot.com.es/2013/01/ejecutar-en-terminal-linux-sublime-text.html). It consists in two simple steps:
You create a script in linux to compile and execute the source code in the Terminal and you store it in your Documents folder named as runC.sh.
#!/bin/bash
gnome-terminal -e "/bin/bash -c 'gcc -Wall $1 -o $2; ./$2; echo; read -p 'Press_enter_to_scape...'; exit; exec /bin/bash'; &"
Through a Sublime Text Build System, you call this script by passing as parameters the name of the source file.
{
"cmd": ["~/Documents/runC.sh ${file_name} ${file_base_name}"],
"shell": true
}
I've tried it in my Mac but as I expected it doesn't work... Could you help me to adapt it for Mac OS please? I've just started programming and I don't know where to start to fix it... thank you very much!
After days of searching I've finally found a solution! In Mac there is no need of a script, all that you need is the proper Build System.
This one for C files:
"cmd": ["bash", "-c", "gcc '${file}' -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]
And this one for C++ files:
"cmd": ["bash", "-cpp", "g++ '${file}' -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]
Now, when I press Cmd+B, a new Terminal window opens and my file runs, and of course it accepts input from the user, which sublime text console doesn't...
Simple and easy! All the credits to this guy: https://stackoverflow.com/a/18562836/4359229
I am trying to run c programs in sublime 2, windows machine, I have installed mingw, updated the path variable, and copied the following code in the sublime new build system
{
"cmd": ["gcc -o $file_base_name $file && ./$file_base_name"],
"path": "C:/Program Files (x86)/CodeBlocks/MinGW/bin",
"shell": true
}
Then i wrote a simple C program
#include<stdio.h>
int main()
{
printf("ihdfoihsdifhoisdhf");
return 0;
}
On pressing CTRL+SHIFT+b and CTRL+b , i am getting the following error
'.' is not recognized as an internal or external command,
operable program or batch file.
[Finished in 0.4s with exit code 1]
The problem is most likely here:
"cmd": ["gcc -o $file_base_name $file && ./$file_base_name"],
The dot-slash is current-directory notation for *nix systems, but you're on a Windows system, so try changing that to
"cmd": ["gcc -o $file_base_name $file && $file_base_name"],
Likewise, you will want to change the forward-slashes / in your path to backslashes \.
GCC only sometimes creates output files, and I have no idea why. I have been compiling my files in Sublime Text with this build system:
{
"cmd": ["gcc", "$file_name", "-o", "${file_base_name}", "-Wall"],
"selector" : "source.c",
"shell" : false,
"working_dir" : "$file_path"
}
GCC runs and gives me any output necessary for debugging, but once I fix the code and there are no errors, no output file is created. I have also tried running gcc input.c -o output.exe in the command line. This was all working just a few hours ago, but has seemingly randomly stopped without me changing anything.