博客
关于我
gcc编译c++文件
阅读量:110 次
发布时间:2019-02-26

本文共 2559 字,大约阅读时间需要 8 分钟。

gcc是编译c语言的,默认情况下,如果直接编译c++程序,会报错:

[root@server demo2]# ls

hello.cpp
[root@server demo2]# cat hello.cpp 
#include <iostream>
using namespace std;
int main(){
  cout<<"hello,c++"<<endl;
  return 0;
}
[root@server demo2]# gcc -o hello hello.cpp 
/tmp/ccAa6oYP.o: In function `main':
hello.cpp:(.text+0xa): undefined reference to `std::cout'
hello.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*)'
hello.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> >&)'
hello.cpp:(.text+0x1c): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccAa6oYP.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x4a): undefined reference to `std::ios_base::Init::Init()'
hello.cpp:(.text+0x59): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

我们可以通过增加参数-lstdc++来编译,结果如下:

[root@server demo2]# gcc -o hello hello.cpp -lstdc++

[root@server demo2]# ls
hello  hello.cpp
[root@server demo2]# ./hello 
hello,c++
[root@server demo2]# 
如果编译c++程序,可以直接通过g++命令来编译,如下:

可以使用更简单的,直接g++ hello.cpp,这样生成的文件就是a.out

[root@server demo2]# g++ hello.cpp 

[root@server demo2]# ls
a.out  hello.cpp
[root@server demo2]# ./a.out 
hello,c++
[root@server demo2]# 

多个文件编译:准备circle.h,circle.cpp,main.cpp

circle.h

#ifndef CIRCLE_H#define CIRCLE_Hclass Circle{  private:    double r;  public:    Circle();    Circle(double r);    double area();};#endif

circle.cpp

#include "circle.h"Circle::Circle(){   this->r = 5;}Circle::Circle(double r){   this->r = r;}double Circle::area(){   return 3.14*r*r;}

main.cpp

#include 
#include "circle.h"using namespace std;int main(){ Circle c(3); cout<<"area => "<
<

多个文件编译,使用gcc编译,就类似这样:gcc -o main main.cpp circle.cpp -lstdc++,该命令编译中,文件不能带上circle.h头文件,否则会报错。

[root@server demo1]# ls

circle.cpp  circle.h  main.cpp
[root@server demo1]# gcc -o main main.cpp circle.cpp -lstdc++
[root@server demo1]# ls
circle.cpp  circle.h  main  main.cpp
[root@server demo1]# ./main
area => 28.26
[root@server demo1]# 

使用g++编译:可以带上头文件circle.h

[root@server demo1]# ls

circle.cpp  circle.h  main.cpp
[root@server demo1]# g++ -o main2 main.cpp circle.h circle.cpp
[root@server demo1]# ls
circle.cpp  circle.h  main2  main.cpp
[root@server demo1]# ./main2 
area => 28.26
[root@server demo1]# 

转载地址:http://xzey.baihongyu.com/

你可能感兴趣的文章
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>