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

C程序调用C++函数

这种需求应该就没C++程序调用C函数需求多了.目前的实现基本只有一类方法,即通过处理被调用的C++文件.

中给出的仍然是完整的,具体的,但又最基本最简单的实现,至于理论性的东西在网上很容易搜索的到.这里是针对调用C++的成员函数的实现.
aa.h
class AA {
    int i;
    public:
        int ThisIsTest(int a, int b); 
        float ThisIsTest(float a, float b);
};
extern "C" int ThisIsTest_C(void* s, int a,int b);
extern "C" float PolymorphicTest_C(void* s,float a, float b);
aa.cpp:
#include "aa.h"
int AA::ThisIsTest(int a, int b){
    return (a + b);
}
float AA::PolymorphicTest(float a, float b){
    return (a+b);
}
int ThisIsTest_C(void* s, int a,int b){
    AA* p = (AA*)s;
    return p->ThisIsTest(a,b);
}
float PolymorphicTest_C(void* s,float a, float b){
    AA* p = (AA*)s;
    return p->ThisIsTest(a,b);
}
a.h:
#ifndef __A_H
#define __A_H
int bar(void* s,int a, int b);
int bar_float(void* s,float a, float b);
#endif
a.c
#include <stdio.h>
#include "a.h"
extern int ThisIsTest_C(void* s, int a,int b);
extern float PolymorphicTest_C(void* s,float a, float b);
int bar(void* s,int a, int b) {
  printf("result=%d\n", ThisIsTest_C(s,a, b));
  return 0;
}
int bar_float(void* s,float a, float b) {
  printf("result=%f\n", PolymorphicTest_C(s,a, b));
  return 0;

main.c:
#include "a.h"
struct S {
  int i;
};
struct S s;
int main(int argc, char **argv){
  int a = 1;
  int b = 2;
  float c = 1.5;
  float d = 1.4;
  bar((void*)&s,a, b);
  bar_float((void*)&s,c,d);
  return(0);
}
Makefile:
all:
    gcc -Wall -c a.c -o a.o
    gcc -Wall -c aa.cpp -o aa.o
    gcc -Wall -c main.c -o main.o
 


相关文档:

Pro*C 中嵌入pl/sql块

/* 包含C头文件 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* 包含SQLCA头文件 */
EXEC SQL INCLUDE sqlca;
EXEC SQL INCLUDE sqlda;
int main()
{
    EXEC SQL BEGIN DECLARE SECTION;
    int  money;
    ......

分享几个经典C程序 浩瀚网络

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 
1.程序分析:利用while语句,条件为输入的字符不为'\n'. 
       
2.程序源代码: 
#include "stdio.h" 
main() 
{char c; 
 int letters=0,space=0,digit=0,others=0;  ......

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

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

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号