c 调用c++库
1. 工程结构为: lib + testdriver
2. c++的lib库工程中:
hello.cpp 中:
//hello.cpp
#ifndef _cplusplus
#define _cplusplus
#include "hello.h"
#endif
void hello()
{
printf("hello!\n");
}
hello.h中:
//hello.h中加入
#ifndef _HELLO_H_
#define _HELLO_H_
#ifdef _cplusplus
extern "C"
{
#endif
#define DECLSPEC __declspec(dllexport)
extern DECLSPEC void hello();
#ifdef _cplusplus
}
#endif
#endif
通过用VS编译生成.lib/.dll后,将库文件和hello.h头文件拷贝到testdriver工程中;
3.testdriver工程中用.c调用
testdriver.h中:
#include "hello.h"
testdriver.c中:
#include "testdriver.h"
// 直接调用
hello();
完成了c 调用 c++库