【问题标题】:How to run (and compile) a C file with arguments with gcc?如何使用 gcc 运行(和编译)带有参数的 C 文件?
【发布时间】:2013-09-26 15:10:06
【问题描述】:

我认为这是一个非常简单的问题,但我对命令行的经验较少。我有一个想要运行的旧 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.                                    */

我试过(在 cmd 中):

gcc tw_r2fft.c 1024 > outputfile.c

gcc 的错误信息是:

gcc: 1024: No such file or directory

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

【问题讨论】:

    标签: c gcc command-line


    【解决方案1】:

    我相信文档意味着您应该首先编译和构建程序,然后使用参数调用可执行文件。因此,在您的情况下,您需要执行以下操作:

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

    【讨论】:

      【解决方案2】:

      你需要在运行之前编译程序

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

      【讨论】:

        【解决方案3】:

        试试这个把 C 程序编译成可执行的二进制文件:

        gcc -o tw_r2fft tw_r2fft.c
        

        然后使用正确的命令行参数启动二进制文件:

        ./tw_r2fft 1024 >outputfile.c
        

        然后您也可以编译并运行您的输出文件::)

        gcc -o test outputfile.c
        ./test
        

        【讨论】:

          【解决方案4】:

          这一行将编译你的程序

          gcc tw_r2fft.c -o tw_r2fft 
          

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

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

          ./tw_r2fft 1024 > outputfile.c
          

          还是需要帮助

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

          【讨论】:

            【解决方案5】:

            该注释是对如何使用已编译程序的说明。您需要先构建它。

            最简单的:

            make tw_r2fft
            ./tw_r2fft 1024 > outputfile.c
            

            下一个最简单的:

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

            【讨论】:

              【解决方案6】:

              你可以试试这个:

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

              或不带 -o 选项:

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

              【讨论】:

              【解决方案7】:

              一行代码编译运行一个程序:

              gcc source.c -o sourceBinary; ./sourceBinary
              

              分号表示 bash 中的结束语句/中断。

              【讨论】:

                猜你喜欢
                • 2020-06-28
                • 1970-01-01
                • 2023-03-08
                • 1970-01-01
                • 2021-06-03
                • 1970-01-01
                • 2012-01-10
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多