C++ 中 extern "C" 用法小结
2010-11-05 20:27
579 查看
在C++中 extern "C" 的两种用法
1. 如果要在C++程序中调用C语言写的函数, 在C++程序里边用 extern "C" 修饰要被调用的这个C程序,告诉C++编译器此函数是C语言写的,是C语言编译器生成的,调用他的时候请按照C语言习惯传递参数等。如:
//在C++程序里边声明该函数
extern "C" int strcmp(const char *s1, const char *s2);
也可以这样
//在C++程序里边声明该函数
extern "C" {
#include <string.h> //string.h里边包含了所要调用的函数的声明
}
2. 用C++语言写的一个函数,如果想让这个函数可以被其他C语言程序所用,则用extern "C" 来告诉C++编译器,请用C语言习惯来编译此函数。如:
//在C++程序里该函数
extern "C" int strcmp(const char *s1, const char *s2)
{
while (*s1 && *s1++ == *s2++) {
}
return *s1 - *s2;
}
1. 如果要在C++程序中调用C语言写的函数, 在C++程序里边用 extern "C" 修饰要被调用的这个C程序,告诉C++编译器此函数是C语言写的,是C语言编译器生成的,调用他的时候请按照C语言习惯传递参数等。如:
//在C++程序里边声明该函数
extern "C" int strcmp(const char *s1, const char *s2);
也可以这样
//在C++程序里边声明该函数
extern "C" {
#include <string.h> //string.h里边包含了所要调用的函数的声明
}
2. 用C++语言写的一个函数,如果想让这个函数可以被其他C语言程序所用,则用extern "C" 来告诉C++编译器,请用C语言习惯来编译此函数。如:
//在C++程序里该函数
extern "C" int strcmp(const char *s1, const char *s2)
{
while (*s1 && *s1++ == *s2++) {
}
return *s1 - *s2;
}
相关文章推荐
- C++ 中 extern "C" 用法小结
- C++ 中 extern "C" 用法小结
- C++ 中 extern "C" 用法小结C++ 中 extern "C" 用法小结(http://blog.csdn.net/playingguitar/archive/2006/08/15/1066127.aspx)
- C++中extern "C"用法
- C++ - extern "C"用法浅析
- c++中的 extern "C"用法
- C++ - extern "C"用法浅析
- C与C++的兼容问题——extern "C"的用法解析
- C++ - extern "C"用法浅析
- C++ 中引用C extern "C" 的用法
- C++中 extern "C" 的两种用法
- extern "C"的惯用法, C与C++的相互调用
- 深入理解C/C++混合编程(关于#ifdef __cplusplus extern "C" {...}的用法)
- C++中extern "C"的用法
- extern "C"的用法解析--C++中extern “C”含义深层探索
- C/C++混合编程 —— extern "C" 的用法
- C++ 中 extern "C" 用法小结
- c++中的 extern "C"
- C/C++混合编程——extern "C"
- extern "C"的用法解析