Semjon Mössinger
Semjon Mössinger

Reputation: 1890

How to run (and compile) a C file with arguments with gcc?

I think it's a very simple question but I have less experience with command line. I have an old C program I want to run. It shows the following text:

/*      This is a stand-alone program intended to generate ...  It is called*/
/*      as follows:                                                         */
/*                                                                          */
/*          tw_r2fft N > outputfile.c                                       */ 
/*      This program will generate the ... array 'w'                        */
/*      and output the result to the display.  Redirect the                 */
/*      output to a file as shown above.                                    */

I tried (in cmd):

gcc tw_r2fft.c 1024 > outputfile.c

gcc's error message was:

gcc: 1024: No such file or directory

I tried some variations but without success.

Upvotes: 4

Views: 54575

Answers (7)

Googolbyte
Googolbyte

Reputation: 31

Compile and run a program in one line:

gcc source.c -o sourceBinary; ./sourceBinary

The semi-colon indicates an ended statement/break in bash.

Upvotes: 3

Anibal Anto
Anibal Anto

Reputation: 157

You can try this:

gcc tw_r2fft.c -o tw_r2fft && ./twr2fft 1024 > outputfile.c

or without the -o option:

gcc tw_r2fft.c && ./a.out 1024 > outputfile.c

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753970

That comment is an explanation of how to use the compiled program. You need to build it first.

Simplest:

make tw_r2fft
./tw_r2fft 1024 > outputfile.c

Next simplest:

gcc -o tw_r2fft tw_r2fft.c
./tw_r2fft 1024 > outputfile.c

Upvotes: 2

Saurabh Rana
Saurabh Rana

Reputation: 3540

This line will compile your program

gcc tw_r2fft.c -o tw_r2fft 

gcc is compiler and tw_r2fft.c is your file name. -o is a way to change the output file name. You could compile the program without -o but then by default the compiler will save your output file as ./a.out

This line executes your output file and passes a command line argument i.e 1024 and the output of the whole program is saved to outputfile.c

./tw_r2fft 1024 > outputfile.c

Still need help

http://www.cyberciti.biz/faq/compiling-c-program-and-creating-executable-file/

Upvotes: 2

buc
buc

Reputation: 6358

Try this to compile the C program to an executable binary:

gcc -o tw_r2fft tw_r2fft.c

Then start the binary with the proper command-line arguments:

./tw_r2fft 1024 >outputfile.c

Then you can compile and run your output file as well: :)

gcc -o test outputfile.c
./test

Upvotes: 5

Kevin
Kevin

Reputation: 56089

You need to compile the program before you run it

gcc tw_r2fft.c -o tw_r2fft
./tw_r2fft 1024 > outputfile.c

Upvotes: 3

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

I believe documentation means that you should first compile and build the program and after that call the executable with the argument. So in your case you will need to do something like:

gcc tw_r2fft.c -o tw_r2fft 
./tw_r2fft 1024 > outputfile.c

Upvotes: 14

Related Questions