make tutorial
play

Make Tutorial Single source file code: g++ -g Wall main.cpp lm o - PDF document

Compiling programs Make Tutorial Single source file code: g++ -g Wall main.cpp lm o main Multiple sources [a,b] g++ -g c Wall a.cpp For : COP 3330. g++ -g c -Wall b.cpp Object oriented Programming (Using C++)


  1. Compiling programs Make Tutorial � Single source file code: � g++ -g –Wall main.cpp –lm –o main � Multiple sources [a,b] � g++ -g –c –Wall a.cpp For : COP 3330. � g++ -g –c -Wall b.cpp Object oriented Programming (Using C++) � g++ -g –o main a.o b.o -lm ht t p: / / www. com pgeom . com / ~pi yush/ t each/ 3330 Piyush Kumar Compiler flags Compilation � -c � Multi stage process Separate compilation flag. Will produce a ``.o'' file only, � g++ -g main.cpp –lm –o main without linking. • g++ -g -c main.cpp –o main.o � -g • g++ -g main.o main The application will have full debug capabilities, but almost no optimization will be performed on the � Compiling and Linking application, leading to lower performance. • Compilation : Produces object code � -Wall ( main.o ) Enable all warnings. • Linking : Produces executable by linking a � -o filename collection of object files and libraries. Write output to file. For more options: “man g++” A Typical Software Project Make � Has 10s to 100s of source files � Make is not tied to any particular � Multiple directories language. � Multiple authors � Make figures out automatically which � Flags : Differ with compilation files it needs to update, based on � Debugging flags (- g ) which source files have changed. � Optimization flags (- O6 – malign-double ) � Make enables the end user to build � Release Vs Test builds. � Make: A tool to automate the build process. and install your package without � Other cool build tools: CMake, knowing the details of how that is automake/autoconf, scons. done. 1

  2. Make and Makefiles An example � Main.cpp � Uses functions from other � “ make ” command reads “ makefile ” in the current directory for instructions source files and is the main program. for the build process. � Hello.cpp � Function definition. � If you want to give it a specific file for � Sumof.cpp � Function definition. input, say Makefile-1 use � Functions.hpp � Function � make –f Makefile-1 declarations. Download code from: http://www.compgeom.com/~piyush/teach/3330/examples/makex.tar.gz Use “ tar zxf makex.tar.gz” to untar files. An Example: Makefile-0 An Example: Makefile-0 � The basic makefile is composed of “rules”: � Running Make target: prerequisites � make -f Makefile-0 Other targets Or source files <tab> system command g++ main.cpp hello.cpp sumof.cpp -o hello � make � Makefile-0 <reads default: Makefile> all: <tab> g++ main.cpp hello.cpp sunof.cpp -o hello Default target for makefiles “ make –f Makefile-0 ” and “ make –f Makefile-0 all ” are equivalent. Targets, Prereqs and Another Example: Makefile-1 commands Dependencies � Makefile-1 � Target: is usually the name of a file that is generated by a program; examples of targets all: hello are executable or object files. A target can also hello: main.o sumof.o hello.o Command be the name of an action to carry out, such as g++ main.o sumof.o hello.o -o hello `clean' main.o: main.cpp g++ -c main.cpp Prerequisites / � A prerequisite is a file that is used as input to Dependencies. create the target. A target often depends on sumof.o: sumof.cpp g++ -c sumof.cpp several files. hello.o: hello.cpp � A command is an action that make carries out. g++ -c hello.cpp Wildcards. clean: rm -rf *o hello *.exe Put a tab before the command “ make –f Makefile-1 clean ” cleans up your directory except source code. 2

  3. Make Another Example: Makefile-2 Comments in makefile. � Only makes out of date prerequisites. � Makefile-2 # The variable CC will be the compiler to use. hello: main.o sumof.o hello.o Variables CC=g++ g++ main.o sumof.o hello.o -o hello # CFLAGS will be the options I'll pass to the compiler. CFLAGS=-c -Wall -g all: hello � How to decide whether “hello” is out hello: main.o sumof.o hello.o Using $(CC) main.o sumof.o hello.o -o hello Variables of date? main.o: main.cpp $(CC) $(CFLAGS) main.cpp � It is out of date if it does not exist, sumof.o: sumof.cpp or $(CC) $(CFLAGS) sumof.cpp � if either main.o , sumof.o or hello.o are more recent than it. hello.o: hello.cpp $(CC) $(CFLAGS) hello.cpp clean: If “hello” is out of date, make executes the command ‘g++ main.o …’ rm -rf *o hello *.exe Makefile targets Make: How does it work? � make reads the makefile in the current � Expected targets in makefiles directory and begins by processing � make all the first rule. (in our case: all) • Compile everything. � but before make can fully process this � make install rule, it must process the rules for the • Install your software. files that ‘all’ depends on, which in this � make clean case are the object files. • Clean intermediate files and executables. � Each of these files is processed according to its own rule. Make: How does it work? Make: Wrap up � For any rule, the recompilation must � make –j2 be done if the prerequisites are more � Uses 2 processors for the build process. recent than the target, or if the � More info: target/object file does not exist. � http://www.gnu.org/software/make/manual/ht ml_node/index.html � man make � Example : http://www.compgeom.com/~piyush/teach/33 30/examples/makex.tar.gz 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend