要命啊,c++里调用c的模块。折磨我好几天了(分数不够再加)

zhangjianwen 2002-06-28 09:02:31
在vc++里,添加一c工程,此c工程编译成.lib文件,连接进vc++工程里,问题是现在我怎样调用c工程里的函数?
...全文
135 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
liugy 2002-06-28
  • 打赏
  • 举报
回复
extern "C"
BigLeo 2002-06-28
  • 打赏
  • 举报
回复
====解释=====================================================
由于要解决函数重载的问题,在编译函数据时C++与C有所不同,
如 int func1(int i,int j)在c++中会被译作func1_int_int
之类的名字以支持函数重载。所以在c++中调用c的函数时,函数
声明前需加为extern "c"。
====举例=====================================================
如c中函数为
int func1(int i);
则在c++中需声明为
extern "c" int func1(int i);

或是集中声明为:
extern "c"
{
int func1(int i);
... ;//其它声明
}
freelybird 2002-06-28
  • 打赏
  • 举报
回复
你又开贴问这个问题呀! 唉, 真是命苦的人呀
难道昨天你没看我回的贴吗?

我再详细重复一次.
第1步 切断speakfr项目与gsm所有的关联. 包括注释掉#include ".\gsm\debug\gsm.h"及delete关联项目gsm
第2步 project->setting link页category: input 的object\library modules: 指定.\gsm\debug\gsm.lib (当然release版应该是.\gsm\release\gsm.lib)
第3步 Compress.h做如下改动(在speakfr项目中注释掉所有#include ".\gsm\debug\gsm.h"语句.
第4步 先编译gsm, 再编译speakfr
第5步 完成. (更合理的做法还是我原来所说的. 工程gsm应该提供一个接口头文件)

// Compress.h: interface for the CCompress class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_COMPRESS_H__45BCA3C5_349A_43FF_9DA6_C2AEDBFA22B5__INCLUDED_)
#define AFX_COMPRESS_H__45BCA3C5_349A_43FF_9DA6_C2AEDBFA22B5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//#include ".\gsm\GSMapi.H"

#define int long
#define SASR
#define GSM_H
#define NeedFunctionPrototypes 1

typedef short word; /* 16 bit signed int */
typedef int longword; /* 32 bit signed int */

typedef unsigned short uword; /* unsigned word */
typedef unsigned int ulongword; /* unsigned longword */

struct gsm_state {

word dp0[ 280 ];

word z1; /* preprocessing.c, Offset_com. */
longword L_z2; /* Offset_com. */
int mp; /* Preemphasis */

word u[8]; /* short_term_aly_filter.c */
word LARpp[2][8]; /* */
word j; /* */

word nrp; /* 40 */ /* long_term.c, synthesis */
word v[9]; /* short_term.c, synthesis */
word msr; /* decoder.c, Postprocessing */

char verbose; /* only used if !NDEBUG */
char fast; /* only used if FAST */

};

// #ifndef GSM_H
//#define GSM_H
//#define NeedFunctionPrototypes 1

// #ifdef __cplusplus
// #define NeedFunctionPrototypes 1
// #endif

//#if __STDC__
//#define NeedFunctionPrototypes 1
//#endif

//#ifdef _NO_PROTO
//#undef NeedFunctionPrototypes
//#endif

//#undef GSM_P /* gnu stdio.h actually defines this... */

//#if NeedFunctionPrototypes
//#define GSM_P( protos ) protos
//#else
//# define GSM_P( protos ) ( /* protos */ )
//#endif

//#ifdef NeedFunctionPrototypes
//#include <stdio.h> /* for FILE * */
//#endif

/* AUTO_SPARC_HACK added by John Walker. If defined, USE_FLOAT_MUL
automatically set when compiling on a Sparc. This lets most
people avoid editing the Makefile. */

//#ifdef AUTO_SPARC_HACK
//#ifdef sparc
//#define USE_FLOAT_MUL
//#define FAST
//#endif
//#endif


/*
* Interface
*/
#define GSM_MAGIC 0xD /* 13 kbit/s RPE-LTP */

#define GSM_PATCHLEVEL 2
#define GSM_MINOR 0
#define GSM_MAJOR 1

#define GSM_OPT_VERBOSE 1
#define GSM_OPT_FAST 2

typedef struct gsm_state * gsm;
typedef short gsm_signal; /* signed 16 bit */
typedef unsigned char gsm_byte;
typedef gsm_byte gsm_frame[33]; /* 33 * 8 bits */


extern "C" gsm gsm_create (void);
extern "C" void gsm_destroy (gsm);

//extern void gsm_encode GSM_P((gsm, gsm_signal *, gsm_byte *));
//extern int gsm_decode GSM_P((gsm, gsm_byte *, gsm_signal *));
extern "C" void gsm_encode (gsm, gsm_signal *, gsm_byte *);
extern "C" int gsm_decode (gsm, gsm_byte *, gsm_signal *);


class CCompress
{
public:
CCompress();
virtual ~CCompress();
public:

gsm Gsm_create();
void Gsm_destroy(gsm TempGsm);

//int Gsm_print(FILE *, gsm, gsm_byte *);
//int Gsm_option(gsm, long, long *);

void Gsm_encode(gsm, gsm_signal *, gsm_byte *);
int Gsm_decode(gsm, gsm_byte *, gsm_signal *);

//int Gsm_explode(gsm, gsm_byte *, gsm_signal *);
//void Gsm_implode(gsm, gsm_signal *, gsm_byte *);

//#undef GSM_PGsm_explodeGsm_explodeGsm_explodeGsm_explodeGsm_explode
//#endif /* GSM_H */

};

#endif // !defined(AFX_COMPRESS_H__45BCA3C5_349A_43FF_9DA6_C2AEDBFA22B5__INCLUDED_)
Rafaelkl 2002-06-28
  • 打赏
  • 举报
回复
.lib本来就是一个二进制已经编译的库,只要能有正确的入口定义,并且让编译程序能找到这个.lib库,那么编译成exe的时候,它会自动找到call的位置的
nuaawyd 2002-06-28
  • 打赏
  • 举报
回复
#ifdef __cplusplus
extern "C" {
#endif

// 中间加你要引用的函数

#ifdef __cplusplus
}
#endif
sans 2002-06-28
  • 打赏
  • 举报
回复
extern CLIB_Func1(...);
extern CLIB_Func2(...);
extern CLIB_Func3(...);

....

在软件中调用:
CLIB_Func1(...);
...
CLIB_Func2(...);
...
sans 2002-06-28
  • 打赏
  • 举报
回复
extern CLIB_Func1(...);
extern CLIB_Func2(...);
extern CLIB_Func3(...);

....

在软件中调用:
CLIB_Func1(...);
...
CLIB_Func2(...);
...
Rafaelkl 2002-06-28
  • 打赏
  • 举报
回复
把.lib加入,代码中加一个include "XXX.h"
然后直接调用啊
sans 2002-06-28
  • 打赏
  • 举报
回复
extern CLIB_Func1(...);
extern CLIB_Func2(...);
extern CLIB_Func3(...);

....

在软件中调用:
CLIB_Func1(...);
...
CLIB_Func2(...);
...
chenkan2000 2002-06-28
  • 打赏
  • 举报
回复
和.lib文件一起应该有个头文件吧,把头文件include到你调用C函数的C++文件里。一定要用extern "C"{}把函数说明包起来。如果没有头文件,自己写一个。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