如何在 Linux 中编译 C++ 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7005713/
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
How to compile a c++ program in Linux?
提问by Chankey Pathak
I made a file hi.cpp and I wrote the command given below:
我创建了一个文件 hi.cpp 并编写了下面给出的命令:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}
then I ran it in my RHEL 6 machine with the following command
然后我使用以下命令在我的 RHEL 6 机器上运行它
gcc hi.cpp
and I got some errors which are as follows:
我得到了一些错误,如下所示:
[chankey@localhost ~]$ gcc hi.cpp
/tmp/cc32bnmR.o: In function `main':
hi.cpp:(.text+0xa): undefined reference to `std::cout'
hi.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, const char*)'
hi.cpp:(.text+0x19): undefined reference to `std::cout'
hi.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, const char*)'
/tmp/cc32bnmR.o: In function `__static_initialization_and_destruction_0(int, int)':
hi.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::Init()'
hi.cpp:(.text+0x51): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc32bnmR.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[chankey@localhost ~]$
What do these errors denote? My code is correct then why am I getting errors?
这些错误表示什么?我的代码是正确的,为什么我会出错?
回答by Jesus Ramos
Use g++
使用 g++
g++ -o hi hi.cpp
g++ is for C++, gcc is for C although with the -libstdc++ you can compile c++ most people don't do this.
g++ 用于 C++,gcc 用于 C,尽管使用 -libstdc++ 您可以编译 C++,但大多数人不会这样做。
回答by Thomas Padron-McCarthy
As the other answers say, use g++instead of gcc.
正如其他答案所说,使用g++而不是gcc。
Or use make: make hi
或者使用 make: make hi
回答by iammilind
You have to use g++ (as mentioned in other answers). On top of that you can think of providing some good options available at command line (which helps you avoid making ill formed code):
您必须使用 g++ (如其他答案中所述)。最重要的是,您可以考虑在命令行中提供一些好的选项(这有助于您避免编写格式错误的代码):
g++ -O4 -Wall hi.cpp -o hi.out
^^^^^ ^^^^^^
optimize related to coding mistakes
For more detail you can refer to man g++ | less
.
有关更多详细信息,您可以参考man g++ | less
。
回答by Dimme
Try this:
尝试这个:
g++ -o hi hi.cpp
gcc is only for C
gcc 仅适用于 C
回答by Dhavalkumar Prajapati
$ g++ 1st.cpp -o 1st
$ g++ 1st.cpp -o 1st
$ ./1st
$ ./第一个
if you found any error then first install g++ using code as below
如果您发现任何错误,请先使用以下代码安装 g++
$ sudo apt-get install g++
$ sudo apt-get install g++
then install g++ and use above run code
然后安装 g++ 并使用上面的运行代码
回答by Siva Prakash
g++ -o foo foo.cpp
g++ --> Driver for cc1plus compiler
g++ --> cc1plus 编译器驱动
-o --> Indicates the output file (foois the name of output file here. Can be any name)
-o --> 表示输出文件(这里的foo是输出文件的名字,可以是任意名字)
foo.cpp --> Source file to be compiled
foo.cpp --> 需要编译的源文件
To executethe compiled file simply type
要执行编译后的文件,只需键入
./foo