gpt4 book ai didi

c - main() 的多重定义

转载 作者:太空宇宙 更新时间:2023-11-04 00:12:36 24 4
gpt4 key购买 nike

大家好,尝试使用两个 main() 并在 main() 的多个定义中遇到此错误。我重命名了我的主要功能,然后为什么会出现此错误,并且还首先在此处为我的 print() 定义。头文件:

#ifndef TOP_H_
#define TOP_H_

#include <stdio.h>
#include <string.h>
#define onemain main
#define twomain main
inline void print();


#endif /* TOP_H_ */

c文件一:

#include "top.h"
void print();
int onemain()
{
print();
return 0;
}
void print()
{
printf("hello one");
}

c文件二:

#include "top.h"
void print();
int twomain()
{
print();
return 0;
}
void print()
{
printf("hello two");
}

Error snapshot

最佳答案

基本上任何 C(甚至 C++)程序都是一堆相互调用的函数。
要开始执行程序,您必须先选择其中一个函数并调用它。
按照惯例,这个初始函数称为 main

当您在一个项目中包含多个源文件时,IDE 会编译它们,然后调用链接器查找一个名为 main 的函数并生成将调用它的可执行文件。

如果出于任何原因,您在所有这些文件中定义了两个“主要”函数,链接器将警告您它无法自行选择您打算将哪一个作为程序的起点。

关于c - main() 的多重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21508654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com