c - Makefile 给出重复错误 C

标签 c linux gcc makefile

我创建了一个 makefile 来同时运行 9 个程序。

这9个程序的名称分别是init_aim.c、register.c、sort.c、schedule.c、add.c、class_schedule.c、class_list.c、grade.c、report.c。

在每个文件中我都实现了代码:

#include "aim.h"
int main(){
    return 0;
}

我还有一个包含代码的头文件

#ifndef AIM_H
#define AIM_H
#endif

我唯一的目标是让我的 makefile 正常工作。并添加“全部:”规则。我当前的 makefile 看起来像这样。

    AIS: init_aim.o register.o sort.o schedule.o add.o class_schedule.o 
    class_list.o grade.o report.o
        gcc -Wall -ansi -pedantic init_aim.o register.o sort.o 
    schedule.o add.o class_schedule.o class_list.o grade.o report.o -o AIS

    init_aim.o: init_aim.c aim.h
        gcc -Wall -ansi -pedantic -c init_aim.c -o init_aim.o

    register.o: register.c aim.h
        gcc -Wall -ansi -pedantic -c register.c -o register.o

    sort.o: sort.c aim.h
        gcc -Wall -ansi -pedantic -c sort.c -o sort.o

    schedule.o: schedule.c aim.h
        gcc -Wall -ansi -pedantic -c schedule.c -o schedule.o

    add.o: add.c aim.h
        gcc -Wall -ansi -pedantic -c add.c -o add.o

    class_schedule.o: class_schedule.c aim.h
        gcc -Wall -ansi -pedantic -c class_schedule.c -o 
    class_schedule.o

    class_list.o: class_list.c aim.h
        gcc -Wall -ansi -pedantic -c class_list.c -o class_list.o

    grade.o: grade.c aim.h
        gcc -Wall -ansi -pedantic -c grade.c -o grade.o

    report.o: report.c aim.h
        gcc -Wall -ansi -pedantic -c report.c -o report.o

请记住这里的间距看起来不合适,因为代码太多了。

当我运行 make 时,我得到:

gcc -Wall -ansi -pedantic -c init_aim.c -o init_aim.o
gcc -Wall -ansi -pedantic -c register.c -o register.o
gcc -Wall -ansi -pedantic -c sort.c -o sort.o
gcc -Wall -ansi -pedantic -c schedule.c -o schedule.o
gcc -Wall -ansi -pedantic -c add.c -o add.o
gcc -Wall -ansi -pedantic -c class_schedule.c -o class_schedule.o
gcc -Wall -ansi -pedantic -c class_list.c -o class_list.o
gcc -Wall -ansi -pedantic -c grade.c -o grade.o
gcc -Wall -ansi -pedantic -c report.c -o report.o
gcc -Wall -ansi -pedantic init_aim.o register.o sort.o schedule.o add.o 
class_schedule.o class_list.o grade.o report.o -o AIS
clang: warning: argument unused during compilation: '-ansi' [-Wunused- 
command-line-argument]
    duplicate symbol _main in:
    init_aim.o
    register.o
duplicate symbol _main in:
    init_aim.o
    sort.o
duplicate symbol _main in:
    init_aim.o
    schedule.o
duplicate symbol _main in:
    init_aim.o
    add.o
duplicate symbol _main in:
    init_aim.o
    class_schedule.o
duplicate symbol _main in:
    init_aim.o
    class_list.o
duplicate symbol _main in:
    init_aim.o
    grade.o
duplicate symbol _main in:
    init_aim.o
    report.o
ld: 8 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see 
invocation)
make: *** [AIS] Error 1

如果有人知道如何解决重复错误并添加“all:”命令,请告诉我。

最佳答案

您已经在每个代码文件中定义了main()
main() 是您尝试创建的可执行文件的“从这里开始”点。
由于多个“从这里开始”点,构建失败。

换句话说:
一个可执行文件中只能有一个 main()

您可能会想到多线程编程。
在这种情况下,您需要对该主题进行一些研究。

您可能会考虑从 shell 运行多个程序。
在这种情况下,您需要创建多个可执行文件并使用适当的 shell 功能分别启动它们,例如用于后台执行。

关于c - Makefile 给出重复错误 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51355296/

相关文章:

c++ - 如何使用 C++ 在 ArmNN for Linux 中加载 base onnx 模型

ruby - 在 Mac OS X 10.7.5 上使用 RVM 安装 Ruby 1.9.3 - C 编译器无法创建可执行文件

ruby - 安装 libv8 3.11.8.3 时出错

c++ - 如何获取所有物理接口(interface)

c - 我的数组 printf 循环在末尾缺少一位数字

c - 搜索列表节点错误

c - 哪个是 C 安全编码中嵌套 if 的替代方案?

linux - EC2 Linux 启动/重启时自动运行 sh 脚本

assembly - 使用堆栈框架外的堆栈空间在 cygwin 上的 gcc 输出

c - 遍历矩阵的相邻对角线