【Qt 学习之路】使用 cmake 在Windows上 编译 ZeroMQ

文章目录

1、概述

前几篇文章讲述了ZeroMQ是什么、网络通信,和如何在Qt上使用现成的ZeroMQ的库,本篇文章中主要讲述如何使用 cmake在 Windows上编译ZeroMQ。下载和安装这里不讲了,在之前的文章中讲述的已经很详细了,想了解的可以看以下相关资料。

相关资料:

1、【Qt 学习之路】在 Qt 使用 ZeroMQ:链接地址

2、【网络编程】ZeroMQ的网络通信:链接地址

2、编译

2.1、用 Visual Studio 的解决方案方式

2.1.1、找到 Builds 文件夹

在 ZeroMQ 的 builds 文件夹下,有很多种编译方法,为了方便演示,本文选择msvc进行演示。

2.1.2、查看 deprecated-msvc 下的 libzmq.sln 文件

2.1.3、使用 Visual Studio 打开 libzmq.sln 解决方案

2.1.4、修改 libzmq.import.props 文件

用 Notepad++ 打开 libzmq.import.props 文件,修改 5处 路径,全部改成...\bin【注意:去掉"...\libzmq\"】

  • 原:
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup Label="Globals">
    <_PropertySheetDisplayName>ZMQ Import Settings</_PropertySheetDisplayName>
  </PropertyGroup>

  <!-- User Interface -->

  <ItemGroup Label="BuildOptionsExtension">
    <PropertyPageSchema Include="$(MSBuildThisFileDirectory)libzmq.import.xml" />
  </ItemGroup>

  <!-- Configuration -->

  <ItemDefinitionGroup>
    <ClCompile>
      <PreprocessorDefinitions Condition="'$(Option-sodium)' == 'true'">ZMQ_USE_LIBSODIUM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <PreprocessorDefinitions Condition="'$(Option-openpgm)' == 'true'">ZMQ_HAVE_OPENPGM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <PreprocessorDefinitions Condition="'$(Option-gssapi)' == 'true'">HAVE_LIBGSSAPI_KRB5;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <PreprocessorDefinitions Condition="'$(Option-draftapi)' == 'true'">ZMQ_BUILD_DRAFT_API;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
  </ItemDefinitionGroup>

  <!-- Linkage -->

  <ItemDefinitionGroup>
    <ClCompile>
      <AdditionalIncludeDirectories>$(ProjectDir)..\..\..\..\..\libzmq\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
      <PreprocessorDefinitions Condition="'$(Linkage-libzmq)' == 'static' Or '$(Linkage-libzmq)' == 'ltcg'">ZMQ_STATIC;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
    <Link>
      <AdditionalDependencies Condition="'$(Linkage-libzmq)' != ''">libzmq.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <AdditionalLibraryDirectories Condition="$(Configuration.IndexOf('Debug')) != -1">$(ProjectDir)..\..\..\..\..\libzmq\bin\$(PlatformName)\Debug\$(PlatformToolset)\$(Linkage-libzmq)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
      <AdditionalLibraryDirectories Condition="$(Configuration.IndexOf('Release')) != -1">$(ProjectDir)..\..\..\..\..\libzmq\bin\$(PlatformName)\Release\$(PlatformToolset)\$(Linkage-libzmq)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Link>
  </ItemDefinitionGroup>

  <!-- Copy -->

  <Target Name="Linkage-libzmq-dynamic" AfterTargets="AfterBuild" Condition="'$(Linkage-libzmq)' == 'dynamic'">
    <Copy Condition="$(Configuration.IndexOf('Debug')) != -1"
          SourceFiles="$(ProjectDir)..\..\..\..\..\libzmq\bin\$(PlatformName)\Debug\$(PlatformToolset)\dynamic\libzmq.dll"
          DestinationFiles="$(TargetDir)libzmq.dll"
          SkipUnchangedFiles="true" />
    <Copy Condition="$(Configuration.IndexOf('Debug')) != -1"
          SourceFiles="$(ProjectDir)..\..\..\..\..\libzmq\bin\$(PlatformName)\Debug\$(PlatformToolset)\dynamic\libzmq.pdb"
          DestinationFiles="$(TargetDir)libzmq.pdb"
          SkipUnchangedFiles="true" />
    <Copy Condition="$(Configuration.IndexOf('Release')) != -1"
          SourceFiles="$(ProjectDir)..\..\..\..\..\libzmq\bin\$(PlatformName)\Release\$(PlatformToolset)\dynamic\libzmq.dll"
          DestinationFiles="$(TargetDir)libzmq.dll"
          SkipUnchangedFiles="true" />
  </Target>

  <!-- Messages -->

  <Target Name="libzmq-info" BeforeTargets="AfterBuild" Condition="'$(Linkage-libzmq)' == 'dynamic'">
    <Message Text="Copying libzmq.dll -&gt; $(TargetDir)libzmq.dll" Importance="high"/>
    <Message Text="Copying libzmq.pdb -&gt; $(TargetDir)libzmq.pdb" Importance="high" Condition="$(Configuration.IndexOf('Debug')) != -1" />
  </Target>

