易截截图软件、单文件、免安装、纯绿色、仅160KB

C++程序调用C函数

这种需求很多,又因为C++和C是两种完全不同的编译链接处理方式,所以要稍加处理.总结大致有两大类实现方法.
文中给出的是完整的,具体的,但又最基本最简单的实现,至于理论性的东西在网上很容易搜索的到.
一.通过处理被调用的C头文件
a.h:
#ifndef __A_H
#define __A_H
#ifdef __cplusplus
extern "C" {
#endif
int ThisIsTest(int a, int b);
#ifdef __cplusplus
}
#endif
#endif
a.c:
#include "a.h"
int ThisIsTest(int a, int b) {
  return (a + b);
}
aa.h:
class AA {
  public:
      int bar(int a, int b);
};
aa.cpp:
#include "a.h"
#include "aa.h"
#include "stdio.h"
int AA::bar(int a, int b){
    printf("result=%d\n", ThisIsTest(a, b));
    return 0;
}
main.cpp:
#include "aa.h"
int main(int argc, char **argv){
  int a = 1;
  int b = 2;
  AA* aa = new AA();
  aa->bar(a, b);
  delete(aa);
  return(0);
}
Makefile:
all:
        gcc -Wall -c a.c -o a.o
        gcc -Wall -c aa.cpp -o aa.o
        gcc -Wall -c main.cpp -o main.o
        g++ -o test *.o
二. 通过处理调用的C++文件
恢复a.h文件为一般性C头文件,在aa.cpp文件中extern包含a.h头文件或函数.
a.h:
#ifndef __A_H
#define __A_H
int ThisIsTest(int a, int b);
#endif
aa.cpp:
extern "C"
{
    #include "a.h"
}
#include "aa.h"
#include "stdio.h"
int AA::bar(int a, int b){
    printf("result=%d\n", ThisIsTest(a, b));
    return 0;
}
or
aa.cpp:
#include "aa.h"
#include "stdio.h"
extern "C"
{
    int ThisIsTest(int a, int b);
}
int AA::bar(int a, int b){
    printf("result=%d\n", ThisIsTest(a, b));
    return 0;
}


相关文档:

企业中感受C/S和B/S的异同

当今世界科学技术飞速发展,尤其以通信、计算机、网络为代表的互联网技术更是日新月异,令人眼花燎乱,目不睱接。由于计算机互联网在政治、经济、生活等各个领域的发展、运用以及网络的迅速普及和全社会对网络的依赖程度,计算机网络已经成为国家的经济基础和命脉,成为社会和经济发展强大动力,其地位越来越重要。但 ......

如何学习linux下的c/c++编程

我计划的学习历程:
1. c和c++的基础知识,这个和操作系统无关,在windows上也可以学。c语言语法相对简单一点,c++不需要把所有的知识点都学会,要知道基本的封装,继承,多态之类的,还有STL。更深入的学习应该是在以后的工作中遇到问题了再去查资料。
2. 看书《APUE》,做书上的习题,掌握文件,系统调用,线程、进程、 ......

realview MDK C/C++ 混合编程问题(续一)

    前几天写了关于Keil与C/C++混合编程的若干问题,今天写的是继续前面的。
   
    在使用C/C++混合编程的时候,程序运行到类里面的虚拟函数时死掉,查看其vptr值为0x00000000,这时候不知道怎么解决,后来自己编了一个实例程序可以通过,发现vptr的值是在__main里面的& ......

使你的C/C++代码支持Unicode

本文摘自I18nGuy
主页的一篇内容,原文地址:http://www.i18nguy.com/unicode/c-unicode.zh-CN.html
这份文档简要的说明了如何修改你的C/C++代码使之支持Unicode。在这里并不准备
解释太多相关的技术细节并且我得假定你已经基本熟悉Microsoft支持Unicode的方式。
它的主要目的是方便你查询相关的数据类型和函数,以及修 ......

Facts of C Programming Language

Facts of C Programming Language
C的一些掌故
(英文原文:http://www.programmingfacts.com/2009/12/01/facts-of-c-programming-language/)
C programming language was developed in 1972 by Dennis Ritchie and Brian Kernighan at the Bell Telephone Laboratories (AT&T Bell Laboratories) for use with the Un ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号