From Texts to Executables
a series of debugging
Motivation
- facing a bunch of bugs when I setup SDL2
- kinds of write up
Background & Goal
Background - SDL2
- Cross-platform software development library
- Written in C
- Allow you to write games that can run on Linux, macOS, and Windows...
- Similar to Allegro

Official libraries
- SDL_image
- SDL_mixer
- SDL_net
- SDL_ttf
- SDL_rtf
- What we need
- SDL2, SDL2_image, SDL2_ttf, SDL2_gfx
Goal
- Make the codes run on MacOS and Windows
- Make the setup process easy
.
├── Makefile
├── fonts
│ └── ...
├── imgs
│ └── ...
├── maps
│ ├── ...
├── src
│ ├── constants.hpp
│ ├── game_utils.cpp
│ ├── game_utils.hpp
│ ├── main.cpp
│ ├── utils.cpp
│ ├── utils.hpp
│ ├── controllers
│ │ ├── ...
│ ├── views
│ │ ├── ...
│ ├── helpers
│ │ ├── ...
│ ├── models
│ │ ├── ...
...
Run on MacOS
Brew Install + Make
brew install sdl2
brew install sdl2_image
brew install sdl2_ttf
brew install sdl2_gfx
make
But on Windows...
Cygwin
POSIX-compatible programming and runtime environment
directory layout that is similar to Unix-like systems
package system


Install Package

No SDL2_gfx
Make
Add the missing header files my solve the problem

Where should I put?
Let's talk about compiler first
- Preprocess
- Compile
- Assemble
Preprocess
- Replace
- # define ll long long
- # include "foo.h"
// add.cpp
int add(int a, int b) {
int result = a + b;
return result;
#include "endBrace.h"
// endBrace.h
}

g++ -E -c add.cpp
Include Path
- g++ --verbose -Iheader -c add.cpp

After adding header files

- compile successfully (generate object.o)
- linking error caused by missing files
Linker
What do linkers do
- find functions & symbols then link them together
- .o files -> executables
- view.o, object.o, keyboard_controller.o, pacman.o...
- -> main.exe
One kind of Linking error
// main.cpp
void Log(const char* message);
int main() {
Log("hello world");
}
// log.cpp
#include <iostream>
void Logg(const char* message) {
std::cout << message << std::endl;
}
If there's only 1 object file
- linker is still needed
- for example
- tell c++ runtime library where’s the entry-point

Let's see our header files
#ifndef SDL_shape_h_
#define SDL_shape_h_
#include "SDL_stdinc.h" C++ */
#ifdef __cplusplus
extern "C" {
#endif
#define SDL_NONSHAPEABLE_WINDOW -1
#define SDL_INVALID_SHAPE_ARGUMENT -2
#define SDL_WINDOW_LACKS_SHAPE -3
extern DECLSPEC SDL_Window * SDLCALL SDL_CreateShapedWindow(
const char *title,unsigned int x,unsigned int y,unsigned int w,
unsigned int h,Uint32 flags);
...
header files are not enough, so related lib should be linked

Library Path
-
-L<directory>
-
-lSDL2
-
find libSDL2
-

After adding related lib
Seem to work???


dll -> dynamic linking library
Then I use MinGW.
Static Linking & Dynamic Linking
Static Linking
- copy all library modules into the final executable image
- after linking, libraries are inside your exe file
- need static library
Pros
- usually faster
Cons
- larger in size

Dynamic Linking

- libraries are linked during runtime
-
need import library
Pros
- smaller in size
Cons
- usually slower
Where's dll
-
The directory of the executable
-
The current directory
-
The Windows system directory
-
The Windows directory
-
The directories listed in the PATH environment variable

But my code is
MinGW\bin\g++ .\src\utils.cpp -c -o bin\utils.o
.
├── Makefile
├── bin
│ └── main.exe
├── src
│ └── ...
├── MinGW
│ ├── bin
│ │ ├── g++.exe
│ │ ├── SDL2.dll
│ │ ├── ...
│ ├── lib
│ │ ├── libSDL2.a
│ │ ├── ...
│ ├── include
│ │ ├── SDL2
│ │ ├── SDL2_rotozoom
...
Full Picture






compile & assemble


Preprocess
libraries
(.lib)
.dll
include
lib
bin

- bin for dll files
- include for header files
- lib for libraries
- set up PATH environment in the batch file
Reference
text to executable
By hsutzu
text to executable
- 304