如何在 Ubuntu Linux 下编译 C++?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1696300/
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 C++ under Ubuntu Linux?
提问by Carl Smotricz
I cut&pasted the below code from a previous questioninto a file called "avishay.cpp" and then ran
我将上一个问题中的以下代码剪切并粘贴到名为“avishay.cpp”的文件中,然后运行
gcc avishay.cpp
only to get the following error messages from the linker. What went wrong, what should I have done?
只是为了从链接器获取以下错误消息。出了什么问题,我应该怎么做?
carl@carl-ubuntu:~/Projects/StackOverflow$ gcc -static avishay.cpp
/tmp/cccRNW34.o: In function `__static_initialization_and_destruction_0(int, int)':
avishay.cpp:(.text+0x41): undefined reference to `std::ios_base::Init::Init()'
avishay.cpp:(.text+0x46): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cccRNW34.o: In function `A::func()':
avishay.cpp:(.text._ZN1A4funcEv[A::func()]+0x11): undefined reference to `std::cout'
avishay.cpp:(.text._ZN1A4funcEv[A::func()]+0x16): 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> >&, char const*)'
avishay.cpp:(.text._ZN1A4funcEv[A::func()]+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
avishay.cpp:(.text._ZN1A4funcEv[A::func()]+0x26): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
avishay.cpp:(.text._ZN1A4funcEv[A::func()]+0x36): undefined reference to `std::cout'
avishay.cpp:(.text._ZN1A4funcEv[A::func()]+0x3b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
/tmp/cccRNW34.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
The C++ code (not my code, I was just trying to run it):
C++ 代码(不是我的代码,我只是想运行它):
#include <iostream>
using namespace std;
class A
{
private:
int _dmember;
public:
void func()
{
cout<<"Inside A!! "<<endl;
cout<<_dmember; // crash when reach here.
}
};
int main ()
{
A* a= NULL;
a->func(); // prints "Inside A!!!"
return 1;
}
回答by Thomas
You should use g++
, not gcc
, to compile C++ programs.
您应该使用g++
,而不是gcc
来编译 C++ 程序。
For this particular program, I just typed
对于这个特定的程序,我只是输入
make avishay
and let make
figure out the rest. Gives your executable a decent name, too, instead of a.out
.
make
剩下的就让我们来解决吧。也给你的可执行文件一个合适的名字,而不是a.out
.
回答by Hymanrabbit
You probably should use g++ rather than gcc.
您可能应该使用 g++ 而不是 gcc。
回答by blwy10
Yes, use g++ to compile. It will automatically add all the references to libstdc++ which are necessary to link the program.
是的,使用 g++ 编译。它将自动添加链接程序所需的对 libstdc++ 的所有引用。
g++ source.cpp -o source
If you omit the -o
parameter, the resultant executable will be named a.out
. In any case, executable permissions have already been set, so no need to chmod
anything.
如果省略该-o
参数,则生成的可执行文件将命名为a.out
. 无论如何,可执行权限已经设置,所以不需要chmod
任何东西。
Also, the code will give you undefined behaviour (and probably a SIGSEGV) as you are dereferencing a NULL pointer and trying to call a member function on an object that doesn't exist, so it most certainly will not print anything. It will probably crash or do some funky dance.
此外,当您取消引用 NULL 指针并尝试在不存在的对象上调用成员函数时,代码会给您未定义的行为(可能还有 SIGSEGV),因此它肯定不会打印任何内容。它可能会崩溃或跳一些时髦的舞蹈。
回答by Manish Bhadani
Update your apt-get:
更新您的 apt-get:
$ sudo apt-get update
$ sudo apt-get install g++
Run your program.cpp:
运行你的程序.cpp:
$ g++ program.cpp
$ ./a.out
回答by chris
Use
用
g++
加++
space followed by the program name. e.g:
空格后跟程序名称。例如:
g++ prog.cpp
g++ prog.cpp
if the filename was "prog.cpp" in this case. if you want to run the program write:
如果在这种情况下文件名为“prog.cpp”。如果你想运行程序写:
./prog
so i used
所以我用
"prog"
“编”
because it was my filename.
因为这是我的文件名。
回答by Javed
even you can compile your c++ code by gcc Sounds funny ?? Yes it is. try it
甚至你可以用 gcc 编译你的 C++ 代码 听起来很有趣??是的。尝试一下
$ gcc avishay.cpp -lstdc++
enjoy
请享用
回答by monksy
g++ is the C++ compiler under linux. The code looks right. It is possible that you are missing a library reference which is used as such:
g++是linux下的C++编译器。代码看起来是正确的。您可能缺少这样使用的库参考:
g++ -l{library name here (math fns use "m")} codefile.cpp
g++ -l{此处为库名(数学 fns 使用“m”)} codefile.cpp
回答by monksy
Use g++. And make sure you have the relevant libraries installed.
使用 g++。并确保您安装了相关的库。
回答by MSharq
you can use g++ --std=c++0x example.cpp -o example
你可以使用 g++ --std=c++0x example.cpp -o example
回答by x0x
To compile source.cpp
, run
要编译source.cpp
,运行
g++ source.cpp
This command will compile source.cpp
to file a.out
in the same directory.
To run the compiled file, run
此命令将编译source.cpp
为a.out
同一目录中的文件。要运行编译的文件,请运行
./a.out
If you compile another source file, with g++ source2.cpp
, the new compiled file a.out
will overwrite the a.out
generated with source.cpp
如果你编译另一个源文件,用g++ source2.cpp
,新编译的文件a.out
将覆盖a.out
生成的source.cpp
If you want to compile source.cpp
to a specific file, say compiledfile
,
run
如果要编译source.cpp
为特定文件,请说compiledfile
,运行
g++ source.cpp -o compiledfile
or
或者
g++ -o compiledfile source.cpp
This will create the compiledfile
which is the compiled binary file. to run the compiledfile
, run
这将创建compiledfile
编译后的二进制文件。运行compiledfile
,运行
./compiledfile
If g++
is not in your $PATH
, replace g++ with /usr/bin/g++
.
如果g++
不在您的 中$PATH
,请将 g++ 替换为/usr/bin/g++
.