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
 
相关文档:
在ANSI C中,对文件的操作分为两种方式,即流式文件操作和I/O文件操作,下面就分别介绍之。
一、流式文件操作
这种方式的文件操作有一个重要的结构FILE,FILE在stdio.h中定义如下:
typedef struct {
int level; /* fill/empty level of buffer */
unsigned flags; /* File status flags */
char fd; /* File des ......
C语言中可变参数的用法
我们在C语言编程中会遇到一些参数个数可变的函数,例如printf()
这个函数,它的定义是这样的:
int printf( const char* format, ...);
它除了有一个参数format固定以外,后面跟的参数的个数和类型是
可变的,例如我们可以有以下不同的调用方法:
printf("%d",i);
printf("%s",s);
printf( ......
本文摘自I18nGuy
主页的一篇内容,原文地址:http://www.i18nguy.com/unicode/c-unicode.zh-CN.html
这份文档简要的说明了如何修改你的C/C++代码使之支持Unicode。在这里并不准备
解释太多相关的技术细节并且我得假定你已经基本熟悉Microsoft支持Unicode的方式。
它的主要目的是方便你查询相关的数据类型和函数,以及修 ......
这种需求很多,又因为C++和C是两种完全不同的编译链接处理方式,所以要稍加处理.总结大致有两大类实现方法.
文中给出的是完整的,具体的,但又最基本最简单的实现,至于理论性的东西在网上很容易搜索的到.
一.通过处理被调用的C头文件
a.h:
#ifndef __A_H
#define __A_H
#ifdef __cplusplus
extern "C" {
#endif
int Th ......