一、编译方式的示例详解

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++的编译链接的示例详解的更多相关文章

  1. gcc/g++等编译器 编译原理: 预处理,编译,汇编,链接各步骤详解

    摘自http://blog.csdn.net/elfprincexu/article/details/45043971 gcc/g++等编译器 编译原理: 预处理,编译,汇编,链接各步骤详解 C和C+ ...

  2. Spring Boot 2.x 快速入门(下)HelloWorld示例详解

    上篇 Spring Boot 2.x 快速入门(上)HelloWorld示例 进行了Sprint Boot的快速入门,以实际的示例代码来练手,总比光看书要强很多嘛,最好的就是边看.边写.边记.边展示. ...

  3. python中的tcp示例详解

    python中的tcp示例详解  目录 TCP简介 TCP介绍 TCP特点 TCP与UDP的不同点 udp通信模型 tcp客户端 tcp服务器 tcp注意点   TCP简介   TCP介绍 TCP协议 ...

  4. Python爬虫之爬取淘女郎照片示例详解

    这篇文章主要介绍了Python爬虫之爬取淘女郎照片示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 本篇目标 抓取淘宝MM ...

  5. libCURL开源库在VS2010环境下编译安装,配置详解

    libCURL开源库在VS2010环境下编译安装,配置详解 转自:http://my.oschina.net/u/1420791/blog/198247 http://blog.csdn.net/su ...

  6. jquery移除、绑定、触发元素事件使用示例详解

    这篇文章主要介绍了jquery移除.绑定.触发元素事件使用示例详解,需要的朋友可以参考下. unbind(type [,data]) //data是要移除的函数 $('#btn').unbind(&q ...

  7. 史上最易懂——ReactNative分组列表SectionList使用详情及示例详解

    React Native系列 <逻辑性最强的React Native环境搭建与调试> <ReactNative开发工具有这一篇足矣> <解决React Native un ...

  8. VS2010 Chart控件(一)Chart控件在ASP.NET网站中的应用示例详解(C#语言)

    步骤如下: 1. Chart控件(一)Chart控件在ASP.NET网站中的应用示例详解(C#语言)" title="VS2010 Chart控件(一)Chart控件在ASP.NE ...

  9. socket编程的同步、异步与阻塞、非阻塞示例详解

     socket编程的同步.异步与阻塞.非阻塞示例详解之一  分类: 架构设计与优化 简介图 1. 基本 Linux I/O 模型的简单矩阵 每个 I/O 模型都有自己的使用模式,它们对于特定的应用程序 ...

随机推荐

  1. python学习笔记(五岁以下儿童)深深浅浅的副本复印件,文件和文件夹

    python学习笔记(五岁以下儿童) 深拷贝-浅拷贝 浅拷贝就是对引用的拷贝(仅仅拷贝父对象) 深拷贝就是对对象的资源拷贝 普通的复制,仅仅是添加了一个指向同一个地址空间的"标签" ...

  2. Serialization performance analysis

    Serialization performance analysis http://www.skyscanner.net/blogs/serialization-performance-analysi ...

  3. 萧墙HTML5手机发展之路(51)——jquerymobile在提高页面访问速度

    正在使用jQuery Mobile开发时间可以选择单页模板和多页模板,在单页模板时从一个页面跳转到另一个页面时从需要server要求.用户会感到轻微的停顿. 使用多页模板,为了改善网页之间跳跃的流畅, ...

  4. lib库实现loadrunner驱动mysql性能测试

    一.添加mysql驱动链接文件到loadrunner的bin和include目录下  以下链接为本人云盘分享,也可百度自行寻找下载源. http://yunpan.cn/cfTxbANSvipGi  ...

  5. W5500问题集锦(一)

    在"WIZnet杯"以太网技术竞赛中,有非常多參赛者在使用中对W5500有各种各样的疑问,对于这款WIZnet新推出的以太网芯片,使用中大家是不是也一样存在下面问题呢?来看一看: ...

  6. struts2注解redirect传递参数解决方案时,中国的垃圾问题

    struts2注解redirect传递参数解决方案时,中国的垃圾问题 试过很多方法  tomcat 编码  .字符串转换 .URLEncoder  .. 但是,没有解决方案,然后仔细阅读   stru ...

  7. Hbuilder jQuery 自定义代码块

    =begin 本文档是jquery代码块的编辑文件.修改其他的代码块,请在对应的激活代码助手点右下角的修改图标. HBuilder可使用ruby脚本来编辑代码块和增强操作命令. 1.编辑代码块 如果要 ...

  8. NHibernate 数据查询之QueryOver<T>

    NHibernate 数据查询之QueryOver<T>   一.限制运算符 Where:筛选序列中的项目WhereNot:反筛选序列中的项目 二.投影运算符 Select:创建部分序列的 ...

  9. C#使用Thrift简介,C#客户端和Java服务端相互交互

    C#使用Thrift简介,C#客户端和Java服务端相互交互 本文主要介绍两部分内容: C#中使用Thrift简介 用Java创建一个服务端,用C#创建一个客户端通过thrift与其交互. 用纯C#实 ...

  10. C注意,使用的语言字符串

    转载请注明出处! 在C语言没有具体的字符串数据类型,字符串的字符串常量和字符数组的形式. 实际上该字符串是零个或更多字符的字符串.并在整个位模式0NUL字节结束.因此,字符串所包括的字符内部不能出现N ...