- Notifications
You must be signed in to change notification settings - Fork182
Compile and execute C "scripts" in one go!
License
ryanmjacobs/c
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
"There isn't much that's special about C. That's one of the reasons why it'sfast."
I love C for its raw speed (although it does have its drawbacks). We shouldall write more C.
With this shell script, you can compile and execute C "scripts" in one go!
(Oh yeah, and it works for C++ too.)
Here's a simple example:
#include<stdio.h>intmain(void) {printf("Hello World!\n");return0;}
Run it by typing:
$ c hello.cHello World!
Or, call it from the shebang!
#!/usr/bin/c#include<stdio.h>intmain(void) {printf("Hello World!\n");return0;}
$ chmod +x hello.c$ ./hello.cHello World!
Use a package manager?Check here.
For the entire system:
$ wget https://raw.githubusercontent.com/ryanmjacobs/c/master/c$ sudo install -m 755 c /usr/bin/c# Or... for systems that prefer /usr/local/bin (e.g. macOS)$ sudo install -m 755 c /usr/local/bin/c
For your local user only:
$ wget https://raw.githubusercontent.com/ryanmjacobs/c/master/c$ sudo install -Dm 755 c~/.bin/c$echo'PATH=$PATH:$HOME/.bin'>>~/.bashrc
Note: if you install it somewhere other than/usr/bin/c
, then your shebangwill be different. For example it may be something more similar to#!/home/ryan/.bin/c
.
c will use whatever$CC
is set to. You can change this with:
$export CC=clang$export CC=tcc$# etc...
Anything you want passed to the compiler, put in quotes as the first argument.Whether they're flags (-Wall
,-O2
, etc.) or file names (file.c
,main.c
, etc.).
$ c"main.c other.c" arg1 arg2$ c"main.c other.c -O3 -Wall -lncurses" arg1 arg2
With only one file, omit the quotes:
$ c hello.c$ c main.c arg1 arg2
After adding a shebang, just set the file to executable and it's ready to run.
$ chmod +x file.c$ ./file.c
Add this to the top of your C file:
#!/usr/bin/c
Just tack on any extra flags, options, or files you want passed to the compiler.Then be sure to add the terminating--
characters.
#!/usr/bin/cfile1.cfile2.c-lncurses-lm--
The default cache size is set to 5 MB. You can change this with:
$export C_CACHE_SIZE=$((10*1024))# 10 MB
The default cache path is set to$TMPDIR/c.cache
. You can change this with:
$export C_CACHE_PATH="/tmp/the_cache"
You can clear the cache with the--clear-cache
flag:
$ c --clear-cache
Feel free to submit any ideas, questions, or problems by reporting an issue.Or, if you're feeling a bit brave, submit a pull request. 😬
Just hack away and make sure that all the tests pass.
$cd tests$ ./test.sh
First of all, I want to clarify why this isnot the same astcc -run
.TCC is a compiler. We all know that. TCC will perform its own set ofoptimizations, just as GCC will perform its own and Clang will perform itsown. The purpose of this script is to give a simple front-end to your favoritecompiler.
Whether it's GCC, Clang, or something else entirely,you get to chooseyour compiler.
Second reason: it's simply satisfying to typec hello.c
and see it run instantly.
Third reason: I'm a fan of speed, and C definitely offers it. Being able towrite a small, fast, and portable C "script" is great. You can pass around aC "script" just like you would a BASH script.
If you're usingzsh
, then you can take advantage ofzsh
's suffix aliases:
$alias -s c='c'$alias -s cc='c'$alias -s cpp='c'
Then you can run files with./file.c
withoutchmod +x
.
Use a package manager? You've come to the right place.
AUR:https://aur.archlinux.org/packages/c/
bpkg:bpkg install ryanmjacobs/c
brew:brew install c
(shebang will be#!/usr/local/bin/c
for Intel-based Macs or#!/opt/homebrew/bin/c
for Apple Silicon)
Maybe later we can implement caching. Done!
Basically, you can do whatever you want provided that you includethe LICENSE notice in any copy of the source. Also, I am not liableif the script breaks anything.
About
Compile and execute C "scripts" in one go!