マングリングとは、関数のシグニチャ(名前と型)をラベルに埋め込むこと(C++で同名の関数を識別するため)。
で、こいつがCのソースをC++コンパイラに解釈させるときにネックとなる。
例:エラーの出るコード
test.h
extern void hoge(void);
test.cpp
#include
#include "./test.h"
void hoge(void){
printf("hoge!n");
}
main.c
#include "./test.h"
int main(void){
hoge();
return 0;
}
具体的な回避方法は以下。
ケース1:C++ソースで定義した関数をCソースから呼ぶ
例のtest.hを
#ifdef __cplusplus extern "C" void hoge(void); #else extern void hoge(void); #endif
に変更するか、
例のtest.cppを
#include
extern ”C”{
#include ”test.h”
}
void hoge(void){
printf("hoge!n");
}
とする。
ケース2:Cソースで定義した関数をC++ソースから呼ぶ
test.h
extern void hoge(void);
test.c
#include
#include "./test.h"
void hoge(void){
printf("hoge!n");
}
main.cpp
extern "C"{
#include "./test.h"
}
int main(void){
hoge();
return 0;
}
コメントを残す