Compiling C++ in cmd using MinGW on Windows 8

北城以北 提交于 2020-01-06 14:43:11

问题


I'm trying to compile c++ programs from the commandline, but I can't. I tried all the steps and instructions listed here.

I got MinGW installed, but I get stuck in the last step of the installation that explains how to change environment paths. There simply isn't PATH part of it on the environment variables, and when I try to add it or even edit the one in System variables, compiling doesn't work (nothing comes up when I type g++ on the command prompt).

I've searched online for solutions, but I haven't been able to find anything.

Reinstalling MinGW32 resolved the issue of not being able to compile C++ at all.

Adding ;C:\mingw\bin in the PATH variable under Advanced Settings in Properties in My Computer fixed compiling in cmd, as answered by @Chrono Kitsune.


回答1:


It looks like either PATH variable is removed from regedit or it has grown to size of more than 2048. To find out

  1. Open cmd window and type PATH, if it prints the path. Something like: C:\Users\robin>PATH
    PATH=C:\ProgramData\Oracle\Java\javapath;............
    Then most probably it has grown to more than 2048 and that is why its not visible. You can update your Path variable in the command line by this command:
    set PATH=%PATH%;C:\cygwin64\bin
    So it will append Path with "C:\cygwin64\bin"

  2. If that doesn't work. Then check in regedit if these are present: HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
    HKCU\Environment
    If not, export them from another PC and then import.




回答2:


  1. Press Win+R to open the Run dialog and type

    sysdm.cpl ,3
    
  2. Click the "Environment Variables" button.
  3. There are two sections: "User Variables" (top) and "System Variables" (bottom). You want to look at the top section.
  4. If you don't have a PATH variable, click the "New" button and type PATH for the variable name. Otherwise, just double-click the existing PATH variable.
  5. Now add the following to the end of the variable's value:

    ;C:\mingw\bin
    

    where C:\mingw\bin is the folder containing g++.exe.

  6. Click OK on each dialog box to close it.
  7. Open a new command prompt and try to execute

    g++.exe -xc -E nul
    

    If you still get an error, try logging off and logging back on.




回答3:


I prefer using a Linux OS for compiling C/C++ programs. I use VirtualBox to host a Ubuntu machine and it works very well.

If you go that route to compile, navigate to the directory using the 'cd' command, and then type 'make' to compile and ./yourprogram to execute.



来源:https://stackoverflow.com/questions/35833343/compiling-c-in-cmd-using-mingw-on-windows-8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!