praktische aspekte der informatikbuilding with cmake •create cmakelists.txt file and a build...
TRANSCRIPT
Praktische Aspekte
der InformatikMoritz Mühlhausen
Prof. Marcus Magnor
https://graphics.tu-bs.de/teaching/ss19/padi/
Make, Libraries,
and Debuggingmake, cmake, libraries, gdb, and IDEs
https://graphics.tu-bs.de/teaching/ss19/padi/
Further Reading
Warning!The following slides are meant to give you
a very superficial introduction.
If you want to learn more, have a look at:http://sslabmcs12.weebly.com/uploads/9/2/2/0/9220774/makefiletutorial.pdf
http://www.cmake.org/Wiki/CMake
http://www.cs.cmu.edu/~gilpin/tutorial
http://qt-project.org
https://graphics.tu-bs.de/teaching/ss19/padi/
Outline
Make
CMake
Libraries
Debugging with gdb
Debugging with IDEs
Assignment
https://graphics.tu-bs.de/teaching/ss19/padi/
Building with Make
• Benefits
Makes building your application easy
May define different “targets”
Targets may depend on each other
May contain Macros
Useful even for non-C++ projects (e.g. LaTeX)
• Drawbacks
Quickly becomes unwieldy for larger projects
https://graphics.tu-bs.de/teaching/ss19/padi/
Building with Make
Makefile
CXX=g++
CXXFLAGS=-I. –g –std=c++11
EXE=worldbuilder
$(EXE): main.o Block.o Sphere.o WorldBuilder.o Vector3D.o
$(CXX) $(CXXFLAGS) -o $@ $^
main.o: main.cpp WorldBuilder.h WorldObject.h Block.h Sphere.h
$(CXX) $(CXXFLAGS) -c $<
Block.o: Block.cpp Block.h WorldObject.h Vector3D.h
$(CXX) $(CXXFLAGS) -c $<
[…]
Vector3D.o: Vector3D.cpp Vector3D.h
$(CXX) $(CXXFLAGS) -c $<
clean:
rm -f *.o *~ $(EXE)
https://graphics.tu-bs.de/teaching/ss19/padi/
Building with Make
Makefile
CXX=g++
CXXFLAGS=-I. –g –std=c++11
EXE=worldbuilder
$(EXE): main.o Block.o Sphere.o WorldBuilder.o Vector3D.o
$(CXX) $(CXXFLAGS) -o $@ $^
main.o: main.cpp WorldBuilder.h WorldObject.h Block.h Sphere.h
$(CXX) $(CXXFLAGS) -c $<
Block.o: Block.cpp Block.h WorldObject.h Vector3D.h
$(CXX) $(CXXFLAGS) -c $<
[…]
Vector3D.o: Vector3D.cpp Vector3D.h
$(CXX) $(CXXFLAGS) -c $<
clean:
rm -f *.o *~ $(EXE)
Output value
All input values
First input value (usually *.cpp)
https://graphics.tu-bs.de/teaching/ss19/padi/
Building with Make
Makefile
CXX=g++
CXXFLAGS=-I. –g –std=c++11
EXE=worldbuilder
$(EXE): main.o Block.o Sphere.o WorldBuilder.o Vector3D.o
$(CXX) $(CXXFLAGS) -o $@ $^
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $<
clean:
rm -f *.o *~ $(EXE)
Pro: Easier to read
Con: make does not know header dependencies
https://graphics.tu-bs.de/teaching/ss19/padi/
Outline
Make
CMake
Libraries
Debugging with gdb
Debugging with IDEs
Assignment
https://graphics.tu-bs.de/teaching/ss19/padi/
Building with CMake
• Benefits
Cross-platform “Meta-Make”
Simple Scripting Language
Works on multiple platforms with multiple build systems
Can create Makefile, VS Solutions, Eclipse Projects, …
Can create installer files (.deb, .dmg, .msi)
• Drawbacks
You still have to write it by hand
https://graphics.tu-bs.de/teaching/ss19/padi/
Building with CMake
CMakeLists.txt
project(worldbuilder)
set(CMAKE_CXX_FLAGS "-g")
set(CMAKE_CXX_FLAGS_DEBUG)
set(worldbuilder_SOURCES
main.cpp
Block.cpp
Sphere.cpp
WorldBuilder.cpp
Vector3D.cpp)
add_executable(worldbuilder ${worldbuilder_SOURCES})
https://graphics.tu-bs.de/teaching/ss19/padi/
Building with CMake
• Create CMakeLists.txt file and a build folder
in your current directory.
• Move to the build folder and run: cmake ..
• If everything worked, run make to compile.
• Once Makefile is created, make also checks
for updates in CMakeLists.txt.
• To clean the cache just delete everything in the
build directory and run cmake .. again.
https://graphics.tu-bs.de/teaching/ss19/padi/
More Cross-Platform Building
• You may also want to try CMake alternatives
QMake (http://qt-project.org)
Ninja (https://martine.github.io/ninja)
Automake (http://www.gnu.org/software/automake)
and many more…
• You will have to develop cross-platform a lot!
• Learning to develop cross-platform today will save
you headaches in the future!
https://graphics.tu-bs.de/teaching/ss19/padi/
Outline
Make
CMake
Libraries
Debugging with gdb
Debugging with IDEs
Assignment
https://graphics.tu-bs.de/teaching/ss19/padi/
Static libraries (.a/.lib)
Static Libraries (.a/.lib)
• Benefits: No need for distributing
additional files.
No changes after compilation.
• Drawbacks: Increases the file size of
your binary.
Redundancy when used in multiple applications.
Shared Libraries (.so/.dll)
• Benefits: Keep your binaries small.
Can be shared between multiple apps.
• Drawbacks: May change after
compilation.
Application needs to know location of files during runtime.
https://graphics.tu-bs.de/teaching/ss19/padi/
Building Libraries
• Treat each library as a separate code project
Store them in separate directories
Use include path (I) and link path (L/l) flags
Use separate Makefiles
• Your main application then needs to know
Which libraries are used? (-l)
Where are the binaries (.a, .lib, …) stored? (-L)
Where are the headers (.h) stored? (-I)
https://graphics.tu-bs.de/teaching/ss19/padi/
Building Libraries
CXX=g++
OBJEXPORTPATH=../libobjexport
OBJEXPORTLIB=objexport
CXXFLAGS=-Wall -I$(OBJEXPORTPATH)/include -g -c
LDFLAGS=-L$(OBJEXPORTPATH)/lib -l$(OBJEXPORTLIB) -g
EXE=my_application
$(EXE): main.o
$(CC) -o $@ $^ $(LDFLAGS)
main.o: main.cpp
$(CC) $(CCFLAGS) $<
[…]
https://graphics.tu-bs.de/teaching/ss19/padi/
CMake and libraries
• Find and use external libraries
Define CMAKE_MODULE_PATH
In there, a Find[lib].cmake file contains a script
to include the [lib] library.
Use target_link_libraries to link them
• Create and use your own library
ADD_LIBRARY(yourlib STATIC ${SOURCE_FILES})
• This week’s materials contain an example for
OpenCV using CMake.
https://graphics.tu-bs.de/teaching/ss19/padi/
External library: OpenCV
• May be installed on your system…
apt-get, rpgm, msi, setup.exe
• … or you may build it yourself
• You need:
Static or shared library
Header files
https://graphics.tu-bs.de/teaching/ss19/padi/
External library: OpenCV
INCPATH = -I/usr/include/opencv
LIBPATH = -L/usr/lib/
OPTIONS = -lcv -lcvaux -lcxcore -lhighgui -lstdc++
CCFLAGS = -Wall -g
EXE=assignment_04
$(EXE): main.o
g++ $(LIBPATH) $(OPTIONS) $^ -o $@
main.o: main.cpp
g++ $(INCPATH) $(CCFLAGS) -c $<
clean:
rm -f *.o *~ $(EXE) testsmooth.png
https://graphics.tu-bs.de/teaching/ss19/padi/
Outline
Make
CMake
Libraries
Debugging with gdb
Debugging with IDEs
Assignment
https://graphics.tu-bs.de/teaching/ss19/padi/
Assertions
• Assertions make your application crash…… but in a useful way!
#include <cassert>
void foo(float probability) {
assert(0.0f <= probability
&& probability <= 1.0f);
// do something ...
}
• Assertions can be easily disabled for release:
#define NDEBUG // or use the –DNDEBUG flag with g++
• Code in disabled assertions is not executed!
https://graphics.tu-bs.de/teaching/ss19/padi/
Debugging with gdb
• gdb let’s you look at your program at runtime.
Variables
Call-stack
Breakpoints & Step-by-Step evaluation
• Requires debug symbols: -g
https://graphics.tu-bs.de/teaching/ss19/padi/
Debugging with gdb
gdb ./our_application
(gdb) run
our_application: Conifer.cpp:31: virtual std::vector< Quad,
std::allocator<Quad> > Conifer::getQuads() const:
Assertion 'center.z > 0.0' failed.
(gdb) bt
#0 raise () from /lib/libc.so.6
#1 in abort () from /lib/libc.so.6
#2 in __assert_fail () from /lib/libc.so.6
#3 in Conifer::getQuads at Conifer.cpp:31
#4 in Estate::getQuads at Estate.cpp:32
#5 in main () at main.cpp:42
(gdb) up
#1 in abort () from /lib/libc.so.6
(gdb) up
#2 in __assert_fail () from /lib/libc.so.6
(gdb) up
#3 in Conifer::getQuads at Conifer.cpp:31
(gdb) display m_size->z
1: this->m_size->z = 0
https://graphics.tu-bs.de/teaching/ss19/padi/
Outline
Make
CMake
Libraries
Debugging with gdb
Debugging with IDEs
Assignment
https://graphics.tu-bs.de/teaching/ss19/padi/
Programming with IDEs
• IDEs make your life simple Auto-completion, refactoring, …
Build organization, debugging, …
KDevelop, MS Visual Studio, Xcode, …
• Qt Creator Combines Editor, Compiler, Debugger, …
Coherent user-interface.
Many comfort functions.
Free cross-platform IDE.
Works with or without Qt.
• But first, a brief look at QMake…
https://graphics.tu-bs.de/teaching/ss19/padi/
Back to building: QMake
qttest.pro
CONFIG -= qt # We won't be using Qt
TEMPLATE = app # We're building an application...
TARGET = QtTest # ... and it's called “QtTest”
# Everything that ends in .h is a header file
HEADERS += *.h
# Everything that ends in .cpp is a source file
SOURCES += *.cpp
# Do not use this notation in a bigger project...
That’s all!
https://graphics.tu-bs.de/teaching/ss19/padi/
Programming with IDEs
• Open your .pro file in Qt Creator
• Your build tools are in the bottom left corner:
You can see the current state of
your project: “Debug” or “Release”.
You can run your code,
debug your code,
and of course build your code
https://graphics.tu-bs.de/teaching/ss19/padi/
Outline
Make
CMake
Libraries
Debugging with gdb
Debugging with IDEs
Assignment
https://graphics.tu-bs.de/teaching/ss19/padi/
Assignment
A. Make it so!Download the new materials. It’s a solution of last week’s assignment.
1. Create a working Makefile and build the code.
2. Create a CMakeLists.txt file and build again using that.
3. Finally, create a QMake .pro file and build using the Qt Creator.
B. That code is broken!You may have noticed that the “solution” contains several bugs.
1. Figure them out using the command line gdb.
2. Then do the same using an IDE (e.g. Qt Creator)
3. Now take a moment to appreciate IDEs.
https://graphics.tu-bs.de/teaching/ss19/padi/
Assignment
C. Create your first library
1. Modify the code to create a worldbuilder library.
Which files go into the library? Which do not?
2. Build your library and write a small application that links itYou may just recycle the old main.cpp.
3. Prepare your library for (imaginary) distribution
Remember, you have to distribute both your library’s object files
as well as all the necessary header files. You will also want a
CMake script that builds the library.
D. Try using another library!Download and build a library (SFML), and write a small application.
http://www.sfml-dev.org/
http://www.sfml-dev.org/documentation/2.3.2/
https://graphics.tu-bs.de/teaching/ss19/padi/
Using SFML
https://graphics.tu-bs.de/teaching/ss19/padi/
Final remarks
• You will have to use at least one external library
in your project.
• If you want to make a tool
Take a closer look at Qt.
We will cover Qt and GUI-programming in the future.
• If you want to make a game
For 2D (beginner), have a look at: SFML, SDL, …
For 3D (advanced), have a look at: Ogre, Irrlicht, …
https://graphics.tu-bs.de/teaching/ss19/padi/