C++如何调用C

来源:互联网 发布:淘宝充值店没有人买 编辑:程序博客网 时间:2024/04/30 04:45

C++程序调用C程序时,会出现编译错误,下面给出解决该问题的实例。

//MathFun.h

#ifndef MATHFUN_H#define MATHFUN_H#ifdef __cplusplusextern "C"{#endif#include <assert.h>/**函数名:matMult*功能:实现两矩阵相乘。*参数:*double fstMat[] - 存放矩阵A的元素*double sndMat[] - 存放矩阵B的元素*int fstRows - 矩阵A与乘积矩阵C的行数*int rowsCols - 矩阵A的列数,矩阵B的行数*int sndCols - 矩阵B与乘积矩阵C的列数*double destMat[] - 返回乘积矩阵C=A*B的元素*返回值:void*作者:LYC*日期:2013-07-04*/void matMult(double fstMat[], double sndMat[], int fstRows, int rowsCols, int sndCols, double destMat[]);#ifdef __cplusplus};#endif#endif

//MathFun.c文件(注意是.c文件)

#include "MathFun.h"void matMult(double fstMat[], double sndMat[], int fstRows, int rowsCols, int sndCols, double destMat[]){int i,j,t;for (i=0; i<fstRows; i++) //对应destMat的行{for (j=0; j<sndCols; j++) //对应destMat的列{for (t=0; t<rowsCols; t++) //求和{destMat[i*sndCols + j] = fstMat[i*rowsCols + t] * sndMat[t*sndCols + j];}}}}


//main.c文件(注意是.c文件)
#include "stdio.h"#include "MathFun.h"#include <malloc.h>#include <memory>void main(){int m = 1;int n = 2;int k = 1;double *pA = (double *)malloc(m * n * sizeof(double));pA[0] = 1;pA[1] = 2;double *pB = (double *)malloc(n * k * sizeof(double));pB[0] = 2;pB[1] = 1;double *pC = (double *)malloc(m * k * sizeof(double));memset(pC, 0, m * k * sizeof(double));matMult(pA, pB, m, n, k, pC);getchar();}



原创粉丝点击