Error while compiling C program in Sublime 2(Windows) - c

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 \.

Related

How to put a test.txt in your CMake to test your program?

So I am using CLion on a Mac and wrote my code and would like to test it. I have an input file called test.txt. I know how to do it using terminal which is simply ./a < test.txt and it will run the binary and take text.txt as input. My question is, can we do it through CMake? So that I don't need to use terminal and just press the "run" button in CLion.
Having:
add_executable(a ...)
The short workaround on systems with sh shell would be to just spawn a shell to do the redirection:
add_test(NAME atest COMMAND sh -c "\"$1\" < \"$2\"" -- $<TARGET_FILE:a> test.txt)
A proper way would be to use CMake instead of shell. So a separate CMake script to run the executable with redirected file. Below is an example that just creates the script from inside CMake - but it can be just a separate file instead.
# redirect_stdin_from_file.cmake
execute_process(
COMMAND "${COMMAND}"
INPUT_FILE "${INPUT_FILE}"
RESULT_VARIABLE ret
)
if(ret)
message(FATAL_ERROR "ERROR: ${COMMAND} failed: ${ret}")
endif()
# CMakeLists.txt
add_test(NAME atest2 COMMAND
${CMAKE_COMMAND}
-D COMMAND=$<TARGET_FILE:a>
-D INPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/test.txt
-P ${CMAKE_CURRENT_SOURCE_DIR}/redirect_stdin_from_file.cmake
)

Sublime Text 3, GCC build system issue

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 "
}
]}

Sublime Text 3 compile and run (in terminal) C on OS X with passed arguments

I'm trying to compile a C program in Sublime Text 3 then run it in the terminal (which opens up through Sublime Text) on OS X Yosemite. My build system is:
{
"cmd": ["gcc", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "gcc '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]
}
When I build, for example, test.c, it compiles fine. When I run the program I get this: bash: line 1: 916 Segmentation fault: 11. I'm sure this is because my program needs arguments passed to it.
So I have two questions:
How can I change the build system so that when I run it, it opens up the terminal and runs in there?
How do I pass arguments to the program before it runs? For example, on Linux I would type ./test hello 20932aa and it will run fine. How can I achieve the same on Sublime Text 3 (OS X Yosemite).
This is all you need in your build system to compile and run C code in ST3. Just replace arg1 arg2 arg3 with your arguments and save your build system before you use Tools -> Build on your C program like usual.
The && operator allows you to execute another "shell_cmd" (shell command) after the operator.
{
"shell_cmd": "make ${file_base_name} && ./${file_base_name} arg1 arg2 arg3"
}
Alternatively, here is a build system with all the bells and whistles.
{
"shell_cmd" : "gcc $file_name -o ${file_base_name}",
"working_dir" : "$file_path",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"variants":
[
{
"name": "Run",
"shell_cmd": "gcc $file_name -o ${file_base_name} && ${file_path}/${file_base_name} arg1 arg2 arg3"
}
]
}
If you want your program to open in a new terminal window, use this build system. You won't be able to pass arguments to it though.
{
"shell_cmd": "make ${file_base_name} && open -a Terminal.app ${file_path}/${file_base_name}",
}

Show C build results in Linux terminal with Sublime Text 3

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.

Compile and run in Mac Terminal Sublime Text files

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

Resources