nanomsg.nng 在windows下的编译

  1. 需要 cmake 版本号大于等于3.1, 个人使用了Visual Studio 的 Native tools command prompt (分为x86, x64 分别用于Build各自构架的nng)
  2.  github源 https://github.com/nanomsg/nng 下载压缩包解压缩
  3. 进入文件夹按照指导输入:

mkdir buildx64
cd buildx64
cmake -G “Ninja” …
ninja
ninja test
ninja install (这一步不要也可, 因为第6步已经生成了相应的文件在buildx64目录中)

这里需要说明 按照上述的方法 build的结果是一个 nng.lib 这样一个static library, 不是 shared library (dll)
同时,你使用这个nng.lib 配合 nng.h 使用nng, 必然不能成功编译, 报错类似:
Error LNK2019 unresolved external symbol __imp__nng_setopt referenced in function
关于为什么会有 __imp 前缀请查阅其他文档。 这里发生错误是因为,nng.h 默认是配合 shared library (dll) 使用, 而不是 static library (lib)
但是默认的 nng编译却生出来 static library
查看nng.h 的源文件:

// NNG_DECL is used on declarations to deal with scope.
// For building Windows DLLs, it should be the appropriate __declspec().
// For shared libraries with platforms that support hidden visibility,
// it should evaluate to __attribute__((visibility("default"))).
#ifndef NNG_DECL
#if defined(_WIN32) && !defined(NNG_STATIC_LIB)
#if defined(NNG_SHARED_LIB)
#define NNG_DECL __declspec(dllexport)
#else
#define NNG_DECL __declspec(dllimport)
#endif // NNG_SHARED_LIB
#else
#if defined(NNG_SHARED_LIB) && defined(NNG_HIDDEN_VISIBILITY)
#define NNG_DECL __attribute__((visibility("default")))
#else
#define NNG_DECL extern
#endif
#endif // _WIN32 && !NNG_STATIC_LIB
#endif // NNG_DECL


注意到宏 NNG_STATIC_LIB 是控制到底使用 static library 还是 shared library 的开关
所以:
1.使用 static library, 必须定义 NNG_STATIC_LIB
2.使用 shared library, 必须定义 NNG_SHARED_LIB
顺便说一句,在我的测试中, NNG_STATIC_LIB 必须在工程中的预定义宏中设置,在其他地方实现无效。
这个问题 nng 上的一个 issue有讨论:
https://github.com/nanomsg/nng/issues/550

再回到最前面,我们如何在命令行中要求cmake 创建 shared library, 而不是 static library呢?

cmake -DBUILD_SHARED_LIBS=ON -G “Ninja” …

参数 BUILD_SHARED_LIBS 显示设为 ON
前缀 -D 表示定义
如此编译出的nng就是 shared lib (dll) 而不是 static lib (lib) 了。

以上我们讲解了如何编译 x86/x64 dll/lib nng的方法
希望对你有帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值