代码之家  ›  专栏  ›  技术社区  ›  verpas

为什么Make会自动执行cc命令?

  •  0
  • verpas  · 技术社区  · 1 年前

    我正在努力学习如何使用Make!所以我有一个简单的Makefile,

    all: main other
    
    main: main.c other.o
        gcc main.c -o main
        echo "Compiled main.exe"
    
    other: other.o
    
    other.c:
        touch $@
        echo "int main(){return 0;}" > $@
        echo "Created other.c"
    
    other.o: other.c
        gcc other.c -o other.o
        echo "Compiled other.o"
    
    clean:
        rm -f main.exe other.o other.c
    

    其中除了在执行cc编译命令的末尾之外,所有内容都按预期执行。这是输出(我添加了箭头),

    > make all  
    touch other.c
    echo "int main(){return 0;}" > other.c
    echo "Created other.c"
    Created other.c
    gcc other.c -o other.o
    echo "Compiled other.o"
    Compiled other.o
    gcc main.c -o main
    echo "Compiled main.exe"
    Compiled main.exe
    cc   other.o   -o other     <---
    /usr/lib/gcc/x86_64-pc-msys/13.2.0/../../../../x86_64-pc-msys/bin/ld: other.o: in functio
    n `mainCRTStartup':
    /c/S/B/src/msys2-runtime/winsup/cygwin/crt0.c:20: multiple definition of `mainCRTStartup'
    ; /usr/lib/gcc/x86_64-pc-msys/13.2.0/../../../crt0.o:/c/S/B/src/msys2-runtime/winsup/cygw
    in/crt0.c:20: first defined here
    /usr/lib/gcc/x86_64-pc-msys/13.2.0/../../../../x86_64-pc-msys/bin/ld: other.o: in functio
    n `mainCRTStartup':
    /c/S/B/src/msys2-runtime/winsup/cygwin/crt0.c:20: multiple definition of `WinMainCRTStart
    up'; /usr/lib/gcc/x86_64-pc-msys/13.2.0/../../../crt0.o:/c/S/B/src/msys2-runtime/winsup/c
    ygwin/crt0.c:20: first defined here
    /usr/lib/gcc/x86_64-pc-msys/13.2.0/../../../../x86_64-pc-msys/bin/ld: other.o:cygming-crt
    beg:(.text+0x50): multiple definition of `__gcc_register_frame'; /usr/lib/gcc/x86_64-pc-m
    sys/13.2.0/crtbegin.o:cygming-crtbeg:(.text+0x0): first defined here
    /usr/lib/gcc/x86_64-pc-msys/13.2.0/../../../../x86_64-pc-msys/bin/ld: other.o:cygming-crt
    beg:(.text+0x70): multiple definition of `__gcc_deregister_frame'; /usr/lib/gcc/x86_64-pc
    -msys/13.2.0/crtbegin.o:cygming-crtbeg:(.text+0x20): first defined here
    /usr/lib/gcc/x86_64-pc-msys/13.2.0/../../../../x86_64-pc-msys/bin/ld: other.o:cygming-crt
    beg:(.data+0x0): multiple definition of `__dso_handle'; /usr/lib/gcc/x86_64-pc-msys/13.2.
    0/crtbegin.o:cygming-crtbeg:(.data+0x0): first defined here
    collect2: error: ld returned 1 exit status
    make: *** [<builtin>: other] Error 1
    

    “<---”所指向的是有问题的线。为什么这会被处决?

    2 回复  |  直到 1 年前
        1
  •  3
  •   Some programmer dude    1 年前

    问题是要生成的默认程序 和链接 程序是 cc 。按照规则 other: other.o 你告诉我 make 以生成可执行文件 other 从对象文件 other.o 然后它将使用默认链接器来执行此操作。

    这些错误是因为 gcc other.c -o other.o 构建 可执行程序 名为的文件 另外o 。若要生成对象文件,您需要使用 -c 选项

    实际上,解决所有问题的简单方法是设置 CC LD 变量,并依赖于隐式规则来构建代码。

    这样一个 Makefile 可能看起来像这样:

    # Explicitly use gcc as the compiler and linker
    CC = gcc
    LD = gcc
    
    # The "default" target, because the first target in the makefile
    # is what make will use when no target is specified
    default: main other
    
    # Build the main executable program from the main.c source
    main: main.c
    
    # Build the other executable program from the other.c source
    other: other.c
    
    # Create the other.c source file
    other.c:
        echo "int main(){return 0;}" > $@
    
    # The clean target isn't for creating a "clean" file, it's phony, fake
    .PHONY: clean
    clean:
        -rm -f main other other.c
    

    [请注意,您需要将缩进转换为制表符]

        2
  •  2
  •   9769953    1 年前

    为什么Make会自动执行cc命令?

    这些都是隐含的规则。

    假设您使用的是GNU Make,请进行通读 https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html ,其中说明

    链接单个对象文件

    n is made automatically from n.o by running the C compiler to
    link the program. The precise recipe used is ‘$(CC) $(LDFLAGS) n.o
    $(LOADLIBES) $(LDLIBS)’.
    

    代替 n 通过 other 对于您的案例。)

    为了理解上述变量,例如 CC ,另请参阅 the section on implicit variables 。这就是为什么你看到 cc 在那条线上:

    科科斯群岛

    用于编译C程序的程序;默认抄送。

    科科斯群岛 变量用于链接部分。