写C/C++兼容的代码
2011-12-04 11:49
302 查看
>> 使用extern "c":
C和C++对函数的处理方式并不相同。
利用extern "C", C++能够知道该函数是C链接。因而,如果要对编译器提示使用C的方式来处理函数的话,那么就要使用extern "C"来说明.
C++编译器开发商已经对C 标准库的头文件作了extern“C”处理,所以 我们可以用#include 来直接引用这些头文件。
Implementatioin:
>> structure
在C++中,为Structure加上构造函数。
- Piaoger
C和C++对函数的处理方式并不相同。
利用extern "C", C++能够知道该函数是C链接。因而,如果要对编译器提示使用C的方式来处理函数的话,那么就要使用extern "C"来说明.
C++编译器开发商已经对C 标准库的头文件作了extern“C”处理,所以 我们可以用#include 来直接引用这些头文件。
/* Raw_C_Api.h*/ #define DLL_EXPORT #ifdef DLL_EXPORT # define DLL_API _declspec(dllexport) /* Make sure we can call this stuff from C++.*/ # ifdef __cplusplus extern "C" { # endif #else # define DLL_API _declspec(dllimport) #endif /* C/C++ compatible interface*/ void DLL_API doSomethingWithCPP(char* myChar); #ifdef DLL_EXPORT # ifdef __cplusplus } /* end of the 'extern "C"' block */ # endif #endif
Implementatioin:
/* Raw_C_Api_ImplByCPP.cpp*/ #include "Raw_C_Api.h" #include <string> /* Process raw C String with std::string*/ void doSomethingWithCPP(char* myChar) { std::string myString(myChar); /* TODO: Do More things on myStrig */ }
>> structure
在C++中,为Structure加上构造函数。
typedef struct _coord_2d { float x; float y; /* define constructors for c++.*/ #if defined(__cplusplus) inline _coord_2d() {} inline _coord_2d(const _coord_2d &v) : x(v.x), y(v.y) {} inline _coord_2d(float x, float y) : x(x), y(y) {} #endif } Coord2d;
- Piaoger
相关文章推荐
- 如何让C/C++代码同时兼容Python2和Python3
- 使得C/C++代码兼容支持OC代码的方法
- 如何让C/C++代码同时兼容Python2和Python3
- C++ Primer 学习笔记_15_新旧代码兼容与多维数组
- VS2012 不兼容vs2010 C++代码,提示说fopen unsafe的解决方法
- C++ 新旧代码的兼容
- C++新旧代码的兼容
- 转载一份C++线程池的代码,非常实用(转)
- Symbian下用C++实现网页浏览的代码
- 终于看到了风格优良的C++代码
- C++的精髓——代码复用、接口复用
- C++ 练习代码
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
- JavaScript 事件监听实例代码[兼容IE,firefox] 含注释
- 在C++中使用sqlite的示例(从下载、编译、工程建立、配置、测试代码编写全方位介绍)
- 感染EXE文件代码(C++)
- C++ - "priority_queue" 优先级队列 简介 及 代码
- lintcode整数排序的完整c++代码
- 球与箱子-c++代码实现及运行实例结果
- C++模版 代码文件放置问题