ryanmjacobs/c

Related work

slowkow opened this issue · 14 comments

Hi @ryanmjacobs,

I just want to mention similar work. You might consider adding a link to your readme or taking inspiration from the ~/.c99shrc config file.

Thanks @slowkow, I'll look into the use of a config file. However, I really like how simple it is now. It's great to just be able to pass in compiler flags using the shebang.

agree, no config files. just sheebang options!

Add to your .bashrc

function c {
    if ! [ "$1" ]; then
        echo "'$1' is not a valid name"
        return 1
    fi

    name=`echo "$1" | awk -F . ' { print $1 } '`

    blue='\033[0;34m'
    green='\033[0;32m'
    NC='\033[0m'

    echo -e "${blue}COMPILING${NC}"
    make "$name"

    if [ $? -ne 0 ]; then
        return 1
    fi

    echo -e "\n\n${green}RUNNING${NC}"
    { ./$name ; }
}

Add rule to Makefile

hello:
    gcc -Wall -pedantic -std=c99 -g hello.c -o hello -lX11 -lXt

Then you can do

bash$ ls
hello.c Makefile
bash$ c hello.c
COMPILING
gcc -Wall -pedantic -std=c99 -g hello.c -o hello


RUNNING
hello world!

https://github.com/burrows-labs/vimrc/blob/master/.bashrc

@burrows-labs Cool, but the purpose of c is to run files with ease. No messing with makefiles, etc. All you need to do is type c file.c (or add a shebang to your file).

I'd prefer to handle the complexity of a project with multiple files and whatever compiler/linker options in a Makefile. This let's me do that and still have the c nicety of compiling/running in one step.

Cool, but the purpose of c is to run files with ease.

Similarly, this eases the compile/execute process for projects with a Makefile.

@ryanmjacobs mentioned that c is meant to be used with small files/projects. If your project needs a makefile, you probably shouldn't be using c - too much stuff could break. If you just want to pass in some cflags, I think you already can do that via the shebang options (correct me if I'm wrong).

Also - what would you do if your makefile had multiple targets? Makefiles aren't meant to be processed by a tool like c - they're meant to be used to build relatively large and complex projects. These projects may include source files from other languages, snippets of bash and what not. To feed that all into c - well, you might as well rewrite make itself. @ellzey also addressed some of these issues in his comment which has now seemed to disappear.

I was imagining scenarios where I have a project layout such as this. The Makefile has a bunch of executable targets, but everything is still pretty simple.

I want to do this

bash$ c exe_target
<compile>
<run>

Instead of

bash$ make exe_target
compile
bash$ ./obj/exe_target
run

This implementation accomplishes those goals. Requires ruby because my bashfu wasn't up to the challenge.

You have just described a normal build system with a wrapper script.

I'd say that this project is done. No new features unless it's something really awesome and keeps things simple as it is now. It's a great way to prototype and should stay that way.

Alternatives have been shown, put them in the readme, and close this issue.

You have just described a normal build system with a wrapper script.

It's like your asking me to argue with you about the semantics of "build systems".

With this shell script, you can compile and execute C "scripts" in one go!
That's from the ryan-c README.

Could I also compile and execute C code from slightly more complicated projects in one go using a simple script? What's the best way to do this?

Alternatives have been shown, put them in the readme, and close this issue.

close/open/whatever, this thread seems like a reasonable place to talk about "scripts that compile and execute C code in one go".

I agree with @alebcay, if you're using a Makefile, then you shouldn't be using c anyways. c is just for small "scripts".

I also agree with @ellzey. This project is pretty much done. Unless something simple and awesome comes along, there isn't really much point in adding it.

@burrows-labs If you want to compile slightly more complex projects, then just use multiple files and link in the libraries you want. Ex:

$ c "one_file.c another_file.c -lncurses -lm one_more_file.c" arg1 arg2

or do the same thing in the shebang:

#!/usr/bin/c one_file.c another_file.c -lncurses -lm one_more_file.c --

then make it executable and run it.

Could I also compile and execute C code from slightly more complicated projects in one go using a simple script? What's the best way to do this?

However you choose to do this, with or without c, expect dismal results - complicated projects use a build system for a reason. We must also remember that the traditional Makefile often performs other processes on the source files in preparation for building. To handle that would require implementing an m4 parser into c, among other things, which simply isn't viable as a shell script - that's why make isn't written in shell script.

complicated projects use a build system for a reason.

Exactly.

close, create a wiki page, allow users to put in their own build flows using this project; link to other similar work, and be done with it.

@ellzey Okay, I created a wiki page. I think anyone can edit it now.