C++:编译源文件

319 阅读1分钟

从命令行运行编译器

在不同的操作系统中,运行c++编译器的命令也不同。最常用的编译器是GUN编译器和微软Visual Studio编译器。 hello.cpp

#include <iostream>

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

GUN 编译器

默认情况下,运行GUN编译器的命令是 g++

centos: yum install gcc-c++ ubuntu: apt install g++

g++ -o hello hello.cpp

微软编译器

运行微软编译器的命令为 cl

安装Visual Studio时会安装命令行工具,集成了需要用到的环境变量。

image.png

以管理员权限打开一个命令行工具,运行 cl 命令:cl /EHsc hello.cpp

your_project_path>cl /EHsc hello.cpp
用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.16.27045 版
版权所有(C) Microsoft Corporation。保留所有权利。

hello.cpp
Microsoft (R) Incremental Linker Version 14.16.27045.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:hello.exe
hello.obj

/EHsc是编译器选项,用以打开标准异常处理。

不出意外的话,会在当前目录下生成一个可执行文件(.exe),和一个二进制.obj文件(未被链接过,因此不是可执行文件)