Introduction To GNU Make And CMake - RRZE Moodle

3y ago
25 Views
3 Downloads
1.81 MB
45 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Nora Drum
Transcription

Introduction toGNU Make and CMakeMartin Ohlerich

Plan for Today .IntroductionShort Survey onBackground Experienceand PreferencesGNU MakeBasic UsageMakefilesCMakeBasic UsageCMakeLists.txtAdvanced Features

SurveyProgramming Experience? (For which OS? Which Programming Language?)Experiences with Build-Systems? (Make, CMake, Scons, IDE-inherent, self-written, .)Knowledge about the Build-Process?

C/C Build Process

Build Dependency Tree

Build Commands (Linux/C)Static Librariesgcc -c -Wall a.cgcc -c -Wall b.car -cvq libmystaticlib.a a.o b.oShared Librariesgcc -fPIC -c -Wall c.cgcc -fPIC -c -Wall d.cgcc shared -Wl,-soname,libmydynlib.so.1 -o libmydynlib.so.1.0.1 c.o d.o -lcgcc -Wall -I include -o prog.exe -L. -lmystaticlib -lmydynlib proc.c# usage

What we want is . Simplicity!!!. Flexibility!!!. Robustness!!!. Sytem-Independence!!!

GNU Make - Basic Usage

Example ProgramsC program :prog.cC program :prog.cppFortran program :prog.f#include stdio.h int main() {prinf("Welcome to Parallel-Programming Course!\n");}#include iostream int main() {std::cout "Welcome to Parallel-Programming Course!\n";}program helloprint *,"Welcome to Parallel-Programming Course!"end program hello

Shortest Makefile evermake progHelpful Make Command-Line Optionsmakemakemakemakemakemakemake-h-v-p-j 4-f MyMakefile-n-C path#######helpversionpre-defined rulesparallel 4 threadsif deviating from defaultdry-rungoto path before make

Different Settings (Compiler/Flags/.)make CC gccCFLAGS "-std c11 -O3 -g -Wall" progmake CXX g CXXFLAGS "-std c 17 -O3 -g -Wall" progmake FC gfortran FFLAGS "-std f95 -ffree-form" prog# C# C # Fortran

Makefiles

GNUmakefile, makefile, MakefileCC gccCFLAGS -std c11 -O3 -g -WallINCS -I./includeprog: prog.c (CC) (CFLAGS) (INCS) prog.c -o progJust enter makeRules - Explanation target : dependencies tab shell command Tab character mandatory (character can be changed via .RECIPEPREFIX)

Splitting the Build ProcessCC gccCFLAGS -std c11 -O3 -g -WallINCS -I./includeLIBS -lm# as harmless exampleprog: prog.o (CC) prog.o (LDFLAGS) (LIBS) -o progprog.o: prog.c (CC) (CFLAGS) (INCS) -c prog.c -o prog.oSome Simplification (DRY)[.]prog: prog.o (CC) (LDFLAGS) (LIBS) -o @prog.o: prog.c (CC) (CFLAGS) (INCS) -c -o @

More Complex Project[.]prog: prog.o mylib.o (CC) (LDFLAGS) (LIBS) -o @prog.o: prog.c mylib.h (CC) (CFLAGS) (INCS) -c -o @mylib.o: mylib.c mylib.h (CC) (CFLAGS) (INCS) -c -o @1 rule for each object that is generated out ofsome dependenciesfirst target (prog) is default targetcan also call make target

ExerciseWrite a small program with few (at least 3 including main) implementation (.c, .cpp,.f) and header files (.h, .hpp, or module files for fortran). You can also include someexternal (system) library if available to practice the inclusion of libraries.Think of how the dependency tree must look like (that's possibly more difficult withinFortran when using modules)!Write a Makefile, which compiles this program! Test the different flags and options ofmake! Also change the compiler and linker flags when calling make in order to observethe effect!10 minutes

Convenience Feature: .PHONY targetsCC gccCFLAGS -std c11 -O3 -g -WallINCS -I./includeLIBS -lm.PHONY: all cleanall: progprog : prog.o (CC) (LDFLAGS) (LIBS) -o @prog.o : prog.c (CC) (CFLAGS) (INCS) -c -o @clean :rm -rf prog.o prog * all is the default targetall could be used to default build several independent libraries and executables.PHONY targets can be used for built-up of a secondary (internal) dependency logic

Convenience Feature: Implicit (Generic) RulesCC gccCFLAGS -std c11 -O3 -g -WallINCS -I./includeLIBS -lm.PHONY: cleanprog: prog.o mylib.o (CC) (LDFLAGS) (LIBS) -o @%.o: %.c (CC) (CFLAGS) (INCS) -c -o @clean:rm -rf *.o prog * Helpful for non-standard source file endings (e.g. .cxx in C )Header dependency tree is more difficult to realize;simplest solution: if headers change make clean && make

Convenience Feature: Functions and @ Operator[.]SRC (wildcard *.c)OBJS (SRC:.c .o)# same as OBJS (patsubst %.c,%.o, SRC).PHONY: cleanprog: (OBJS) (CC) (LDFLAGS) (LIBS) -o @%.o: %.c (CC) (CFLAGS) (INCS) -c -o @clean:@rm -rf (OBJS) prog * @echo "Everything clean"Next to wildcard, lot of more functions available ( Docu)With @, shell command is not printed to screenThat's so far most generic Makefile (w/o header dependency)

And a lot of more advanced Features .Conventions and Standard Targets .

ExerciseImprove, i.e. shorten, your former attempt of a Makefile!10 minutes

CMake - Basic Usage

st1 Hands-On CMake Exercise (Warm-Up)Go to https://cmake.org/download and download the latest sources!tar xf cmake-3.16.3.tar.gzmkdir build && cd buildcmake ./cmake-3.16.3make -j 4##make VERBOSE 1 -j 4##make installmake help######out-of-source buildConfigurationCheck how many cores you have!!maybe helpful!!! Careful !!!can helpcmake executable configures the build, and creates a Makefilebuild is done out-of-source!5 minutes

Useful cmake Command-Line Optionscmakecmakecmakecmake-h--version-G .-D .####Help!VersionShow/Specify GeneratorSpecify variables (Compilers, Flags, .)Generators determine which build system will be supported (Default Linux: Unix Makefiles)cmake -G "CodeBlocks - Unix Makefiles"\-DCMAKE CXX COMPILER (which icpc) \-DCMAKE INSTALL PREFIX /usr\path-to-sourcepath-to-source can be absolute/relative; must contain a CMakeLists.txt file

"What the Hell .!", you may say"How should I remember all these flags and variables!?"You don't need to!(see also CMake Docu: cmake-variables)

Convenience Tool: ccmakeccmake path-to-source# or "ccmake ." if cmake already passed oncePage 0 of 1EMPTY CACHE[.]EMPTYPressPressPressPressCACHE:[enter] to edit option Press [d] to delete an entry CMake Version 3.13.4[c] to configure[h] for helpPress [q] to quit without generating[t] to toggle advanced mode (Currently Off)

Convenience Tool: ccmake (cont'd).CMAKE BUILD TYPEReleaseCMAKE COLOR MAKEFILEONCMAKE CXX COMPILER/usr/bin/c CMAKE CXX COMPILER AR/usr/bin/gcc-ar-8CMAKE CXX COMPILER RANLIB/usr/bin/gcc-ranlib-8CMAKE CXX FLAGS.BUILD CursesDialog: Build the CMake Curses Dialog ccmakePress [enter] to edit option Press [d] to delete an entry CMake Version 3.13.4Press [c] to configurePress [g] to generate and exitPress [h] for helpPress [q] to quit without generatingPress [t] to toggle advanced mode (Currently Off)

Convenience Tool: cmake-gui

The more important VariablesCMAKE INSTALL PREFIXCMAKE BUILD TYPECMAKE LANG COMPILERCMAKE LANG FLAGSBUILD SHARED LIBS#####path to install after build via "make install"none, Debug, Release, RelWithDebInfo, MinSizeRel, .compiler (CC, CXX, FC)compiler flags (CFLAGS, CXXFLAGS, FFLAGS)build shared libraries (.so, .dll) if ONCMAKE LANG COMPILER names the compiler; you can't change thelanguage! LANG can be C, CXX, Fortran, CUDA, .Can be used to e.g. set mpicc for MPI programs, or scorep for profiling/tracinginstrumentationDeveloper can add project specific variables ( CMakeLists.txt)

Now the Fun-Part:CMakeLists.txtFor those who want to go to a higher levelhttps://cmake.org/documentation latest CMake Tutorial

One File ProjectExercisePick your "Hello, World!"example from the Makefile part!Build one or all examples usingcmake/make!CMakeLists.txtcmake minimum required (VERSION 3.5)project (Tutorial C CXX Fortran)add executable (progcpp prog.cxx)add executable (progc prog.c)add executable (progf prog.f)Source file and CMakeLists.txt in the same directoryYou can select one language; C/C as default can beomittedSeveral targets (executables/libraries) possible

Standard Project - C/C #ifndef FUNC#define FUNCvoid print text(const char* text);#endif#include "func.h"int main() {print text("Hello");}How to extend to many filesshould be immediately clear!Good Thing: Header dependencytree is automatic!#include "func.h"#include iostream void print text(const char* text) {std::cout text '\n';}

Standard Project - C/C (cont'd)CMakeLists.txt for C (C same)cmake minimum required (VERSION 3.5)project (Tutorial)include directories(" {PROJECT SOURCE DIR}")add executable (prog prog.cxx func.cxx)in the project's top directoryExercisePut the header files in a include subfolder,and the other sources into a src subfolder!Adapt the CMakeLists.txt accordingly, andbuild the executable!Add more header and source files!Or use example projects!

Standard Project - FortranCMakeLists.txt for Fortranprogram hellouse funccall print text("Hello!")end program hellomodule funcimplicit nonecontainssubroutine print text(a)CHARACTER (LEN *), intent(in) :: awrite(*,*) aend subroutine print textend module funccmake minimum required (VERSION 3.5)project (Tutorial Fortran)set(CMAKE Fortran MODULE DIRECTORY {CMAKE BINARY DIR}/modules)add executable (prog prog.f func.f)ExercisePut the source into a src folder, and changeCMakeLists.txt!Add more source files!

External LibrariesFree Style (not portable)Preferred Style[.]include directories("path-to-header-files")add executable (prog .)target link libraries(prog fftw3)[.]find package(Boost 1.72.0 EXACT REQUIRED regex)include directories( {Boost INCLUDE DIRS})add executable(prog . )target link libraries(prog Boost::regex)C INCLUDE PATH andCPLUS INCLUDE PATH can be usedfinds libfftw3.soadd boost path to CMAKE PREFIX PATH( CMake Docu: find package)for header-only modules, regex andBoost::regex can be omitted

Project Internal LibrariesTop Level CMakeLists.txtinclude directories (" {PROJECT SOURCE DIR}/mylib")add subdirectory (mylib)set (EXTRA LIBS {EXTRA LIBS} mylib)add executable (prog .)target link libraries (prog {EXTRA LIBS})add subdirectory includesanother source directory with aCMakeLists.txt fileSub-Directory CMakeLists.txtadd library(mylib mylib.cxx)mylib.cxx and mylib.h both in subdirectory mylib relative to project top-levelfolderExercise: Realize a library solution with thestandard project code from above! TestBUILD SHARED LIBS!

Advanced CMake Features/Tools

CMake Configure-Time Code-ModificationCMake Tutorial: Steps 1/2cmake minimum required(VERSION 3.5)project(Tutorial VERSION 1.0)[.]option(USE MYLIB"Use MYLIB implementation" ON)[.]configure file(TutorialConfig.h.in TutorialConfig.h)[.]Source Code#ifdef USE MYLIB#include "MyLib.h"#endif[.]#ifdef USE MYLIBconst double outputValue mylib func();#endifTutorialConfig.h.inadds a project specific CMake variableUSE MYLIB, which can be ON or OFF#define Tutorial VERSION MAJOR @Tutorial VERSION MAJOR@#define Tutorial VERSION MINOR @Tutorial VERSION MINOR@

Install TargetsCMake Tutorial: Step 4[.]install(TARGETS mylib DESTINATION lib)install(FILES mylib.h DESTINATION include)install(TARGETS prog DESTINATION bin)install(FILES " {PROJECT BINARY DIR}/TutorialConfig.h"DESTINATION include)DESTINATION is relative toCMAKE INSTALL PREFIXgets relevant when executingmake installCMAKE BUILD WITH INSTALL RPATH ONcan be used to set RPATH for dynamic librarydependencies

Testing SupportCMake Tutorial: Step 4[.]enable testing()add test(NAME Runs COMMAND Tutorial 25)add test(NAME Usage COMMAND Tutorial)set tests properties(UsagePROPERTIES PASS REGULAR EXPRESSION "Usage:.*number")[.]a er successful build:make testctest# orFor unit, integration and generalfunction tests

CPack - Creating Install Packages CMake Tutorial: Step 7include(InstallRequiredSystemLibraries)set(CPACK RESOURCE FILE LICENSE " {CMAKE CURRENT SOURCE DIR}/License.txt")set(CPACK PACKAGE VERSION MAJOR " {Tutorial VERSION MAJOR}")set(CPACK PACKAGE VERSION MINOR " {Tutorial VERSION MINOR}")include(CPack)install(TARGET .) must be setcpack# Build in all Generators available in CPackConfig.cmakecpack -G TGZ# Build in TGZ (Tarball); ZIP, RPM, DEBcpack --config CPackSourceConfig.cmake # Source Tarball (out of source!!!)CPackConfig.cmake and CPackSourceConfig.cmake can/must be editedRPM and DEB require additional actions/tools (rpmbuild)

Setting Specific CMake VariablesFor instance, requiring C 17 standard conform compiler:set(CMAKE CXX STANDARD 17)set(CMAKE CXX STANDARD REQUIRED True)

CMake Scripting LanguageLearn CMake Scripting Language in 15 Minutesvariables, listsset(world "World")if-elseif-else constructsmessage("Hello, {world}!")file(GLOB sources "src/*.cxx")while, for each loopsmessage("Source Files: {sources}")functionsarithmetics is possible (math)

ReferencesGNU Make Documentation (PDF)CMake DocumentationCMake TutorialMastering CMake (PDF)CMake Fortran UseCMake Fortran SupportC Build ProcessShared Libraries: Understanding Dynamic Loading (Linux)

Press [enter] to edit option Press [d] to delete an entry CMake Version 3.13.4 Press [c] to configure Press [g] to generate and exit Press [h] for help Press [q] to quit without generating

Related Documents:

programming as well. In fact, while this guide is written and intended as an introduction to Octave, it can serve equally well as a basic introduction to MATLAB. What is GNU? A gnu is a type of antelope, but GNU is a free, UNIX-like computer operating system. GNU is a recursive acronym that stands for \GNU's not Unix." GNU Octave (and

bug-gnubg@gnu.orgor make abug report. All about GNU Backgammon iii COLLABORATORS TITLE : All about GNU Backgammon ACTION NAME DATE SIGNATURE WRITTEN BY Albert Silver and Christian Anthon July 23, 2018 REVISION HISTORY NUMBER DATE DESCRIPTION NAME All about GNU Backgammon March 2007 The GNU Backgammon Project All about GNU March 2007 Albert .

Introduction to GNU Radio Creating Gnu radio blocks Block behavior and Scheduler Message passing interface Table of contents 1 Introduction to GNU Radio What is GNU Radio GNU radio with gnuradio-companion . Debugged (with spectrum analyzer for instanc

Introduction to Gnu radio GNU radio with gnuradio-companion Creating Gnu radio blocks Block behavior and Scheduler Message passing interface Table of contents . Debugged (with spectrum analyzer for instance) Tanguy Risset Introduction to GNU Radio 13.File Size: 1MB

the GNU Radio libraries. The GNU Radio package is provided with a complete HDTV transmitter and receiver, a spectrum analyzer, an oscilloscope, a multichannel receiver . and a wide collection of modulators and demodulators. The user interface is called GNU Radio companion or GRC. GNU Ra

software environments as Matlab or GNU Radio. Figure 1. SDR with USRP and GNU Radio [10]. III. GNU RADIO GNU Radio [12] is an open-source toolkit that provides tools for development and simulation of SDR systems. It is used to design and execute algorithms that define a desired communication system. There are basically three ways to use GNU .

GNU is a Unix-like computer operating system developed by the GNU project. It is composed wholly of free software. It refers to GNU's Not Unix .GNU Project emphasizes on freedom and thus its logo type show a GNU, an animal living in freedom FSF: FSF is Free Software Foundation.

Jan 17, 2020 · the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, with the Front-Cover Texts being “A GNU Manual,” and with the Back-Cover Texts as in (a) below. A copy of the license is included in the section en