compiling .cpp with gcc

compiling .cpp with gcc

本文关键字:gcc with cpp compiling      更新时间:2023-10-16

好吧,这是我无意中做的,使用gcc而不是g++编译.cpp

但我实际上想逐行理解控制台的输出,如果它有意义的话。

struct a{
   int pointer;
   int  rollno;
};
struct a student,*studentref;
studentref = &student;
studentref->rollno = 141; 
studentref->pointer = 8;
cout<<studentref->rollno<<") : "<<studentref->pointer<<endl;

使用gcc structpointers.cpp -o structp编译此代码会得到以下输出:

sourab@sourab:/home/gbear/coding/learningds$ gcc structpointers.cpp -o structp
/tmp/ccXrq1Cv.o: In function `main':
structpointers.cpp:(.text+0x2e): undefined reference to `std::cout'
structpointers.cpp:(.text+0x33): undefined reference to `std::ostream::operator<<(int)'
structpointers.cpp:(.text+0x38): 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> >&)'
structpointers.cpp:(.text+0x40): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccXrq1Cv.o: In function `__static_initialization_and_destruction_0(int, int)':
structpointers.cpp:(.text+0x6e): undefined reference to `std::ios_base::Init::Init()'
structpointers.cpp:(.text+0x7d): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

.cpp文件上调用g++gcc之间最显著的区别是g++在C++标准库中自动链接,而gcc没有;您看到的所有错误都是缺少对C++标准库提供的内容的引用的链接器错误。

(请注意,这并不是唯一的差异;有关详细信息,请参阅此问题)

g++和gcc是GNU编译器集合的编译器驱动程序,它们有后端,一旦编译模式启动,它们就会自动连接。像""这样的错误

structpointers.cpp:(.text+0x2e): undefined reference to `std::cout'

是链接错误,因为您使用了gcc来编译cpp,所以不存在专门用于cpp的标准库,所以它找不到std::cout等语法。

您也可以参考:使用g++编译