Home cpp extern
Post
Cancel

cpp extern

Example

1
2
3
4
5
6
7
8
9
10
11
12
extern "C" void foo() {

}

#ifdef __cplusplus
extern "C" {
#endif //__cplusplus  
  	void foo();
  	void bar();
#ifdef __cplusplus
}
#endif //__cplusplus

Note

  • 用extern “C” 修饰的代码,编译器将按C的方式来编译
  • 修饰声明(declaretion)而不是实现(definition)
  • 用在C++调用来自C的函数这种场景,实现C++/C混编
  • C语言不认识extern “C”
  • CPP宏:__cplusplus

References

This post is licensed under CC BY 4.0 by the author.