简体   繁体   English

尝试在 vs code 中安装和运行 C++ 测试代码

[英]trying to install and run C++ test code in vs code

Hi guys i installed the vs code and downloaded the needed extensions (C/C++-code runner) and i installed Mingw but when i run this test code大家好,我安装了 vs 代码并下载了所需的扩展(C/C++-code runner),我安装了 Mingw,但是当我运行这个测试代码时

    #include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  return 0;
}

it shows this它表明了这一点

PS C:\Users\faroo\Desktop\C++ 101> cd "c:\Users\faroo\Desktop\C++ 101\" ; if ($?) { g++ calssCar.cpp -o calssCar } ; if ($?) { .\calssCar }
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

can you please help me你能帮我么

The compiler is working fine but the linker trying to link it as a Windows GUI App, which uses WinMain() instead of main() as entry point of application.编译器工作正常,但链接器试图将其链接为 Windows GUI 应用程序,该应用程序使用WinMain()而不是main()作为应用程序的入口点。

You should recheck your compiler flags in vsCode settings.您应该重新检查 vsCode 设置中的编译器标志。 If I remember properly, for Mingw-w64, the flag is -mwindows .如果我没记错的话,对于 Mingw-w64,标志是-mwindows

I suspect you probably used some template to create vscode project but chose the wrong one (gui app instead of console app)我怀疑您可能使用了一些模板来创建 vscode 项目,但选择了错误的模板(gui 应用程序而不是控制台应用程序)

Also, as a beginner I would recommend using TDM-GCC instead of msys2, as it's better intergreted with Windows and is easier to work with.另外,作为初学者,我建议使用 TDM-GCC 而不是 msys2,因为它可以更好地与 Windows 集成,并且更易于使用。

  1. one thing i noticed is IF YOU RUN YOUR CODE WITHOUT SAVING it is giving this error.我注意到的一件事是,如果您在不保存代码的情况下运行代码,则会出现此错误。

try RUN after saving保存后尝试运行

I suggest you create a new cpp file with another title.Then copy your code to the new file.Try to run the code in the new file again.Maybe it can works.我建议你创建一个新的 cpp 文件,用另一个标题。然后将你的代码复制到新文件中。尝试再次运行新文件中的代码。也许它可以工作。 Such error usually happen to my laptop.It dosen't matter.这种错误通常发生在我的笔记本电脑上。没关系。

It's because maybe you haven't set your path of Mingw.那是因为也许你还没有设定你的 Mingw 路径。 Let me guide you to do that if you're using Windows:如果您使用的是 Windows,让我指导您执行此操作:

  1. Install Mingw (which you already have), like I have installed it in C:\Mingw安装 Mingw(您已经拥有),就像我在 C:\Mingw 中安装它一样
  2. Then open Mingw\bin folder from that drive, like I have opened C:\MinGW\bin然后从该驱动器打开 Mingw\bin 文件夹,就像我打开了 C:\MinGW\bin
  3. Copy "C:\MinGW\bin" and then open This Pc.复制“C:\MinGW\bin”,然后打开这台电脑。
  4. Right-click on open space and click properties.右键单击开放空间,然后单击属性。
  5. Go to Advanced system settings.转到高级系统设置。
  6. Click on the Environment variable.单击环境变量。
  7. Then in system variables, double click on PATH.然后在系统变量中,双击PATH。
  8. Click on new, and paste your path there.单击新建,然后将您的路径粘贴到那里。
  9. Press ok and now run your code again in Vscode, it'll surely work!按确定,现在在 Vscode 中再次运行您的代码,它肯定会工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM