修复 VS Code 中 运行 C 时 'main' 的多重定义
Fixing multiple definition of 'main' when running C in VS Code
我正在使用 VS Code 版本 1.62.0(用户设置)
我正在尝试 运行 C 中的基本程序 (test.c):
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello");
return 0;
}
我正在尝试通过单击播放符号 (CTRL+ALT+N) 运行,但它在终端中变成这样:
PS C:\Users\X\Dropbox\My PC_X\Downloads> cd "c:\Users\X\Dropbox\My PC_X\Downloads\est\" ; if ($?) { g++ test.c *.c -o test } ; if ($?) { .\test }
C:\Users\X\AppData\Local\Temp\ccyBS6yO.o:test.c:(.text+0x0): multiple definition of `main'
C:\Users\X\AppData\Local\Temp\ccwvYj0K.o:test.c:(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
我不知道这些信息是否有帮助。为了您的信息,我正在使用 Windows 11(在我的情况下,我认为它真的坏了)。以前,我将 [桌面、文档、图片] 与 OneDrive 同步(OneDrive 成为我的本地文件夹)。但是前两天,我关闭了Documents的同步,有些文档完全在OneDrive中(不一定在我的本地文件夹中)[为了更清楚,看图片]
这是我的 gcc 版本:
gcc.exe (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
这是我的 task.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\MinGW\bin\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: C:\MinGW\bin\gcc.exe"
}
]
}
来自您的 IDE 配置,您正在尝试编译您的文件,然后是所有 .c 文件,因此包含第一个文件,所以它有 2 个主文件,因为您编译了同一个文件两次
g++ test.c *.c -o test
if ($?) { g++ test.c *.c -o test }
啊,所以展开后*.c
你运行
g++ test.c test.c -o test
所以,正如错误消息所说,您已经定义了两次 main
- 在 test.c
的两个副本中。因此,从您的构建作业中删除 *.c
。
我正在使用 VS Code 版本 1.62.0(用户设置)
我正在尝试 运行 C 中的基本程序 (test.c):
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello");
return 0;
}
我正在尝试通过单击播放符号 (CTRL+ALT+N) 运行,但它在终端中变成这样:
PS C:\Users\X\Dropbox\My PC_X\Downloads> cd "c:\Users\X\Dropbox\My PC_X\Downloads\est\" ; if ($?) { g++ test.c *.c -o test } ; if ($?) { .\test }
C:\Users\X\AppData\Local\Temp\ccyBS6yO.o:test.c:(.text+0x0): multiple definition of `main'
C:\Users\X\AppData\Local\Temp\ccwvYj0K.o:test.c:(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
我不知道这些信息是否有帮助。为了您的信息,我正在使用 Windows 11(在我的情况下,我认为它真的坏了)。以前,我将 [桌面、文档、图片] 与 OneDrive 同步(OneDrive 成为我的本地文件夹)。但是前两天,我关闭了Documents的同步,有些文档完全在OneDrive中(不一定在我的本地文件夹中)[为了更清楚,看图片]
这是我的 gcc 版本:
gcc.exe (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
这是我的 task.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\MinGW\bin\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: C:\MinGW\bin\gcc.exe"
}
]
}
来自您的 IDE 配置,您正在尝试编译您的文件,然后是所有 .c 文件,因此包含第一个文件,所以它有 2 个主文件,因为您编译了同一个文件两次
g++ test.c *.c -o test
if ($?) { g++ test.c *.c -o test }
啊,所以展开后*.c
你运行
g++ test.c test.c -o test
所以,正如错误消息所说,您已经定义了两次 main
- 在 test.c
的两个副本中。因此,从您的构建作业中删除 *.c
。