-
We need to be able to use libraries for Games Programming
-
For this module, we’ll mainly be using the {link-sdl2}[SDL2] library
-
By the end of this session, you should have:
-
gotten our test program to run, using two different build systems
-
manual approach
-
automated approach (using {link-conan}[conan.io] and {link-cmake}[cmake])
-
-
written an automation script to fully automate that
-
e.g. a Windows
.batfile
-
-
-
Imagine you have a friend/collegue/future self that needs to develop program
-
They/you will need to setup their system for this
-
Write (through this session) instructions of how to set up for developing your program
-
write by hand or a digital document
-
make sure to give all steps, with sufficient detail
-
-
Email your instructions at the end of the session
-
-
There are 3 main stages going from source code to a running program.
-
This is true independent of C++
-
Compile allh *translation unit" (roughly, each source code file)
-
Creates binary object files (
.objon Windows) -
COMPILE-TIME errors
-
Link object files together
-
Link also with any static libraries (
.libon Windows) -
Creates an executable binary (
.exeon Windows) -
LINK-TIME errors
-
Header-only libraries
-
just source code - EASY
-
-
Static libraries
-
embedded into our exe by the linker - MEDIUM
-
-
Dynamically-linked libraries / Shared libraries
-
linked to our exe at run-time - HARD
-
-
Header-only libraries only contain source code.
-
Our compiler will compile all this library code for us right into our program.
-
Static libraries still have header files
-
these tell us and our compiler the interface to the library
-
what functions/variables exist
-
-
Static libraries have pre-built binary files that will be embedded in our executable program
-
Find library header files and put somewhere on our local system
-
Use the header files (
.hor.hpp) from our code -
Tell our compiler where to look for header files to compile in
-
Tell our linker which libraries to link to
-
Tell our linker where to look for binary library files are (
.lib)
-
Dynamic libraries still have header files
-
these tell us and our compiler the interface to the library
-
what functions/variables exist
-
-
Dynamic libraries usually have an import library (
.lib) files-
tell our linker what is in the binary and where
-
-
Dynamic library have dynamically linked binaries (
.dll)-
provide executable code to our program, loaded at run-time
-
-
Find library header files and put somewhere on our local system
-
Use the header files (
.hor.hpp) from our code -
Tell our compiler where to look for header files to compile in
-
Tell our linker which libraries to link to
-
Tell our linker where to look for binary import library files are (
.lib) -
Make sure our Operating System can find the Dynamic libraries at run-time
-
on Windows, this would in the path, or in the same directory as the
.exe
-
-
We’ll start with a "HelloWorld!" Program, with some initial, minimal functions from SDL2 that we wish to use
-
We’ll go through a series of steps to:
-
allow our program to compile
-
allow our program to link
-
allow our program to run
-
-
We’ll then repeat this using {link-conan}[conan.io] and {cmake}[cmake] to make our lives easier and more scalable
-
our program will depend on at least 3 libraries during this module
-
-
New Project
-
Visual C++ → Win32 Console Application
-
ANYWHERE you choose
-
Make "Empty project"
-
Untick "Security Development LifeCycle"
-
-
Add a new source file named
main.cpp -
Add the contents below BY HAND - don’t copy-paste
link:{examplesdir}/buildSystem/main.cpp[role=include]-
Note the
#includedirectives-
this ask the compile to literally copy-paste that header into this file
-
those in angle-brackets (
<or>) are System headers
-
-
Note the function names beginning with
SDL-
these functions are from the SDL2 library (we hope/intend)
-
Your IDE (Visual Studio) is telling you something about these. What is it saying? What is the error?
-
-
Look carefully at the function signature of the main function
-
Build → Build Solution (Ctrl-Shift-B)
-
What are the errors?
-
Search the web for SDL2 and try to find a download link
-
There are a number of versions on https://www.libsdl.org/download-2.0.php
-
Which do you think you want?
-
HINT: We are developing software
-
HINT: What platform are we on?? and Word-size??
-
HINT: What is our compiler suite?
-
-
Which version of SDL2 did you download??
-
What is it’s version number?
-
-
Extract your download
-
Where do you think you should you put it?
-
Your program will need to access them
-
What happens if you move your project ??
-
-
How big are these files?
-
What folders and files have you extracted?
-
Do you think these files should be versioned along with your source code?
-
-
What header file you need??
-
What are all these other files??
-
-
Where in the library you’ve download is this header file?
-
How do you tell Visual Studio where header files are?
-
Search for how to?
-
Figuring out questions like this yourself is important.
-
Is Visual Studio confusing on this?
-
-
How many clicks do you need to do in Visual Studio to do this?
-
Think this is error prone/easy/repeatable?
-
-
and which libraries to try to link to
-
How do you tell Visual Studio where library files are?
-
How do you tell Visual Studio which libraries to link to?
-
How many clicks do you need to do in Visual Studio to do this?
-
-
Where does your Operating System look for Dynamic libraries by default?
-
Can you change this? Should you?
-
What else can you do?
-
HINT: see the above heading!!
-
-
Assuming you now have a running program
-
Run your
.exefrom the command line -
Can you make it so that when you run your program from Visual Studio you can see the output?
-
HINT: there are many ways of doing this
-
-
-
Find a collegue/friend
-
Go through each other instructions
-
What did you each do the same, or differently?
-
Do those things matter?
-
-
What would this be like if you had 3 libraries your project needed?
-
or 8 libraries?
-
-
Historically, C++ hasn’t had a package manager
-
Most/Many other languages do have (good/poor) package managers
-
-
{link-conan}[Conan.io] is pretty new
-
that’s great - makes things easier
-
that’s challenging - this change and break
-
changes, broken things is very common
-
especially in Games!!
-
-
-
Conan.io is pre-installed on Lab machines
-
if working at home or on a laptop, you’ll need to install it
-
-
If you want to use a versioning system, now would be a good time to start
-
we strongly recommend the use of some kind of versioning
-
we recommend git, but other good tools exist
-
-
Make a new folder somewhere for your project
-
call it
gamesProgrammingor anything you like -
you can (if you like) remove all the library files you carefully downloaded!!
-
-
We tell conan about the libraries we need with special file -
conanfile.txt-
conan can then go off and find these libraries for us
-
and even, build them from source if it needs to
-
and create files for us, that tell us where things are
-
-
Create a file name
conanfile.txtin the root of your new (gamesProgramming) folder, with the following content-
this gives conan the library name, version and who’s package to use, and the channel
-
see conanio on readthedocs for more info
-
link:{examplesdir}/buildSystem/conanfile.txt[role=include]-
We’ve said our project requires the
SDL2library, version2.0.4that has been packaged bylasoteand is in thestablechannel -
We’ve said the option
sharedon theSDL2library is set toTrue-
meaning we want a shared (dynamic) version of SDL2
-
we could go with
Falseand embed SDL2 directly in our program-
we shouldn’t do this
-
for good practice
-
and without checking whether the license terms of the library allow this (e.g. GPL license) - Discuss/Research
-
-
-
We’ve said we want conan to generate output for
cmake-
this output will contain details on where the needed parts of libraries are located, etc.
-
-
Go to your
gamesProgrammingfolder with a console-
we recommend gitBash on Lab machines
-
that way you can use git from the commandline also if you choose
-
-
Make a new directory called
build-
this is where are build files will go
-
most of these will be temporary - we don’t need to version them
-
using a separate directory keeps the build clean
-
-
in your console
cdtobuild -
run
conan install ..from thebuilddirectory-
ask conan to install packages listed in the
..(parent) directory (inconanfile.txt) -
watch the output
-
try to figure out what conan is doing
-
what information does it tell you at the end?
-
-
Conan has created a bunch of files for us
-
What two, separate locations has conan put files?
-
What files has conan put in out present directory?
-
-
Have a look at the
conanbuildinfo.cmake-
This is a file that tells cmake about file locations
-
It’s not very human readable
-
-
Add a generator
textto yourconanfile.txtand re-run the conan install command-
Have a look a the new file - this should tell you stuff in an easier way
-
But not useful for anything except:
-
your knowledge or
-
if you had to set things up manually (you could do this in Visual Studio now, using the libraries conan has installed)
-
you can remove the
textgenerator if you want, and clear the associated, generated file
-
-
-
{link-cmake}[cmake] is a build system builder!!
-
It can make various forms of build system
-
We’re interested in Visual Studio solutions
-
We tell cmake what we want using a
CMakeLists.txtfile-
note the capitalisation
-
-
In the root of your project folder (
gamesProgramming), create a file named, exactly,CMakeLists.txt-
with the following content
-
link:{examplesdir}/buildSystem/CMakeLists.txt[role=include]-
The name of the solution (
ConanTest) -
minimum cmake version ( 2.8.12)
-
use the
conanbuildinfo.cmakefile we made -
run some stuff to make conan work
-
make an
execalledconanTestfrommain.cpp -
use libraries from conan for that exe
conanTest -
make
conanTestour startup project
-
In your
builddirectory run the following, from the command line (gitBash)-
note what build system we’re and platform we’re asking cmake to create for
-
the
..tell cmake to look in the parent directory for theCMakeLists.txtfile
-
cmake .. -G "Visual Studio 14 Win64"-
Load the Visual Studio solutin (.sln) in the build directory, and build from the UI
-
OR ask cmake to build (with Visual Studio’s compiler) for you
cmake --build . --config Release #ask cmake to build in Release mode-
We’ve only really executed 4 commands above
-
that depend on 3 files
-
-
BUT that is a lot to remember; hard to get right; easy to get wrong; etc
-
We should automate that too.
-
Batch files, AKA Script files can executed commands for us
-
There are many different scripting engines
-
default on Windows - Batch files -
.bat -
default on Linux - Shell files -
.sh -
Powershell
-
"real" scripting languages
-
Python, Lua, Ruby, …
-
-
-
Automate the steps from part two using a Batch/Script file
-
it needs to do all the steps we’ve done automated
-
making and going into
builddirectory -
conan
-
cmake
-
clean up (leave the command line at the same path)
-
-
Now you have SDL2 installed and working from your program, try to:
-
Make SDL log the "HelloWorld!" output, instead of using
cout-
What do you need to do differently?
-
-
Look up SDL’s documentation of Logging.
-
What control do you have over the logs?
-
Why might you want this control? - discuss with a collegue/friend
-
-
Ask SDL to create you a Window
-
Search SDL’s documentation for the function
-
What parameters does it need? What do they each do?
-