</Project>
  • 改:
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup Label="Globals">
    <_PropertySheetDisplayName>ZMQ Import Settings</_PropertySheetDisplayName>
  </PropertyGroup>

  <!-- User Interface -->

  <ItemGroup Label="BuildOptionsExtension">
    <PropertyPageSchema Include="$(MSBuildThisFileDirectory)libzmq.import.xml" />
  </ItemGroup>

  <!-- Configuration -->

  <ItemDefinitionGroup>
    <ClCompile>
      <PreprocessorDefinitions Condition="'$(Option-sodium)' == 'true'">ZMQ_USE_LIBSODIUM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <PreprocessorDefinitions Condition="'$(Option-openpgm)' == 'true'">ZMQ_HAVE_OPENPGM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <PreprocessorDefinitions Condition="'$(Option-gssapi)' == 'true'">HAVE_LIBGSSAPI_KRB5;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <PreprocessorDefinitions Condition="'$(Option-draftapi)' == 'true'">ZMQ_BUILD_DRAFT_API;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
  </ItemDefinitionGroup>

  <!-- Linkage -->

  <ItemDefinitionGroup>
    <ClCompile>
      <AdditionalIncludeDirectories>$(ProjectDir)..\..\..\..\..\libzmq\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
      <PreprocessorDefinitions Condition="'$(Linkage-libzmq)' == 'static' Or '$(Linkage-libzmq)' == 'ltcg'">ZMQ_STATIC;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
    <Link>
      <AdditionalDependencies Condition="'$(Linkage-libzmq)' != ''">libzmq.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <AdditionalLibraryDirectories Condition="$(Configuration.IndexOf('Debug')) != -1">$(ProjectDir)..\..\..\..\bin\$(PlatformName)\Debug\$(PlatformToolset)\$(Linkage-libzmq)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
      <AdditionalLibraryDirectories Condition="$(Configuration.IndexOf('Release')) != -1">$(ProjectDir)..\..\..\..\bin\$(PlatformName)\Release\$(PlatformToolset)\$(Linkage-libzmq)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Link>
  </ItemDefinitionGroup>

  <!-- Copy -->

  <Target Name="Linkage-libzmq-dynamic" AfterTargets="AfterBuild" Condition="'$(Linkage-libzmq)' == 'dynamic'">
    <Copy Condition="$(Configuration.IndexOf('Debug')) != -1"
          SourceFiles="$(ProjectDir)..\..\..\..\bin\$(PlatformName)\Debug\$(PlatformToolset)\dynamic\libzmq.dll"
          DestinationFiles="$(TargetDir)libzmq.dll"
          SkipUnchangedFiles="true" />
    <Copy Condition="$(Configuration.IndexOf('Debug')) != -1"
          SourceFiles="$(ProjectDir)..\..\..\..\bin\$(PlatformName)\Debug\$(PlatformToolset)\dynamic\libzmq.pdb"
          DestinationFiles="$(TargetDir)libzmq.pdb"
          SkipUnchangedFiles="true" />
    <Copy Condition="$(Configuration.IndexOf('Release')) != -1"
          SourceFiles="$(ProjectDir)..\..\..\..\bin\$(PlatformName)\Release\$(PlatformToolset)\dynamic\libzmq.dll"
          DestinationFiles="$(TargetDir)libzmq.dll"
          SkipUnchangedFiles="true" />
  </Target>

  <!-- Messages -->

  <Target Name="libzmq-info" BeforeTargets="AfterBuild" Condition="'$(Linkage-libzmq)' == 'dynamic'">
    <Message Text="Copying libzmq.dll -&gt; $(TargetDir)libzmq.dll" Importance="high"/>
    <Message Text="Copying libzmq.pdb -&gt; $(TargetDir)libzmq.pdb" Importance="high" Condition="$(Configuration.IndexOf('Debug')) != -1" />
  </Target>

