gcc与g++的编译链接的示例详解
一、编译方式的示例详解
1. 编译C代码
代码如下:main.c
/*!
******************************************************************************
* \File
* main.c
* \Brief
* C codes
* \Author
* Hank
******************************************************************************
*/
#include <stdio.h>
int main(int argc, char* argv[])
{
int a = 4;
int b = 3;
int ret = 0;
ret = add(a,b);
printf(" %d + %d = %d\n",a,b,ret);
return;
}
int add(int a, int b)
{
return (a + b);
}
1.1 用gcc编译:
$ gcc -Wall -g main.c -o gcc_compile_c
main.c: In function 'main':
main.c:20: warning: implicit declaration of function 'add'
main.c:23: warning: 'return' with no value, in function returning non-void
运行:
$ ./gcc_compile_c
4 + 3 = 7
1.2. 用g++编译
代码如上所示
$ g++ -Wall -g main.c -o g++_compile_c
main.c: In function 'int main(int, char**)':
main.c:20: error: 'add' was not declared in this scope
main.c:23: error: return-statement with no value, in function returning 'int'
改成如下:
#include <stdio.h>
int add(int a, int b)
{
return (a + b);
}
int main(int argc, char* argv[])
{
int a = 4;
int b = 3;
int ret = 0;
ret = add(a,b);
printf(" %d + %d = %d\n",a,b,ret);
return 0;
}
编译运行:
$ g++ -Wall -g main.c -o g++_compile_c
$ ./g++_compile_c
4 + 3 = 7
可见,C++的规则检查强于C;
2. 编译C++代码
代码如下: main.cpp
#include <iostream>
/*!
******************************************************************************
* \File
* main.cpp
* \Brief
* C++ source code
* \Author
* Hank
******************************************************************************
*/
#include <iostream>
using namespace std;
class Arithmetic
{
private:
int m_iVarA;
int m_iVarB;
public:
int add();
int sub();
int mul();
int div();
int mod();
public:
Arithmetic();
Arithmetic(int, int);
~Arithmetic();
};
int main(int argc, char* argv[])
{
int a = 4, b = 3;
int ret = 0;
Arithmetic arith(a, b);
ret = arith.add();
cout<<a<<" + "<<b<<" = "<<ret<<endl;
return 0;
}
Arithmetic::Arithmetic(int a, int b):m_iVarA(a),m_iVarB(b)
{}
Arithmetic::~Arithmetic()
{
m_iVarA = 0;
m_iVarB = 0;
}
int Arithmetic::add()
{
return (m_iVarA + m_iVarB);
}
2.1 用gcc编译
$ gcc -Wall -g -lstdc++ main.cpp -o gcc_compile_c++
$ ./gcc_compile_c
4 + 3 = 7
2.2 用g++编译
$ g++ -Wall -g main.cpp -o g++_compile_c++
$ ./g++_compile_++
4 + 3 = 7
3. 编译 C++代码中含有C语言的代码
代码main.cpp
/*!
******************************************************************************
* \File
* main.cpp
* \Brief
* C++ source code
* \Author
* Hank
******************************************************************************
*/
#include <iostream>
using namespace std;
class Arithmetic
{
private:
int m_iVarA;
int m_iVarB;
public:
int add();
int sub();
int mul();
int div();
int mod();
public:
Arithmetic();
Arithmetic(int, int);
~Arithmetic();
};
extern "C" int add(int, int);
int main(int argc, char* argv[])
{
int a = 4, b = 3;
int ret = 0;
Arithmetic arith(a, b);
ret = add(a, b);
cout<<a<<" + "<<b<<" = "<<ret<<endl;
return 0;
}
int add(int a, int b)
{
return (a + b);
}
Arithmetic::Arithmetic(int a, int b):m_iVarA(a),m_iVarB(b)
{}
Arithmetic::~Arithmetic()
{
m_iVarA = 0;
m_iVarB = 0;
}
int Arithmetic::add()
{
return (m_iVarA + m_iVarB);
}
3.1 用gcc编译
$ gcc -Wall -g -lstdc++ main.cpp -o gcc_compile_cINc++
$ ./gcc_compile_cINc++
4 + 3 = 7
3.2 用g++编译
$ gcc -Wall -g main.cpp -o g++_compile_cINc++
$ ./g++_compile_cINc++
4 + 3 = 7
二、各种库的编译链接方式示例详解
1. C语言代码链接调用C语言库
库代码文件:
/*!
******************************************************************************
* \File
* arith.h
******************************************************************************
*/
#ifndef __ARITH_H__
#define __ARITH_H__
int add(int a, int b);
#endif
/*!
******************************************************************************
* \File
* arith.c
******************************************************************************
*/
#include "arith.h"
int add(int a, int b)
{
return (a + b);
}
调用库的代码:
/*!
******************************************************************************
* \File
* main.c
* \Brief
* C codes
* \Author
* Hank
******************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "arith.h"
int main(int argc, char* argv[])
{
int a = 4;
int b = 3;
int ret = 0;
// 加载动态库
void *p_Handler = dlopen("./arith.so", RTLD_NOW);
if (!p_Handler)
{
printf("%s\n",dlerror());
exit(1);
}
// 引入接口函数
char *error;
typedef int (*arith_add)(int, int);
arith_add pf_add_interface;
pf_add_interface = (arith_add)dlsym(p_Handler, "add");
error = dlerror();
if (error)
{
printf("%s\n", error);
exit(1);
}
// 调用
ret = pf_add_interface(a,b);
printf(" %d + %d = %d\n",a,b,ret);
dlclose(p_Handler); // 关闭句柄
return 0;
}
1.1 gcc实现
生成库:
$ gcc -Wall -g -fPIC -o arith.so -shared arith.c
生成可执行文件:
$ gcc -Wall -g -rdynamic -ldl main.c -o compile_cLIBc
$ ./compile_cLIBc
3 + 4 = 7
1.2 g++实现
生成库:
$ g++ -Wall -g -fPIC -o arith.so -shared arith.c
生成可执行文件:
$ g++ -Wall -g -rdynamic -ldl main.c -o compile_cLIBc
$ ./compile_cLIBc
3 + 4 = 7
2. C++代码链接调用C++库
请参见文章《linux下C++动态链接C++库》详解
3. C++代码链接调用 C语言库
请参见文章《linux下C++动态链接C语言库》详解
gcc与g++的编译链接的示例详解的更多相关文章
- gcc/g++等编译器 编译原理: 预处理,编译,汇编,链接各步骤详解
摘自http://blog.csdn.net/elfprincexu/article/details/45043971 gcc/g++等编译器 编译原理: 预处理,编译,汇编,链接各步骤详解 C和C+ ...
- Spring Boot 2.x 快速入门(下)HelloWorld示例详解
上篇 Spring Boot 2.x 快速入门(上)HelloWorld示例 进行了Sprint Boot的快速入门,以实际的示例代码来练手,总比光看书要强很多嘛,最好的就是边看.边写.边记.边展示. ...
- python中的tcp示例详解
python中的tcp示例详解 目录 TCP简介 TCP介绍 TCP特点 TCP与UDP的不同点 udp通信模型 tcp客户端 tcp服务器 tcp注意点 TCP简介 TCP介绍 TCP协议 ...
- Python爬虫之爬取淘女郎照片示例详解
这篇文章主要介绍了Python爬虫之爬取淘女郎照片示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 本篇目标 抓取淘宝MM ...
- libCURL开源库在VS2010环境下编译安装,配置详解
libCURL开源库在VS2010环境下编译安装,配置详解 转自:http://my.oschina.net/u/1420791/blog/198247 http://blog.csdn.net/su ...
- jquery移除、绑定、触发元素事件使用示例详解
这篇文章主要介绍了jquery移除.绑定.触发元素事件使用示例详解,需要的朋友可以参考下. unbind(type [,data]) //data是要移除的函数 $('#btn').unbind(&q ...
- 史上最易懂——ReactNative分组列表SectionList使用详情及示例详解
React Native系列 <逻辑性最强的React Native环境搭建与调试> <ReactNative开发工具有这一篇足矣> <解决React Native un ...
- VS2010 Chart控件(一)Chart控件在ASP.NET网站中的应用示例详解(C#语言)
步骤如下: 1. Chart控件(一)Chart控件在ASP.NET网站中的应用示例详解(C#语言)" title="VS2010 Chart控件(一)Chart控件在ASP.NE ...
- socket编程的同步、异步与阻塞、非阻塞示例详解
socket编程的同步.异步与阻塞.非阻塞示例详解之一 分类: 架构设计与优化 简介图 1. 基本 Linux I/O 模型的简单矩阵 每个 I/O 模型都有自己的使用模式,它们对于特定的应用程序 ...
随机推荐
- 警惕使用WebClient.DownloadFile(string uri,string filePath)方法
原文:警惕使用WebClient.DownloadFile(string uri,string filePath)方法 WebClient.DownloadFile(string uri,string ...
- awk精简教材
awk就不多介绍了,最优秀的文本处理工具之一 一.内置变量表 属性 说明 $0 当前记录(作为单个变量) $1~$n 当前记录的第n个字段,字段间由FS分隔 FS 输入字段分隔符 默认是空格 NF 当 ...
- [转]Android与电脑局域网共享之:Samba Server
大家都有这样的经历,通过我的电脑或网上邻居访问另一台计算机上的共享资源,虽然电脑和手机之间可以有多种数据传输方式,但通过Windows SMB方式进行共享估计使用的人并不是太多,下面我就简单介绍一下, ...
- 根据首尾字节的tcp分包断包算法
这个算是我的一点小总结吧,放出来分享给大家,原来在网上找这种算法都找了N久没找到,自己写也是走了许多弯路,就放出来遛一遛吧 大家将就这个看看, 这是其中的一个主要的方法,其余的我就不放出来了,其中的I ...
- JavaScript闭包小窥
众所周知,JavaScript没有块级作用域,只有函数作用域.那就意味着定义在函数中的参数和变量在函数外部是不可见的,而在一个函数内部任何位置定义的变量,在该函数内部任何地方都可见.这带来的好处是内部 ...
- SignalR + KnockoutJS + ASP.NET MVC 实现井字游戏
SignalR + KnockoutJS + ASP.NET MVC 实现井字游戏 1.1.1 摘要 今天,我们将使用SignalR + KnockoutJS + ASP.NET MVC实现一个实 ...
- 对于vijos11.2模拟赛
特意起了个傻逼标题,只是想提醒一下自己以后不要犯逗(所以应该没有什么神犇点进来吧?) T1,T3 当场写的时候就觉得是不可写的,看了题解之后还是觉得不可写,人弱没办法.到了这个时候也懒得管这么难的东西 ...
- SugarSync的API总结
SugarSync API App支持SugarSync网盘的前提: 1.AccessKeyID:xxx 2.Private Access Key:xxx 3.AppID:xxx 详细的API总结如下 ...
- C++构造 下一个排列 的函数
今天围观刘汝佳神犇的白书发现了一个好用的函数: next_permutation(); 可以用于可重, 或者不可重集, 寻找下一个排列. 时间复杂度尚不明. //适用于不可重和可重集的排列. # in ...
- 【Apache ZooKeeper】命令行zkCli.sh使用指南
ZooKeeper命令行 原文 http://blog.csdn.net/ganglia/article/details/11606807 ZooKeeper客户端 ...