c

Running c program from command line in one step?


I just got started writing some C programs.

To start with I was just running them through VS code. Nice and easy, I just had to press a button and bam, there it was.

But now I need to pass files as arguments to my program, which creates the need of running it from the command line.

The way I do it now, is using this two step process, (which I think is just the basic way of doing it):

ask@Garsy:~/Notes/ethHack/crpytifiles$ gcc test.c -o test

and then running the file:

ask@Garsy:~/Notes/ethHack/crpytifiles$ ./test 

This is a bit tedious in the long run. Is there any way I could do this process in one step?

And perhaps also without creating the executable?

It would be really cool if I could just run it as you normally would with a python or java file, one command, and the thing runs.


Solution

  • You could do that with a makefile. More about GNU Make here.

    all:
      gcc test.c -o test
      ./test
    

    The file should be called Makefile or makefile (it can have different names,just keeping it simple), and you can run it by executing:

    make

    Assuming you have GNU Make installed and test.c is located in the same directory with makefile.