如何用Command Prompt(CMD)编译运行C/C++代码?

这里的版本是Visual Studio 2015,因为目录的问题,自行调整吧

这里主要是通过Visual Studio里的cl.exe来编译我们的C++代码

 

  1. 打开Developer Command Prompt for VS2015(名字可能不同),这个在Start Menu也能找到,注意是Visual Studio的,不是Command Prompt,Command Prompt过后可能会出现问题
  2. 输入cl,情况大概下面差不多,如果没有的话确认好你开的是Visual StudioCommand Prompt

 

1

 

  1. 假设有个hello.cpp在C盘里的hello文件夹里,目录为C:\hello\hello.cpp(如果是C语言的话把.cpp改成.c
  2. hello.cpp的代码
     #include <iostream>using namespace std;
    int main()
    {
    cout<<"Hello Kitty"<<endl;
    return 0;
    }
  3. 输入cd C:\hello 去到C里的hello目录
  4. 输入 cl  /EHsc hello.cpp,就会生成.obj文件和.exe文件
  5. cl.exe 编译器会生成包含已编译代码的 .obj 文件,然后运行链接器来创建名为 hello.exe 的可执行程序
  6. 输入hello来运行hello.exe
  7. 大功告成  

 

screenshot2

 

 

更多讨论在 点我进入