c++中想调用c语言方法
在头文件test.h中有如下c代码
#pragma once //防止头文件重复编译
#include <stdio.h>
void show();
...
...
在源文件中有如下c++代码
#include<iostream>
using namespace std;
#include "test.h"
int main(){
show();
//在c++中函数是可以发生重载,编译器会把这个函数名称偷偷改变,例如_showv();c语言中没有函数重载,所有,链接不到头文件中的show()方法;所有编译器产生error:1个无法解析的外部命令
}
- 方法一
在c++中加入如下代码
extern "C" void show();//show 方法按照C语言方式做链接
int main()
...
...
- 方法二
在c文件中加入如下代码
#pragma once //防止头文件重复编译
#ifdef __cplusplus
extern "C"{
#endif
#include <stdio.h>
void show1();
void show2();
#ifdef __cplusplus
}
#endif