Windows平台下使用cmake构建SDL2和SDL2_image库

一、 环境准备与工具安装

  1. 下载安装Mingw-w64 GCC编译器,注意安装路径中不要有空格和中文,否则后期使用GCC编译各种工具库的时候可能会出现路径错误

  2. 下载并安装CMake最新版本

  3. 设置运行时库的搜索路径(PATH),我把程序安装在了D盘,其中CMake安装在了D:\program Files\CMake文件夹下,Mingw-w64安装在了D:\msys64\mingw64文件夹下,如图指定了可执行程序搜索路径
    在这里插入图片描述

  4. 获取SDL2源码(官方GitHub或官网预编译库)和SDL2_image源码(官方GitHub或官网预编译库)或者它们在国内的镜像,我下载的是SDL-release-2.32.8版和SDL_image-release-2.8.12版。
    https://gitcode.com/GitHub_Trending/sd/SDL
    https://gitcode.com/gh_mirrors/sd/SDL_image

二、库目录结构设计

将下载的SDL2、SDL2_image文件夹解压,在其中新建build文件夹,如图:
在这里插入图片描述

三、编译SDL2与SDL2_image库

(一)编译SDL2库

在源文件夹中有个INSTALL.txt,它提示着各种平台和工具的编译方法:

To compile and install SDL:

    1.  Windows with Visual Studio:
        * Read ./docs/README-visualc.md

        Windows with gcc, either native or cross-compiling:
        * Read the FAQ at https://wiki.libsdl.org/FAQWindows
        * Run './configure; make; make install'

        macOS with Xcode:
        * Read docs/README-macosx.md

        macOS from the command line:
        * Run './configure; make; make install'

        Linux and other UNIX systems:
        * Run './configure; make; make install'

        Android:
        * Read docs/README-android.md

        iOS:
        * Read docs/README-ios.md

        Using Cmake:
        * Read docs/README-cmake.md

我准备使用Windows+Cmake进行编译,参考CMake那一行, docs/README-cmake.md里这样写着:

## Building SDL

Assuming the source for SDL is located at `~/sdl`
	```sh
	cd ~
	mkdir build
	cd build
	cmake ~/sdl
	cmake --build .
	```

This will build the static and dynamic versions of SDL in the `~/build` directory.
Installation can be done using:

	```sh
	cmake --install .        # '--install' requires CMake 3.15, or newer
	```

其中的~/sdl,就代表着刚才图片展示的源文件目录,脚本的意思是它在目录中要新建一个build文件夹(我之前已经建好了),然后在build目录中进行cmake操作。其中cmake ~/sdl含义是构建cmake起始脚本比如CMakeCache.txt,为build做准备,cmake --build .含义是在当前目录(build)中编译生成静态库(libSDL2main.a、libSDL2.a、libSDL2_test.a)和动态链接库(SDL2.dll)。而cmake --install .的含义是将编译好的库存放到C盘对应目录下,我的电脑装在了C:\Program Files (x86)\SDL2下。
在这里插入图片描述

报错处理

我在编译过程中遇到了CMAKE_C_COMPILER not set, after EnableLanguage -- Configuring incomplete, errors occurred!错误,这个错误的核心原因是Windows 上没有找到 C 编译器,CMake 无法配置 SDL2 编译环境。使用命令cmake -S .. -B . -G "MinGW Makefiles"即可完成编译。所以我的编译和安装完整代码为:

cd .
mkdir build
cd build
cmake -S .. -B . -G "MinGW Makefiles"
cmake --build .
sudo cmake --install .

其中sudo命令需在"系统>高级"里面打开:
在这里插入图片描述

(二)编译SDL2_image库

同SDL2编译过程一样,到源文件目录下,依次输入命令,直接上代码:

cd .
mkdir build
cd build
cmake -S .. -B . -G "MinGW Makefiles"
cmake --build .
sudo cmake --install .

可以看到安装成功了。
在这里插入图片描述

附:项目构建与调用的配置

以笔者前期编写的linux平台C++模拟蚁群为例,记录windows平台下代码移植。

(一)代码准备

  1. 修改.vscode/c_cpp_properties.json文件
    在该文件中添加include路径:
{
    "configurations": [
        {
            "includePath": [
                "${workspaceFolder}/**",
                "C:\\Program Files (x86)\\SDL2\\include\\SDL2",
                "C:\\Program Files (x86)\\SDL2_image\\include\\SDL2"
            ],
        }
    ]
}
  1. 修改main.cpp
#define SDL_MAIN_HANDLED //include<SDL.h>之前要定义此行,让SDL2不要接管main函数,否则gcc运行时找不到main函数
#include <SDL.h>
#include <SDL_image.h>

(二)使用GCC构建

  1. 编辑task.json文件
    加入include路径、load路径,编译运行即可。
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "g++",
            "args": [
                "-g",
                "-IC:\\Program Files (x86)\\SDL2\\include\\SDL2",
                "-IC:\\Program Files (x86)\\SDL2_image\\include\\SDL2",
                "-LC:\\Program Files (x86)\\SDL2\\bin",
                "-LC:\\Program Files (x86)\\SDL2_image\\bin",
                "-lSDL2",
                "-lSDL2_image",
                "main.cpp",
                "ant.cpp",
                "home.cpp",
                "ground.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值