</Project>

2.1.5、编译生成

按照官网的方式,这一步应该是成功了,但是目前是失败,还少文件,可能是官方下载的包有问题

2.2、用 C++ 的cmake方式

2.2.1、准备好编译环境

上面用 VS 既然不行,这里就不用VS,用Qt或者直接用cmake来尝试cmake编译。

2.2.1.1、准备编译环境

这里用的是Qt的Creator来编译,准备好cmake

2.2.1.2、编译一个纯C++程序

编译一个纯C++程序,选择cmake,然后编译运行,查看编译环境是否成功

2.2.1.3、查看编译结果,确保Cmake可用

直接用MinGW来编译,查看结果

2.2.2、打开ZeroMQ的CMakeLists.txt文件

在ZeroMQ的主目录,找到CMakeLists.txt文件,然后用Qt Creator打开

2.2.3、编译ZeroMQ

编译完ZeroMQ就可以直接看编译成功

3、编译后处理

3.1、找到重点三类文件

重点是以下几个文件,找到就可以自己使用了

  • 1、include文件夹下的.h文件
  • 2、.lib库
  • 3、.dll库

3.2、文件分类保留处理

因为这个是Debug模式下编译的,所以我们可以自己建立一份Debug模式的文件夹。很多不同的第三方库,Debug和Release文件编译出的结果大部分是不同的,所以在调用时一般也会区分库的处理,这里我们做分类保留处理即可。

如何调用库我就不在赘述了,之前的文章中讲述过了,可以查看文章开头 第 1 章概述中的相关资料查看。

相关推荐
cpsvps_net1 小时前
美国服务器环境下Windows容器工作负载智能弹性伸缩
windows
甄超锋1 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
cpsvps4 小时前
美国服务器环境下Windows容器工作负载基于指标的自动扩缩
windows
网硕互联的小客服7 小时前
Apache 如何支持SHTML(SSI)的配置方法
运维·服务器·网络·windows·php
etcix7 小时前
implement copy file content to clipboard on Windows
windows·stm32·单片机
前端市界7 小时前
前端视角: PyQt6+Vue3 跨界开发实战
前端·qt·pyqt
许泽宇的技术分享7 小时前
Windows MCP.Net:基于.NET的Windows桌面自动化MCP服务器深度解析
windows·自动化·.net
非凡ghost8 小时前
AMS PhotoMaster:全方位提升你的照片编辑体验
windows·学习·信息可视化·软件需求
mortimer10 小时前
一次与“顽固”外部程序的艰难交锋:subprocess 调用exe踩坑实录
windows·python·ai编程
gameatp12 小时前
从 Windows 到 Linux 服务器的全自动部署教程(免密登录 + 压缩 + 上传 + 启动)
linux·服务器·windows