关于编写c++动态库常用的定义
1. 关于
- 1.1 最近一段时间,写了不少动态库,慢慢的也积累了东西。
- 1.2 之前一直做Windows的动态库,没有做过Linux和OS X的动态库,太缺乏经验了: 代码缺乏 编译器支持的判断、缺乏c++版本判断、缺乏操作系统的判断.... 总之,导致了很多问题。
2. Unicode和ANSI
这个,特别是 call Windows API 很明显,一个Windows API函数提供了 Unicode和ANSI的支持。添加下面的宏支持两种编码:
// 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个版本:CreateFileA 和 CreateFileW,其第一个参数是设备的名字,可以这样写:
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
调用函数CreateFile:CreateFile( 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++动态库常用的定义的更多相关文章
- Delphi调用C# 编写dll动态库
Delphi调用C# 编写dll动态库 编写C#dll的方法都一样,首先在vs2005中创建一个“类库”项目WZPayDll, using System.Runtime.InteropServices ...
- Linux 下Python调用C++编写的动态库
在工程中用到使用Python调用C++编写的动态库,结果报如下错误: OSError: ./extract_str.so: undefined symbol: _ZNSt8ios_base4InitD ...
- linux c++ 加载动态库常用的三种方法
链接库时的搜索路径顺序:LD_LIBRARY_PATH --> /etc/ld.so.conf --> /lib,/usr/lib 方法1. vi .bash_profile 设置环 ...
- makefile编写---.so动态库的生成和调用
http://blog.sina.com.cn/s/blog_559f6ffc0100fl3z.html 动静 http://blog.csdn.net/yuyunliuhen/article/de ...
- 【转】分析Linux和windows动态库
原文地址:http://www.cnblogs.com/chio/archive/2008/11/13/1333119.html 摘要:动态链接库技术实现和设计程序常用的技术,在Windows和Lin ...
- Linux和windows动态库
转载:http://www.cnblogs.com/chio/archive/2008/11/13/1333119.html 态链接库技术实现和设计程序常用的技术,在Windows和Linux系 统中 ...
- C++ 系列:静态库与动态库
转载自http://www.cnblogs.com/skynet/p/3372855.html 这次分享的宗旨是——让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择 ...
- C++静态库与动态库
C++静态库与动态库 这次分享的宗旨是--让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择.这里不深入介绍静态库.动态库的底层格式,内存布局等,有兴趣的同学,推荐一 ...
- c/c++:动态库 静态库 linux/windows 例子 (转)
作者:吴秦出处:http://www.cnblogs.com/skynet/本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名吴秦(包含链接). C++静 ...
随机推荐
- mysql-加密函数AES_DECRYPT函数
向user表插入数据age字段值为888,并用AES_DECRYPT函数进行加密,key为age(可以自己随意设置,记住就行) insert into user(name,sex,age) value ...
- shell 小数的比较大小问题
经过实验,if 语句中的数值判断是不可以比较小数大小的:-gt -ne 2. 直接用awk awk -v num1=6.6 -v num2=5.5 'BEGIN{print(num1>num ...
- SIG -MESH -1
协议栈 node:成为蓝牙mesh网络中一员的设备被称为节点(Node). 蓝牙mesh规格定义了节点可能拥有的特性.具有这些特性中的一个或多个,即表示节点可以在网络中扮演相应的特殊角色.定义的 ...
- Navicat连接Linux系统下的Mysql数据库
1 . 进入Linux机器 , 登录并进入mysql如果没有安装mysql,参照 https://blog.csdn.net/weixin_35353187/article/details/81712 ...
- 学习java的第十天
一.今日收获 1.java完全学习手册第二章2.9程序流程控制中的选择结构与顺序结构的例题 2.观看哔哩哔哩上的教学视频 二.今日问题 1.例题的问题不大,需要注意大小写,新的语句记忆不牢 2.哔哩哔 ...
- ORACLE dba_extents
dba_extents OWNER 拥有者 SEGMENT_NAME 段名 PARTITION_NAME 分区名 SEGMENT_TYPE 段类型 TABLESPACE_NAME 表空间名 EXTEN ...
- ORACLE 获取执行计划的方法
一.获取执行计划的6种方法(详细步骤已经在每个例子的开头注释部分说明了): 1. explain plan for获取: 2. set autotrace on : 3. statistics_lev ...
- vue-cli4脚手架搭建二
vue-cli4脚手架搭建一 vue create vue3 cd vue3 yarn serve http://localhost:8080/#/ main.js文件 import Vue from ...
- ArrayList删除特定元素的方法
最朴实的方法,使用下标的方式: ArrayList<String> al = new ArrayList<String>(); al.add("a"); a ...
- OpenStack之二: 安装OpenStack的yum源及相关组件
#: 在所有节点执行 [root@localhost ~]# yum install centos-release-openstack-stein -y #: 安装相关组件(只在管理端和计算几点安装) ...