关于编写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++静 ...
随机推荐
- 一类利用队列优化的DP
I.导入: 这是一个\(O(n^2)\)的状态和转移方程: \[f(i,j)=\left\{ \begin{aligned} f(i-1,j-1)+k \ (1\leq j)\\ \max_{k \i ...
- [R] 如何在Linux命令行进行参数传入?
以前由于R命令行传参不友好,经常嵌套在其他程序语言(如Perl/Python)中来进行传参,但现在也陆续有一些方式来实现R的传参了,这里简单罗列下. 方法一 最传统的方法就是使用系统自带的comman ...
- GO 语言使用copy 拷贝切片的问题
使用copy,直接改变原片的值,而不是先创建一个副本.
- nginx_日志
192.168.31.250 - - [13/Nov/2019:08:38:07 +0800] "GET /aa HTTP/1.1" 404 571 "-" & ...
- Python三元表达式,列表推导式,字典生成式
目录 1. 三元表达式 2. 列表推导式 3. 字典生成式 3.1 字典生成式 3.2 zip()方法 1. 三元表达式 """ 条件成立时的返回值 if 条件 else ...
- Oracle-怎么在表的特定位置增加列
create table tmp as select ID,UserName,RealName,Sex,Tel,Addr from tabName;drop table tabName;rename ...
- 【原创】基于RPA的软件功能自动化测试
简介:1个功能自动化的框架 特点:OCR识别文字内容,pylackey对比图像相似度 代码极简 适用于绝大部分场景 只需要对按钮进行截图 配合第三方库可以生成漂亮的测试报告 文件结构:action-- ...
- 日常Java 2021/10/1
正则表达式 \cx匹配由x指明的控制字符.例如,lcM匹配一个Control-M或回车符.x的值必须为A-Z或a-z之一.否则,将c视为一个原义的'℃'字符.\f匹配--个换页符.等价于\xOc和\c ...
- Jenkins:参数化构建:分支|模块|回滚|打印日志
@ 目录 多分支 安装Git Parameter Plug-In 配置参数 选择构建分支 分模块 前提 分模块build 参数配置 分模块shell脚本 mvn 的基本用法 分模块运行 Jenkins ...
- Vue相关,vue.nextTick
vue中有一个较为特殊的API,nextTick.根据官方文档的解释,它可以在DOM更新完毕之后执行一个回调,用法如下: // 修改数据 vm.msg = 'Hello' // DOM 还没有更新 V ...