extern C浅析
1个无法解析的外部命令:意思是链接时链接不到外部函数或方法
在C++中函数可以发生重载,编译器会把函数名称统统改变
C++中想调用C语言方法:
1.
extern "C" void show();//show()方法按C语言方式做连接
2.
#ifdef __cplusplus//两个下划线_
extern "C"{
extern "C"
#endif//!__cplusplus
#include <stdio.h>
void show();
#ifdef __cplusplus//两个__下划线
}
#endif//!__cplusplus
解决的问题:在C++中调用C语言的函数
Test.h
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
void show();
#ifdef __cplusplus
}
#endif
Test.c
#include "Test.h"
void show() {
printf("show()--HelloWorld!\n");
}
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "Test.h"
//extern "C" void show();
int main() {
show();
system("pause");
return EXIT_SUCCESS;
}