【问题标题】:How to compile and run in one line on linux terminal?如何在linux终端上一行编译运行?
【发布时间】:2017-06-11 03:38:51
【问题描述】:

每次编译运行c文件,我都要输入:

gcc 文件名.c

a.out

我不想分两行做,如何在linux终端上一行编译运行?

【问题讨论】:

标签: c linux gcc terminal


【解决方案1】:

试试

gcc filename.c && a.out

如果第一个命令成功,它只会运行第二个命令。见https://askubuntu.com/questions/334994/which-one-is-better-using-or-to-execute-multiple-commands-in-one-line

【讨论】:

  • 这个问题的一个问题是,程序可能会在编译时出现警告,然后在运行时没有给用户检查警告的机会。请参阅下面的答案。
【解决方案2】:

您可以使用; 分隔命令。比如:

gcc filename.c; ./a.out

但是,如果编译成功,您可能只想运行 a.out。为此,您可以使用&&:

gcc filename.c && ./a.out

【讨论】:

    【解决方案3】:

    快速而肮脏的解决方案是错误的

    快速而肮脏的解决方案在 C 中是一个非常糟糕的主意:

    gcc myfile.c && ./a.out
    

    如果编译失败,这将不会运行可执行文件,但是当编译成功时,代码即使发出警告也会自动运行;在 C 语言中,您应该始终在尝试运行代码之前至少检查警告。在大多数情况下,您不应该只运行编译时带有警告的代码。通常运行带有警告的代码意味着代码有一些未定义的行为;你不应该如此盲目地奔跑。当然,在上面的代码中至少有警告,可能不会有很多警告,但无论如何应该有。在绝对最小值,应该使用:

    gcc myfile.c -Wall -Wextra -Werror && ./a.out
    

    使用-Wall -Wextra 会针对许多导致未定义行为的愚蠢错误发出警告,而-Werror 会防止编译后带有警告的代码自动运行。

    更好的解决方案

    为了解决这个问题,输入less,我曾经在我的搜索路径中使用这个保存为crepl的bash脚本:

    #!/bin/bash
    gcc -std=c99 -Wall -Wextra -Wpedantic -Werror $1 -o tempout &&\
        ./tempout && rm tempout
    

    当我想快速测试一些保存在其中的源代码时,例如myfile.c,我可以在命令行输入:

    crepl myfile.c
    

    由于-Werror,如果编译失败,代码将不会运行,如果编译时出现警告,它将不会运行。如果编译成功,则程序运行,运行后临时可执行文件被删除。

    改进

    自从最初写这个答案以来,我已经将我的解决方案演变成一个稍微花哨的 bash 脚本,它接受编译器、链接库等可选的进一步参数。

    #!/usr/bin/env bash
    # crun
    #
    # A script to invoke gcc, build the executable, execute the binary,
    # and cleanup after. The script will exit without running the executable
    # if there are any compiler errors or warnings.
    #
    # This script uses -std=c18, but it could probably be modified so that
    # the version is taken from a command-line parameter, defaulting to c18.
    #
    # Any commands following the crun invocation are appended to CMD.
    CMD="gcc -std=c18 -Wall -Wextra -Wpedantic -Werror"
    TEMPFILE="tempfile$$"
    for ARG in "$@"
    do
        CMD+=" $ARG"
    done
    CMD+=" -o ${TEMPFILE} && ./${TEMPFILE} && rm ${TEMPFILE}"
    
    eval $CMD
    

    现在如果我需要链接,例如数学库:

    crun myfile.c -lm
    

    成功了,如果出现任何错误或警告(已达到合理水平)则失败,然后自行清理。

    【讨论】:

      【解决方案4】:

      您想要如何编译和运行。 我给你如何编写、编译和运行。

      cat << EOF > temp.c && gcc temp.c ; ./a.out ; rm temp.c a.out
      #include <stdio.h>
      int main(){ printf("%s\n", "Hello World!"); return 0; }
      EOF
      

      如果你经常使用这样的东西,你可能也会对此感兴趣:

      #!/bin/sh
      # GCC in the streams
      
      temp="temp.c"
      
      sgcc() {
          (
              rm "$temp" # just to be sure
              # append every line from shell to the temp file
              while read -r l; do printf "%s\n" "$l" >>"$temp"; done
              cat "$temp"
              gcc "$temp"
              exitcode=$?
              ./a.out
              rm "$temp" a.out
              exit $exitcode
          )
      }
      

      你可以像下面这样调用:

      sgcc << EOF
      #include <stdio.h>
      int main(){ printf("%s\n", "Hello World!"); return 0; }
      EOF
      

      【讨论】:

        【解决方案5】:

        解释:

        A ; B – 运行A,然后运行B,不管A的成功或失败

        A && B – 仅当 A 成功时才运行 B

        A || B – 仅当 A 失败时才运行 B

        【解决方案6】:

        在你的 .(shell)rc 中添加别名:

        alias gcb='f(){ gcc "$1" -o "$2"; ./"$2"; unset -f f; }; f'
        alias gcr='f(){ gcc "$1"; ./a.out; rm -f a.out; unset -f f; }; f'
        

        第一个修改二进制文件的名称并执行它。第二个编译、运行和删除二进制文件。

        演示 gcb:

        ~$ gcb main.c build 
        Hello World
        ~$ ls
        main.c build
        

        演示 gcr:

        ~$ gcr main.c  
        Hello World
        ~$ ls
        main.c 
        

        访问别名here的原始结构。

        【讨论】:

          猜你喜欢
          • 2022-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-06
          • 2014-02-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多