Compile and run in Mac Terminal Sublime Text files - c

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

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
)

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.

Error while compiling C program in Sublime 2(Windows)

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

C program, calling object file

I have written a simple helloworld.c program. I can compile it and run it on linux terminal using gcc and ./a.out command. My query is regarding calling .o file without any extension. For example, to run my program instead of typing "./helloworld.out", I want to run it using keyword "helloworld" on my terminal. Any hints???
Thank You.
Just compile using
gcc -o helloworld helloworld.c
The -o option is for the output file name
Then use:
./helloworld
You need the ./ to tell the shell where the executable resides, since the current directory is unlikely to be in $PATH.

Shell script file for compiling and producing output of C file

I wrote this .sh file to compile any c source file, so that when I run it, it asks for a filename and gcc compiles it and then, runs the executable a.out.
But this doesn't work properly when error is present in .c files. It also shows that a.out is not present. I don't want this error message ( a.out is not present ) but just want to print only the error message generated for the .c files..
Here's the script..
echo `clear`
echo enter file name
read FILE
gcc $FILE
./a.out
echo -e '\n'
If you enable abort-on-error in shell scripts, life will be a lot easier:
#!/bin/sh
set -eu # makes your program exit on error or unbound variable
# ...your code here...
Utilizing builtin rules as an alternative to your script you might want to use make as an alternative to a handcrafted script. To compile file.c and run the generated executable all you need to do is:
make file && ./file
If you don't know it, I strongly suggest you take a look at the make utility as it will ease your work a lot. Managing anything more than a one file project can get really nasty without it.
You can chain compilation and execution commands:
echo `clear`
echo enter file name
read FILE
gcc $FILE && ./a.out
echo -e '\n'
Here, if gcc will fail, the shell will drop the ./a.out command.
You may also protect the file name with double quotes :
#! /bin/bash
clear
echo -n "Enter file name: "
read FILE
gcc -Wall -W "$FILE" && ./a.out
echo
I there hope this is what you were looking it only requires this command:
./compile executableName myCProgram.c -lm
you can place more C files ahead of each others and add more libraries at the end of the line and executableName does not require the .exe
#!/bin/bash
args="$#"
quant=$#
#Copies in case you need the -lm(math library) params
biblio=${args#*-}
#grabs all params except the first one which should be the name of the executable
firstCommand=${*:2:${#args}}
#Remove the "-lm -lc" from firstCommand
firstCommand=${firstCommand%%-*}
printf "\nEXECUTING: gcc -W -Wall -c $firstCommand\n"
#Creates the object file ".o"
gcc -W -Wall -c $firstCommand
#Convert the files names from example.c to example.o
args=${args//.c/.o}
printf "\nEXECUTING: gcc -o $args\n\n"
#Creates the executable
gcc -o $args
printf "\n**Now execute comand: ./$1 **\n\n"

Resources