C语言 如何使用 gcc 运行(和编译)带有参数的 C 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19031980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:33:12  来源:igfitidea点击:

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

cgcccommand-line

提问by Semjon M?ssinger

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:

我认为这是一个非常简单的问题,但我对命令行的经验较少。我有一个我想运行的旧 C 程序。它显示以下文本:

/*      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):

我试过(在cmd中):

gcc tw_r2fft.c 1024 > outputfile.c

gcc's error message was:

gcc 的错误信息是:

gcc: 1024: No such file or directory

I tried some variations but without success.

我尝试了一些变化,但没有成功。

回答by Ivaylo Strandjev

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

回答by buc

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

尝试将 C 程序编译为可执行二进制文件:

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

回答by Saurabh Rana

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

gcc 是编译器,tw_r2fft.c 是你的文件名。-o 是一种更改输出文件名的方法。您可以在没有 -o 的情况下编译程序,但默认情况下编译器会将您的输出文件保存为 ./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

这一行执行你的输出文件并传递一个命令行参数,即 1024,整个程序的输出被保存到 outputfile.c

./tw_r2fft 1024 > outputfile.c

Still need help

仍然需要帮助

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

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

回答by Kevin

You need to compile the program before you run it

您需要先编译程序,然后再运行它

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

回答by Jonathan Leffler

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

回答by Anibal Anto

You can try this:

你可以试试这个:

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

or without the -o option:

或不带 -o 选项:

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