Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions cpp/experiments/opengl_shaders/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Link GLEW and SDL

An experiment to link GLEW and SDL.

## How to run

I downloaded .dll and .lib files for 64-bit processor on Windows operating system. I checked the licences for SDL and GLEW and as I understood they allow redistribution. In case I would like to run this experiment on other machine, I would need it to be 64-bit and I would need the binaries.

I don't know of more convenient way to distribute them so I can download the binaries on other machine.

## Important

**DYNAMIC LIBRARIES MUST BE PLACED IN THE LOCATION OF EXECUTABLE!!!**

An alternative approach is to add it to wherever the executable searches for dynamic libraries on the system.

It seems it is common approach to deliver dynamic libraries in the directory of the executable. For example, World of Warcraft has dynamic libraries located next to the executable.

## Notes

Notes about problems I encountered when performing this experiment.

### Linking order is important

The following command to link SDL2, causes the error below the command:

```cmd
clang++ main.cpp -L./ -lSDL2 -lSDL2main -o main
```

```cmd
C:/msys64/ucrt64/bin/ld: .//SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text$mn+0x84): undefined reference to `SDL_wcslen'
C:/msys64/ucrt64/bin/ld: .//SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text$mn+0xa2): undefined reference to `SDL_iconv_string'
C:/msys64/ucrt64/bin/ld: .//SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text$mn+0xb6): undefined reference to `SDL_strlen'
C:/msys64/ucrt64/bin/ld: .//SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text$mn+0xf3): undefined reference to `SDL_memcpy'
C:/msys64/ucrt64/bin/ld: .//SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text$mn+0xfb): undefined reference to `SDL_free'
C:/msys64/ucrt64/bin/ld: .//SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text$mn+0x122): undefined reference to `SDL_SetMainReady'
C:/msys64/ucrt64/bin/ld: .//SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text$mn+0x12e): undefined reference to `SDL_main'
C:/msys64/ucrt64/bin/ld: .//SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text$mn+0x18e): undefined reference to `SDL_ShowSimpleMessageBox'
```

However, the following command linked without problems:

```cmd
clang++ main.cpp -L./ -lSDL2main -lSDL2 -o main
```

### SDL requires specific signature for main function

SDL2 requires the following signature:

```cpp
int main(int argv, char** args) {
// ...
}
```

Without signature like this, SDL2 causes an error:

```txt
undefined reference to `SDL_main`
```

[Here is stackoverflow answer.](https://stackoverflow.com/questions/10803918/undefined-reference-to-sdl-main)

### Linking against `opengl32.lib`

This library `opengl32.lib` is delivered with operating system. GLEW allows us to use latest OpenGL functions. Therefore we have to against `opengl32.lib` because we are using the code from there as well.

### Linking against dynamic libraries for GLEW and SDL

The reason dynamic libraries are linked without any code provided in this example is that the compiled binary is using dynamic libraries relative to the executable. An alternative would be to place dynamic libraries in `C:\Windows\System32`, but that is bad practice because any program can run them. If there is any vulnerability within them it can be exploited.

### Using better includes

I would like to include glew and SDL2 libraries as follows:

```cpp
#include "GL/glew.h"
#include "SDL2/SDL.h"
```

I had to tell the compiler where to search for included files.

The full command:

```cmd
clang++ main.cpp -L./ -lSDL2main -lSDL2 -lglew32 -lopengl32 -I./deps/GLEW-2.2.0/include -I./deps/SDL2-2.26.2/include -o main
```

One directory to search for includes:

```txt
-I./deps/GLEW-2.2.0/include
```

Another directory to search for includes:

```txt
-I./deps/SDL2-2.26.2/include
```

The reason i said _one and another directory_ is because it tells where to search for included files. Imagine the compiler as a person who has a list of places to visit in town. But we suggest one or more places to visit. This flag tells the compiler exactly that.

### Explanation of the compilation command

- Search for lib files in: `-L./deps/GLEW-2.2.0/lib`
- Search for lib files in: `-L./deps/SDL2-2.26.2/lib`
- Link SDL2main.lib: `-lSDL2main`
- Link SDL2.lib: `-lSDL2`
- Link glew32.lib: `-lglew32`
- The `opengl32.lib` file is distributed with operating system. There is no need to add it to the project, link flag: `-lopengl32`
- Tell the compiler where to search for included glew headers: `-I./deps/GLEW-2.2.0/include`
- Tell the compiler where to search for included SDL2 headers: `-I./deps/SDL2-2.26.2/include`
Binary file added cpp/experiments/opengl_shaders/SDL2.dll
Binary file not shown.
40 changes: 40 additions & 0 deletions cpp/experiments/opengl_shaders/compile.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@ECHO OFF

@REM LINKER SEARCH PATHS
SET link_search_flag=-L
SET link_search_path_sdl=%link_search_flag%./deps/SDL2-2.26.2/lib
SET link_search_path_glew=%link_search_flag%./deps/GLEW-2.2.0/lib
SET link_search_paths=%link_search_path_sdl% %link_search_path_glew%

@REM LINKER INCLUDES
SET link_flag=-l
SET link_sdl=%link_flag%SDL2main %link_flag%SDL2
SET link_glew=%link_flag%glew32
SET link_opengl=%link_flag%opengl32
SET link_libs=%link_sdl% %link_glew% %link_opengl%

@REM COMPILER INCLUDE PATHS
SET include_flag=-I
SET include_glew=%include_flag%./deps/GLEW-2.2.0/include
SET include_sdl=%include_flag%./deps/SDL2-2.26.2/include
SET include_paths=%include_glew% %include_sdl%

@REM SOURCE FILES
SET file_name=main
SET file_extension=.cpp
SET file=%file_name%%file_extension%

@REM OUTPUT
SET out_name=%file_name%
SET out_extension=.exe
SET out=%out_name%%out_extension%

ECHO Compiling %file%...

clang++ %file% %link_search_paths% %link_libs% %include_paths% -o %out%

@REM Check if compilation succeeded
IF %ERRORLEVEL% EQU 0 (
ECHO Compilation succeeded! Type ./%out% to run.
)
@REM Let compiler handle failure
73 changes: 73 additions & 0 deletions cpp/experiments/opengl_shaders/deps/GLEW-2.2.0/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
The OpenGL Extension Wrangler Library
Copyright (C) 2002-2007, Milan Ikits <milan ikits[]ieee org>
Copyright (C) 2002-2007, Marcelo E. Magallon <mmagallo[]debian org>
Copyright (C) 2002, Lev Povalahev
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The name of the author may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.


Mesa 3-D graphics library
Version: 7.0

Copyright (C) 1999-2007 Brian Paul All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Copyright (c) 2007 The Khronos Group Inc.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and/or associated documentation files (the
"Materials"), to deal in the Materials without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Materials, and to
permit persons to whom the Materials are furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Materials.

THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
Loading