如何在 Linux 上编译 .cpp 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6194948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:59:47  来源:igfitidea点击:

How do I compile a .cpp file on Linux?

c++linux

提问by pluc

I'm fairly comfortable with Linux and compiling things - I normally just follow the instructions and can manage to get myself out of trouble. This time, I was given a .cpp file by a random Internet citizen and I would really like to know how to compile it. Everything I seem to try (g++, c++, gcc) doesn't seem to work.

我对 Linux 和编译东西相当满意 - 我通常只是按照说明进行操作,并且可以设法让自己摆脱困境。这一次,我随机收到了一个.cpp文件,我很想知道如何编译它。我似乎尝试的一切(g++、c++、gcc)似乎都不起作用。

Anyhow, here's the file: http://pastebin.ca/2073013

无论如何,这是文件:http: //pastebin.ca/2073013

Edit: Updated with verbose output from g++ file.cpp -o whatever: http://pastebin.ca/2073052

编辑:更新详细输出g++ file.cpp -o whateverhttp: //pastebin.ca/2073052

回答by loudandclear

You'll need to compile it using:

您需要使用以下命令编译它:

g++ inputfile.cpp -o outputbinary

The file you are referring has a missing #include <cstdlib>directive, if you also include that in your file, everything shall compile fine.

您所引用的文件缺少一个#include <cstdlib>指令,如果您还将该指令包含在您的文件中,则一切都可以正常编译。

回答by rwik

Just type the code and save it in .cpp format. then try "gcc filename.cpp" . This will create the object file. then try "./a.out" (This is the default object file name). If you want to know about gcc you can always try "man gcc"

只需键入代码并将其保存为 .cpp 格式。然后尝试 "gcc filename.cpp" 。这将创建目标文件。然后尝试“./a.out”(这是默认的目标文件名)。如果您想了解 gcc,您可以随时尝试“man gcc”

回答by David Grayson

The compiler is telling you that there are problems starting at line 122 in the middle of that strange FBI-CIA warning message. That message is not valid C++ code and is NOT commented out so of course it will cause compiler errors. Try removing that entire message.

编译器告诉您在奇怪的 FBI-CIA 警告消息中间的第 122 行开始存在问题。该消息不是有效的 C++ 代码并且没有被注释掉,所以它当然会导致编译器错误。尝试删除整个消息。

Also, I agree with In silico: you should always tell us what you tried and exactly what error messages you got.

另外,我同意 In silico:您应该始终告诉我们您尝试了什么以及您收到了哪些错误消息。