1. 关于

  • 1.1 最近一段时间,写了不少动态库,慢慢的也积累了东西。
  • 1.2 之前一直做Windows的动态库,没有做过Linux和OS X的动态库,太缺乏经验了: 代码缺乏 编译器支持的判断、缺乏c++版本判断、缺乏操作系统的判断.... 总之,导致了很多问题。

2. Unicode和ANSI

这个,特别是 call Windows API 很明显,一个Windows API函数提供了 UnicodeANSI的支持。添加下面的宏支持两种编码:

// to
// c/c++ run time library
#ifdef _UNICODE
#ifndef UNICODE
#define UNICODE
#endif
#endif
// windows
#ifdef UNICODE
#ifndef _UNICODE
#define _UNICODE
#endif
#endif

一个例子,演示需要调用函数CreateFile,它有2个版本:CreateFileACreateFileW,其第一个参数是设备的名字,可以这样写:

	TCHAR *tc_com_name = nullptr;
#ifdef UNICODE
std::wstring wstr = str2wstr(spp._name);
tc_com_name = const_cast<TCHAR *>(wstr.c_str());
#else
tc_com_name = const_cast<TCHAR*>(spp._name.c_str());
#endif // !UNICODE

调用函数CreateFileCreateFile( tc_com_name, ...) 就好啦。

3. __cplusplus

通常使用这个宏判断c++的版本,但是, Visual Studio X (X = 2003, 2005, 2008 , 2010... 下面简称VS)编译器中,这个默认值一直都是:199711L。官网也说了,目前仅支持VS2017(version 15.7)以上版本可以添加命令修改, 具体的可以看这里

使用__cplusplus判断c++版本示例

#if __cplusplus >= 201103L
#define has_cxx_11
#endif //

4. 动态库导出符

根据操作系统的不同,设置对应的宏(一个例子)

// to definite an export flag
#if defined(_WIN32) || defined(_WIN64) || defined(WIN32)
//----------------------------------------------------------------------
#ifndef _lib_sp_api_
#define _lib_sp_api_ __declspec(dllexport)
#else
#define _lib_sp_api_ __declspec(dllimport)
#endif /// !_lib_pipe_api_ #elif defined(_unix_) || defined(_linux_) || defined(_unix) || defined(_linux) || #elif defined(__APPLE__)
//---------------------------------------------------------------------- #ifndef _lib_sp_api_
#define _lib_sp_api_ __attribute__((visibility ("default")))
#endif /// !_lib_pipe_api_ #endif /// !

5. 编译器判断

可能你需要根据编译器的不同执行某些代码,下面的代码可以帮到你,一个例子

#if defined(__clang__) || defined(__GNUC__)
// clang or gcc(++)
#elif defined(_MSC_VER) // use vs compiler
#if 1900 <= _MSC_VER // 1900 = vs2015
#ifndef has_cxx_11
#define has_cxx_11
#endif //
#endif #endif

6. 操作系统的判断

可能你还需要对操作系统的判断,比如编写串口通信时,需要call系统api完成相关操作,下面的代码可以帮到你。 一个例子

#if defined(_WIN32) || defined(_WIN64)

	# ifndef os_is_win
#define os_is_win
#else
#endif /// os_is_win #elif defined(_linux) || defined(_linux_) || defined() || defined (_unix_) # ifndef os_is_linux
#define os_is_linux
#else
#endif /// os_is_linux #elif defined(__APPLE__)
#ifndef os_is_apple
#define os_is_apple
#else
#endif /// os_is_apple #endif //

7.附完整版

copy即可使用

// c/c++ run time library
#ifdef _UNICODE
#ifndef UNICODE
#define UNICODE
#endif
#endif // windows
#ifdef UNICODE
#ifndef _UNICODE
#define _UNICODE
#endif
#endif // cpp_version to check if it suppports c++11
//---------------------------------------------------------------------------------------------
#if __cplusplus >= 201103L
#define use_cpp_11
#endif // ! __cplusplus >= 201103L // compiler
//---------------------------------------------------------------------------------------------
#if defined(__clang__) || defined(__GNUC__)
#define compiler_is_clang
// clang or gcc(++)
#elif defined(_MSC_VER) // use vs compiler #ifndef compiler_is_vs
#define compiler_is_vs
#endif //! compiler_is_vs
//-------------------------------------------- #if 1900 <= _MSC_VER // 1900 = vs2015 #ifndef use_cpp_11
#define use_cpp_11
#endif // #endif #endif //! defined(__clang__) || defined(__GNUC__) // os
//---------------------------------------------------------------------------------------------
#if defined(_WIN32) || defined(_WIN64) #ifndef os_is_win
#define os_is_win
#else
#endif // os_is_win #elif defined(_linux) || defined(_linux_) || defined(__linux) || defined (_unix_) #ifndef os_is_linux
#define os_is_linux
#else
#endif /// os_is_linux #elif defined(__APPLE__)
#ifndef os_is_osx
#define os_is_osx
#else
#endif /// os_is_osx #endif // !defined(_linux) || defined(_linux_) || defined(__linux) || defined (_unix_) // -----------------------------------------------------------------------------------------
#if defined(os_is_win) #ifndef _oct_udp_api_export_export_
#define _lib_udp_api_export_ __declspec(dllexport)
#else
#define _lib_udp_api_export_ __declspec(dllimport)
#endif //! _oct_udp_api_export_export_ // -----------------------------------------------------------------------------------------
#elif defined(os_is_linux) || defined(os_is_osx) #ifndef _oct_udp_api_export_export_
#define _lib_udp_api_export_ __attribute__((visibility("default")))
//#define _oct_udp_api_export_ __attribute__ ((visibility("default")))
#endif // !_oct_udp_api_export_export_ #endif // !os_is_win

