在ubuntu shell上编译cpp程序时出现问题[重复]
Posted
技术标签:
【中文标题】在ubuntu shell上编译cpp程序时出现问题[重复]【英文标题】:Issue when compilin cpp program on ubuntu's shell [duplicate] 【发布时间】:2017-05-18 10:06:18 【问题描述】:所以我尝试了 Linux C++ 编译器,这发生在编译以下代码时:
#include <iostream>
using namespace std;
int main()
cout<<"Hello World!\n"<<endl;
return 0;
ceibal@ceibal:~$ gcc HelloWorld.cpp
/tmp/cc78Dkhk.o: In function `main':
HelloWorld.cpp:(.text+0xa): undefined reference to `std::cout'
HelloWorld.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> >&, char const*)'
HelloWorld.cpp:(.text+0x14): 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> >&)'
HelloWorld.cpp:(.text+0x1c): 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> >&))'
/tmp/cc78Dkhk.o: In function `__static_initialization_and_destruction_0(int, int)':
HelloWorld.cpp:(.text+0x4a): undefined reference to `std::ios_base::Init::Init()'
HelloWorld.cpp:(.text+0x4f): undefined reference to `std::ios_base::Init::~Init()'
collect2: ld devolvió el estado de salida 1
ceibal@ceibal:~$ pico HelloWorld.cpp
ceibal@ceibal:~$ gcc HelloWorld.cpp
HelloWorld.cpp:3:1: error: expected ‘;’ before ‘int’
ceibal@ceibal:~$ pico HelloWorld.cpp
ceibal@ceibal:~$ gcc HelloWorld.cpp
/tmp/ccIivRal.o: In function `main':
HelloWorld.cpp:(.text+0xa): undefined reference to `std::cout'
HelloWorld.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> >&, char const*)'
HelloWorld.cpp:(.text+0x14): 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> >&)'
HelloWorld.cpp:(.text+0x1c): 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> >&))'
/tmp/ccIivRal.o: In function `__static_initialization_and_destruction_0(int, int)':
HelloWorld.cpp:(.text+0x4a): undefined reference to `std::ios_base::Init::Init()'
HelloWorld.cpp:(.text+0x4f): undefined reference to `std::ios_base::Init::~Init()'
collect2: ld devolvió el estado de salida 1
我尝试使用相同的程序,但在 C 上运行良好,但当我将它翻译成 c++ 时,这种情况发生了。
【问题讨论】:
【参考方案1】:$ gcc HelloWorld.cpp
这会调用 C 编译器,但您的代码是 C++。使用g++
调用C++编译器。
$ g++ HelloWorld.cpp
【讨论】:
以上是关于在ubuntu shell上编译cpp程序时出现问题[重复]的主要内容,如果未能解决你的问题,请参考以下文章
在 Linux 服务器上编译 C++ 程序时 Intel 编译器的语法
在 macOS 上编译 XDR 时出现问题(RPC types.h 问题)