zoukankan      html  css  js  c++  java
  • 写C/C++兼容的代码

    >> 使用extern "c":

    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

  • 相关阅读:
    strong,weak, retain, assign的区别@property的参数
    iOS 声明属性关键字讲解
    iOS中ARC和非ARC混用
    存储过程修改产品描述页图片alt描述信息
    mysql字符串函数(转载)
    读者证
    存储过程
    复制档案或目录 linux cp命令详解
    linux shell获取时间
    linux 备份日志文件
  • 原文地址:https://www.cnblogs.com/piaoger/p/2275304.html
Copyright © 2011-2022 走看看