关于编写c++动态库常用的定义的更多相关文章

  1. Delphi调用C# 编写dll动态库

    Delphi调用C# 编写dll动态库 编写C#dll的方法都一样,首先在vs2005中创建一个“类库”项目WZPayDll, using System.Runtime.InteropServices ...

  2. Linux 下Python调用C++编写的动态库

    在工程中用到使用Python调用C++编写的动态库,结果报如下错误: OSError: ./extract_str.so: undefined symbol: _ZNSt8ios_base4InitD ...

  3. linux c++ 加载动态库常用的三种方法

    链接库时的搜索路径顺序:LD_LIBRARY_PATH --> /etc/ld.so.conf --> /lib,/usr/lib 方法1. vi .bash_profile    设置环 ...

  4. makefile编写---.so动态库的生成和调用

    http://blog.sina.com.cn/s/blog_559f6ffc0100fl3z.html  动静 http://blog.csdn.net/yuyunliuhen/article/de ...

  5. 【转】分析Linux和windows动态库

    原文地址:http://www.cnblogs.com/chio/archive/2008/11/13/1333119.html 摘要:动态链接库技术实现和设计程序常用的技术,在Windows和Lin ...

  6. Linux和windows动态库

    转载:http://www.cnblogs.com/chio/archive/2008/11/13/1333119.html 态链接库技术实现和设计程序常用的技术,在Windows和Linux系 统中 ...

  7. C++ 系列:静态库与动态库

    转载自http://www.cnblogs.com/skynet/p/3372855.html 这次分享的宗旨是——让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择 ...

  8. C++静态库与动态库

    C++静态库与动态库 这次分享的宗旨是--让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择.这里不深入介绍静态库.动态库的底层格式,内存布局等,有兴趣的同学,推荐一 ...

  9. c/c++:动态库 静态库 linux/windows 例子 (转)

    作者:吴秦出处:http://www.cnblogs.com/skynet/本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名吴秦(包含链接). C++静 ...

随机推荐

  1. jquery操作html中图片宽高自适应

    在网站制作中如果后台上传的图片不做宽高限制,在前台显示的时候,经常会出现图片变形,实用下面方法可以让图片根据宽高自适应,不论是长图片或者高图片都可以完美显示. $("#myTab0_Cont ...

  2. CMSIS-RTOS 信号量Semaphores

    信号量Semaphores 和信号类似,信号量也是一种同步多个线程的方式,简单来讲,信号量就是装有一些令牌的容器.当一个线程在执行过程中,就可能遇到一个系统调用来获取信号量令牌,如果这个信号量包含多个 ...

  3. 添加页面、页面交互、动态添加页面tab

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ViewDictTosPr ...

  4. c#Gridview添加颜色

    e.Row.Cells[1].ForeColor = System.Drawing.Color.Blue;

  5. Navicat连接Linux系统下的Mysql数据库

    1 . 进入Linux机器 , 登录并进入mysql如果没有安装mysql,参照 https://blog.csdn.net/weixin_35353187/article/details/81712 ...

  6. Flink(三)【核心编程】

    目录 一.Environment 二.Source 从集合读取数据 从文件读取数据 从kakfa读取数据(常用) 自定义数据源 三.Transform map Rich版本函数 flatMap key ...

  7. LR中的快捷建

    Ctrl+F  弹出搜索对话框 CTRL+F8  弹出view tree 界面 (寻找关联) 觉得不错的可关注微信公众号在手机上观看,让你用手机边玩边看

  8. springmvc中的异常处理方法

    //1.自定义异常处理类       2.编写异常处理器    3.配置异常处理器 package com.hope.exception;/** * 异常处理类 * @author newcityma ...

  9. Appium获取toast消息(二)

    刚接触appium进行移动端设备的UI自动化,在遇到toast消息的时候很是苦恼了一阵,最后通过强大的搜索引擎找到了个相对解决方法,废话不多说,直接贴代码↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ...

  10. 什么是maven(二)

    转自博主--一杯凉茶 maven项目构建ssh工程(父工程与子模块的拆分与聚合)   前一节我们明白了maven是个什么玩意,这一节就来讲讲他的一个重要的应用场景,也就是通过maven将一个ssh项目 ...