c++從入門到精通——extern C小結
extern "C"的主要作用就是爲了實現c++代碼能夠調用其他c語言代碼。加上extern "C"後,這部分代碼編譯器按c語言的方式進行編譯和鏈接,而不是按c++的方式。
test.h
#pragma once
#ifdef __cplusplus //兩個_下劃線
extern "C" {
#endif // !__cplusplus
#include <stdio.h>
void show();
#ifdef __cplusplus //兩個_下劃線
}
#endif // !__cplusplus
test.c
#include "test.h"
void show()
{
printf("hello world \n");
}
extern C淺析.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "test.h"
//C++中想調用C語言方法
//extern "C" void show(); //show方法 按照C語言方式做連接
//解決的問題就是 在C++中調用C語言的函數
int main(){
show(); //在C++中 函數是可以發生重載的,編譯器會把這個函數名稱偷偷改變 _showv void
system("pause");
return EXIT_SUCCESS;
}