Praktische Aspekte der Informatik Moritz Mühlhausen Prof. Marcus Magnor https://graphics.tu-bs.de/teaching/ss19/padi/
Make, Libraries, and Debugging make, 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++ Output value CXXFLAGS=-I. – g – std=c++11 EXE=worldbuilder All input values $(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 $< First input value (usually *.cpp) […] 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 $@ $^ %.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) Shared Libraries (.so/.dll) • Benefits: • Benefits: No need for distributing Keep your binaries small. additional files. Can be shared between No changes after multiple apps. compilation. • Drawbacks: • Drawbacks: May change after Increases the file size of compilation. your binary. Application needs to Redundancy when used know location of files in multiple applications. 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/
Recommend
More recommend