简体   繁体   English

如何通过终端编译C++代码

[英]how to compile C++ code through terminal

I am using the atom text editor.我正在使用原子文本编辑器。 I want to compile a simple "hello world" program using the osx terminal (yosemite version 10.10.4).我想使用 osx 终端(优胜美地版本 10.10.4)编译一个简单的“hello world”程序。 I named the program ctut.cpp.我将程序命名为ctut.cpp。 So, then on the terminal I wrote the command g++ ctut.cpp but ended up getting所以,然后在终端上我写了命令g++ ctut.cpp但最终得到

clang: error: no such file or directory: 'ctut.cpp' clang:错误:没有这样的文件或目录:'ctut.cpp'

clang: error: no input files铛:错误:没有输入文件

why is it saying the file does not exist?为什么说文件不存在?

You need to pass the file's path if it is not in your home directory.如果文件不在主目录中,则需要传递文件的路径。 If your file is inside a directory named Code inside the Home directory, then you need to do g++ ~/Code/ctut.cpp .如果您的文件位于Home目录中名为Code的目录中,那么您需要执行g++ ~/Code/ctut.cpp

You do need to cd into the directory where you saved ctut.cpp or use the files path as explained in chintan's answer although your also not typing the full command.您确实需要 cd 进入保存 ctut.cpp 的目录,或者使用 chintan 的回答中解释的文件路径,尽管您也没有输入完整的命令。 It should be g++ ctut.cpp -o ctut then to run the program you type ./ctut应该是g++ ctut.cpp -o ctut然后运行你输入的程序./ctut

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

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