为啥要写二进制编译 exe程序,CMake minGW练习教程?

前阵子,钻研了一下electron,想开发一款离线图片压缩桌面程序。写的过程中发现程序在,windows环境下运行没问题,然而在linux 和 macOS会有问题。常常排查发现是node依赖的第三方插件问题(只给了 exe程序)。
所以开始着手钻研,如何通过 c++之类的源码,编译出别离对应 Wndows Linux MacOS的二进制应用程序。
通过一周的工夫查阅大量的材料(google),最终得出上面的流程。

当初只是学会了,编译Linux 和windows的应用程序,然而我想要是 在一个零碎环境下别离编译出Windows Linux MacOS。目前只接触到了 穿插编译这个货色。正在着手学习,摸索出残缺无坑的流程再持续发相干文章。

本文存储于github

工具筹备

OS: windows 10

CMake: https://cmake.org/

Visual Studio 2019: https://visualstudio.microsof...

下载 libpng-1.6.35 源码 Download the libpng-1.6.35 source code

libpng: https://github.com/glennrp/li...

解压 libpng-1.6.35.tar.gz

directory|-libpng-1.6.35

关上 zlib.props 文件,查看依赖zlib的版本

libpng > projects -> vstudio -> zlib.props

line 34  <ZLibSrcDir>..\..\..\..\zlib-1.2.8</ZLibSrcDir>

下载 zlib-1.2.8 源码 Download the zlib-1.2.8 source code

zlib : https://github.com/madler/zli...

解压 zlib-1.2.8.tar.gz

directory|-libpng-1.6.35|-zlib-1.2.8

编译 zlib-1.2.8

  1. 关上 CMake gui
  2. 点击 Browse Source 抉择 zlib-1.2.8

  1. 点击 Browse Build 抉择 zlib-1.2.8/build 目录
  2. 点击 Configure(生成CMake Cahce) 抉择 Visual Studio
  3. 和 x64点击 Finish 期待输入音讯 Configuring done

  1. 点击 Generate(生成Visual Studio的*.sln) 期待输入音讯 Generating done

  1. 点击 Open Project,会在 Visual Studio
  2. 中关上
  3. 生成debug版的动态链接库。

    (1). 确认解决方案为debug、X64平台。(2). 在zlibstatic我的项目上右击–>生成  期待 输入窗口提醒生成胜利 (2个)(3). zlib的debug、X64版的动态链接库曾经编译胜利。![5.png](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3280b06039684e1da05855fdd2c762c4~tplv-k3u1fbpfcp-watermark.image)
  4. 生成release版的动态链接库。

    (1). 确认解决方案为release、X64平台。(2). 在zlibstatic我的项目上右击–>生成  期待 输入窗口提醒生成胜利 (2个)(3). zlib的release、X64版的动态链接库曾经编译胜利。

  1. 最重要的一步 复制 zlib-1.2.8/build/zconf.h 文件到 zlib-1.2.8/

编译 libpng-1.6.35

  1. 关上 CMake gui
  2. 点击 Browse Source 抉择 zlib-1.2.8
  3. 点击 Browse Build 抉择 zlib-1.2.8/build 目录
  4. 勾选 Advanced, 批改 ZLIB_INCLUDE_DIR,ZLIB_LIBRARY_DEBUG、ZLIB_LIBRARY_RELEASE 后点击 Configure

    ZLIB_INCLUDE_DIR       C:\c++\zlib-1.2.8ZLIB_LIBRARY_DEBUG     C:\c++\zlib-1.2.8\build\Debug\zlibstatic.libZLIB_LIBRARY_RELEASE   C:\c++\zlib-1.2.8\build\Release\zlibstatic.lib

  5. 单击 Generate,生成Visual Studio 2019的*.sln我的项目。
  6. 点击 Open Project,会在 Visual Studio 2019 中关上
  7. 在Visual Studio 2019中别离以Debug和Release形式编译png_static我的项目。

编译 mozjpeg-4.0.3

没有前置依赖 按下面的流程即可

我想用 minGW 怎么编译?

  1. 在 CMake gui 中,点击 Configure ,抉择 “MinGW Makefile” 期待 Configuring done
  2. 点击 Generate 期待 Generate done
  3. 关上 cmd,执行命令 cd C:\c++\zlib-1.2.8\build\
  4. 执行 minGW32-make (每个装置的minGW make命令可能不一样 自行抉择即可)

编译进去的 exe我独自复制进去不能怎么回事?

因为 应用程序 须要依赖 某个dll,将这个dll 放在和exe程序同一地位即可

Built on

  • Github
  • libpng: libpng is the official PNG reference library.
  • zlib: ZLIB DATA COMPRESSION LIBRARY
  • mozjpeg: MozJPEG improves JPEG compression efficiency achieving higher visual quality and smaller file sizes at the same time.
  • CMake: CMake is an open-source, cross-platform family of tools designed to build, test and package software.
  • Visual Studio 2019:A full-featured IDE that can be used to code, debug, test, and deploy to any platform.
  • minGw:Mingw-w64 is an advancement of the original mingw.org project, created to support the GCC compiler on Windows systems.