欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > windows >内容正文

windows

nanomsg.nng 在windows下的编译

发布时间:2024/1/1 windows 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 nanomsg.nng 在windows下的编译 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
  • 需要 cmake 版本号大于等于3.1, 个人使用了Visual Studio 的 Native tools command prompt (分为x86, x64 分别用于Build各自构架的nng)
  •  github源 https://github.com/nanomsg/nng 下载压缩包解压缩
  • 进入文件夹按照指导输入:
  • 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的方法
    希望对你有帮助。

    总结

    以上是生活随笔为你收集整理的nanomsg.nng 在windows下的编译的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。