如何编辑、编译和执行 C++ 程序?
c++object oriented programmingprogramming
使用文本编辑器创建一个新的 cpp 文件。在其中输入以下内容 −
#include<iostream> int main() { std::cout << "Hello world"; }
将此文件另存为 source.cpp。
一旦您准备好编译器和源程序,就可以非常轻松地编译和运行 C++ 程序。假设您已安装 GCC 编译器,并且您有一个要编译的 source.cpp 文件,请按照以下说明进行编译和运行。
如果您使用的是 Windows,请打开一个新的终端窗口或 cmd。
将目录更改为您拥有 source.cpp 文件的目录。例如,如果它在 C:/Users/Dell/Documents,请输入您的命令行 −
$ cd 'C:/Users/Dell/Documents'
现在输入以下命令,使用 g++ 编译源文件。
$ g++ -o <name-you-want-to-give> source.cpp
将 <name-you-want-to-give> 替换为任何名称,如 myprogram 等。
运行它!现在您可以使用运行该程序 −
$ ./myprogram