FROM: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/mingw/index.php

Setting up SDL 2 on MinGW

Last Updated 6/21/14

1)First thing you need to do is download SDL headers, libary and binaries. You will find them on the SDL website, specifically on this page.

You'll want to download the MinGW development libraries.

Open the gzip archive and there should be a tar archive. Open up the tar archive and the should be a folder called SDL2-2.something.something. Inside of that folder there should be a bunch of folders and files, most importantly i686-w64-mingw32 which contains the 32bit library and x86_64-w64-mingw32 which contains the 64bit library.

This is important: most compilers still compile 32bit binaries by default to maximize compatibility. We will be using the 32bit binaries for this tutorial set. It doesn't matter if you have a 64bit operating system, since we are compiling 32bit binaries we will be using the 32bit library.

Inside of i686-w64-mingw32 are the include, lib, and bin folders which contain everything we need compile and run SDL applications. Copy the contents of i686-w64-mingw32 to any directory you want. I recommend putting it in a folder that you dedicate to holding all your development libraries for MinGW. For these tutorials I'm putting it in a directory I created C:\mingw_dev_lib

2)Next you're going to want to get up the path for mingw so you can run mingw commands in any directory. Open up the system menu either by A) right clicking My Computer and selecting Properties or B) going to the Control Panel and selecting the system menu. Once your in the system menu, click advanced system settings.

and then click environment variables

Under system variables, select the "Path" variable and click edit.

What the path variable does is tell the OS where to look when running an executable. What we want to do is whenever we run the g++ command, the OS should look in the MinGW bin directory where g++.exe is located. If you installed MinGW by itself, the MinGW bin directory should be

C:\MinGW\bin

Append it to the long list of paths with a semi-colon after it and click ok.

If you're using an IDE like Code::Blocks which uses MinGW, you can also set its MinGW bin directory which should be at

C:\Program Files (x86)\CodeBlocks\MinGW\bin

Now when ever you run a command that uses a MinGW executable, the OS will know to look in the MinGW bin directory.

If you run your program and it complains that it can't find SDL2.dll, it means you did not set the path correctly.

3)Now go download the source for lesson 01. Extract the source somewhere. Open up a command window in the directory by holding shift and right clicking.

Now compile by entering this big old command (This command assumed you have SDL 2 extracted at C:\mingw_dev_lib):

g++ 01_hello_SDL.cpp -IC:\mingw_dev_lib\include\SDL2 -LC:\mingw_dev_lib\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o 01_hello_SDL

Having to manually punch in this compilation command gets very tedious very quickly. This is why I recommend using Make.

4) MingGW Make allows you to make build scripts that'll automate the compilation process.

From Basic Makefile
#OBJS specifies which files to compile as part of the project
OBJS = 01_hello_SDL.cpp #OBJ_NAME specifies the name of our exectuable
OBJ_NAME = 01_hello_SDL #This is the target that compiles our executable
all : $(OBJS)
g++ $(OBJS) -IC:\mingw_dev_lib\include\SDL2 -LC:\mingw_dev_lib\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o $(OBJ_NAME)

Here we have a basic Makefile. At the top we declare and set the "OBJS" macro which specifies which files we're compiling. Then we set the "OBJ_NAME" macro that specifies the name of our executable.

After setting these two macros, we have the "all" target which compiles the program. It's followed by the dependencies which as you can see is the OBJS macro, because obviously you need the source files to compile the program.

After specifying the name of the target and its dependencies, the command to create the target is on the next line. The command to create the target must begin with a tab or Make will reject it.

As you would expect, the command to compile the program is largely the same as the command we would compile it off the command line. A key difference is that we have macros that we insert into the command which makes things like adding new files to the project must easier since you only have to change the macro as opposed to changing the whole command.

In future tutorials, we will be using more libraries. We should probably use more macros to make the process of adding them easier.

From Makefile

#OBJS specifies which files to compile as part of the project
OBJS = 01_hello_SDL.cpp #CC specifies which compiler we're using
CC = g++ #INCLUDE_PATHS specifies the additional include paths we'll need
INCLUDE_PATHS = -IC:\mingw_dev_lib\include\SDL2 #LIBRARY_PATHS specifies the additional library paths we'll need
LIBRARY_PATHS = -LC:\mingw_dev_lib\lib #COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
# -Wl,-subsystem,windows gets rid of the console window
COMPILER_FLAGS = -w -Wl,-subsystem,windows #LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2 #OBJ_NAME specifies the name of our exectuable
OBJ_NAME = 01_hello_SDL #This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)

Now our compilation command is much more flexible.

Near the top we have the macros that define the files we're compiling and the compiler we're using.

Next we have the "INCLUDE_PATHS" macro which specifies the additional directories we're getting header files from. As you can see, we're using the include directory from the SDL2 folder we extacted earlier. The "LIBRARY_PATHS" sets the additional library file paths. Notice how there's a -I before every include directory and a -L before every library directory.

