使用gcc编译c语言程序,用GCC编译C ++程序
在這里,我們將看到如何使用GCC(GNU C編譯器)編譯C ++程序。讓我們考慮一下,我們要編譯這個程序。
示例#include
using?namespace?std;
main()?{
cout?<
}
如果這是一個C程序,我們可以像下面這樣用GCC進行編譯-gcc?test.c
但是,如果我們將c ++文件名放在該區域中,則可能會產生一些錯誤。gcc?test.cpp
輸出結果/tmp/ccf1KGDi.o:?In?function?`main':
1325.test.cpp:(.text+0xe):?undefined?reference?to?`std::cout'
1325.test.cpp:(.text+0x13):?undefined?reference?to?`std::basic_ostream?>&
std::operator<?>(std::basic_ostream?>&,?char?const*)'
1325.test.cpp:(.text+0x1d):?undefined?reference?to?`std::basic_ostream?>&?std::endl
std::char_traits?>(std::basic_ostream?>&)'
1325.test.cpp:(.text+0x28):?undefined?reference?to?`std::ostream::operator<
/tmp/ccf1KGDi.o:?In?function?`__static_initialization_and_destruction_0(int,?int)':
1325.test.cpp:(.text+0x58):?undefined?reference?to?`std::ios_base::Init::Init()'
1325.test.cpp:(.text+0x6d):?undefined?reference?to?`std::ios_base::Init::~Init()'
collect2:?error:?ld?returned?1?exit?status
$
這不是編譯錯誤。這是鏈接錯誤。要添加正確的鏈接器,我們必須使用–lstdc ++選項。gcc?test.cpp?-lstdc++
輸出結果$?./a.out
Hello?World.?This?is?C++?program
$
總結
以上是生活随笔為你收集整理的使用gcc编译c语言程序,用GCC编译C ++程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DDR4内存兼容DDR5?别慌,这些经验
- 下一篇: c语言函数的程序设计,C语言程序设计第3