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 模型都有自己的使用模式,它们对于特定的应用程序 ...
随机推荐
- C# 字符串加密解密函数
原文:C# 字符串加密解密函数 using System; using System.Text;using System.Security.Cryptography; using System.IO; ...
- MVC+Bootstrap设计
MVC+Bootstrap) 二 框架设计 文章目录: 一.前言 二.结构图 三.项目搭建 四.代码生成 五.实现接口 六.依赖倒置 七.登录实现 八.最后 一.前言 这个框架是从最近几年做过的项目中 ...
- Java 之复合赋值运算符
1.引入问题 切入正题,看下面代码,结果应该是怎么样的 public class App{ public static void main( String[] args ){ byte a=1 ; i ...
- Spring IOC 之个性化定制the nature of a bean
1.生命周期回调 为了影响容器管理的bean的生命周期,你可以实现Spring的InitializingBean和DisposableBean接口.容器首先调用afterPropertiesSet() ...
- OCP-1Z0-051-题目解析-第3题
3. You need to extract details of those products in the SALES table where the PROD_ID columncontains ...
- ASP.NET DataTable的操作大全
DataTable表示一个与内存有关的数据表,可以使用工具栏里面的控件拖放来创建和使用,也可以在编写程序过程中根据需要独立创建和使用,最常见的情况是作为DataSet的成员使用,在这种情况下就需要用在 ...
- Eclipse在SVN安装步骤(两种)和使用方法
一.至Eclipse安装SVN,最常见的两种方式:手动模式,使用安装向导.具体操作步骤如下: 单程:手动安装 1.官方网站下载,从site-1.6.9.zip文件,网址是:subclipse.tigr ...
- SignalR + KnockoutJS + ASP.NET MVC 实现井字游戏
SignalR + KnockoutJS + ASP.NET MVC 实现井字游戏 1.1.1 摘要 今天,我们将使用SignalR + KnockoutJS + ASP.NET MVC实现一个实 ...
- Using Ninject in a Web Application
http://aidenweb.co.uk/?p=15 Using Ninject in a Web Application I have been meaning to look at Ninjec ...
- Linq无聊练习系列2--select/distinct练习
void dataBindByWhere() { /**************select/distinct 练习*******************/ //获 ...