The "COMPILER_FLAGS" macro are the additional options we use when compiling. In this case we're disabling all warnings and disabling the console window. The "LINKER_FLAGS" macro specifies which libraries we're linking against. Here we're compiling against 32bit mingw, SDL2, and SDL2main. Notice how there's a -l flag before every library.

Finally at the bottom we have our target compiling using all of our macros. Thanks to macros we can very easily change the macros to add more files and libraries as we need them.

Save this Makefile code to a file named "Makefile" (case sensitive with no file extension) or you can use the one I premade here. Open a command line in the directory with the source files and run the command mingw32-make.exe. Make will search for a file named "Makefile" in the directory Make was called in and run the Makefile that will compile your code.

Now that you have SDL 2 compiling, it time to go onto part 2 of the tutorial.

【转】Setting up SDL 2 on MinGW的更多相关文章

  1. 【转】Setting up SDL Extension Libraries on MinGW

    FROM:http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/windows ...

  2. 【转】Setting up SDL Extension Libraries on Code::Blocks 12.11

    FROM: http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/window ...

  3. 【转】Setting up SDL Extension Libraries on Windows

    FROM: http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/window ...

  4. 【转】Setting up SDL 2 on Code::Blocks 12.11

    FROM: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/codeblocks/index.php Setting up SDL 2 on ...

  5. 【转】Setting up SDL on Windows

    FROM: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/index.php Setting up SDL on Windows Last ...

  6. 【转】Setting up SDL 2 on Visual Studio 2010 Ultimate

    from: Lazy Foo'Productions - Setting up SDL 2 on Visual Studio 2010 Ultimate 1)First thing you need ...

  7. 【转】Setting up SDL Extension Libraries on Visual Studio 2010 Ultimate

    FROM:http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/windows ...

  8. 【转】Setting up SDL Extension Libraries on Visual Studio 2019 Community

    FROM:http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/windows ...

  9. 【转】Setting up SDL 2 on Visual Studio 2019 Community

    FROM: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/msvc2019/index.php Setting up SDL 2 on V ...

随机推荐

  1. PHP正则表达式核心技术完全详解 第1节

    作者:极客小俊 一个专注于web技术的80后 我不用拼过聪明人,我只需要拼过那些懒人 我就一定会超越大部分人! CSDN@极客小俊,原创文章, B站技术分享 B站视频 : Bilibili.com 个 ...

  2. 与Bat脚本的故事

    因为工作时需要将定时处理的业务抽出来,废弃通过监听定时调用的这种方法,改为通过第三方软件定时执行bat脚本来实现,所以学习了一下bat脚本,整理出一些学习中的基础点和重点. 基础点: (1)bat脚本 ...

  3. 实验 5:OpenFlow 协议分析和 OpenDaylight 安装

    一.实验目的 回顾 JDK 安装配置,了解 OpenDaylight 控制的安装,以及 Mininet 如何连接;通过抓包获取 OpenFlow 协议,验证 OpenFlow 协议和版本,了解协议内容 ...

  4. JVM调优常用参数总结

    GC通用参数 -Xmn -Xms -Xmx -Xss 年轻代 最小堆 最大堆 栈空间 -XX:+UseTLAB 使用TLAB,默认打开 -XX:+PrintTLAB 打印TLAB的使用情况 -XX:T ...

  5. H264裸码流I/P/B帧类型判别

    花了两天时间做了个h264裸流nal类型和frame类型检测的工具,已上传至github,有需要的自行下载. 1.NAL类型检测 nal类型检测非常容易,对照下表即可容易判断类型. 较常用nal类型包 ...

  6. 2017-01-26--编译busybox总结

    错误一: ox@ubuntu:busybox-1.16.0$ make menuconfig Makefile:431: *** mixed implicit and normal rules: de ...

  7. 手把手教你AspNetCore WebApi:缓存(MemoryCache和Redis)

    前言 这几天小明又有烦恼了,系统上线一段时间后,系统性能出现了问题,马老板很生气,叫小明一定要解决这个问题.性能问题一般用什么来解决呢?小明第一时间想到了缓存. 什么是缓存 缓存是实际工作中非常常用的 ...

  8. 多测师讲解selenium _携程选票定位练习_高级讲师肖sir

    打开携程网 from selenium import webdriverfrom time import sleepfrom selenium.webdriver.common.keys import ...

  9. day45 Pyhton 数据库Mysql 02

    一.前期回顾 数据库 mysql的安装 配置环境 为什么要用数据库? 稳定性 一致性 并发 存取数据效率高 数据库的分类 关系型数据库 mysql oracle sqlserver 非关系型数据库 r ...

  10. day10 Pyhton学习

    一.昨日内容回顾 函数: 定义:对功能或者动作的封装 def 函数名(形参): 函数体 函数名(实参) return:  返回,当程序运行到return的时候,终止函数的执行 一个函数一定拥有返回值 ...