Effective C++ Compilation with Makefiles and G++ Features

recitation 1 cop4530 n.w
1 / 7
Embed
Share

Learn about compiling C++ programs using G++, creating efficient makefiles, and utilizing useful flags for better code management. Explore examples and advanced techniques to enhance your C++ development workflow.

  • C++
  • Compilation
  • Makefiles
  • G++
  • Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. RECITATION 1 COP4530 g++ features, better makefiles

  2. Compiling C++ programs Single-file programs g++ prog1.cpp Multiple-file programs To invoke the compile stage: g++ -c <file_name> Example: g++ -c frac.cpp g++ -c main.cpp To invoke the linking stage: g++ -o <target_name> <object_files> example: g++ -o frac frac.o main.o (run the program: frac)

  3. Some useful flags -std=c++11 (flag for using c++11 standard features) -Wall (all warnings about construction and some language- specific warnings) -pedantic (Enables warnings demanded by ISO C/C++) -I (including directories to search for header files) -c (compile only) -o <filename> (output rename)

  4. Makefiles Filename: makefile or Makefile It consists of several sections: <target_name>: <dependency list> <TAB><commands> Any line that starts with a # character is a comment.

  5. Make file example # This is a comment line frac: main.o frac.o g++ -o frac main.o frac.o main.o: main.cpp frac.h g++ -c main.cpp frac.o: frac.cpp frac.h g++ -c frac.cpp clean: rm *.o frac

  6. Two ways to invoke commands: make : only the first target make <target_name> Clean section: Removing the old object files To invoke: make clean

  7. More advanced makefile HOME = /home/courses/cop4530/recitation CC = g++ -Wall -pedantic PROJ = $(HOME)/rect2/makeutil INCL = -I $(PROJ) all: main.x main.x: largest.o print.o main.o $(CC) -o main.x print.o largest.o main.o largest.o: $(PROJ)/largest.h $(PROJ)/largest.cpp $(CC) -c $(INCL) $(PROJ)/largest.cpp print.o: $(PROJ)/print.h $(PROJ)/print.cpp $(CC) -c $(INCL) $(PROJ)/print.cpp main.o: $(PROJ)/main.cpp $(CC) -c $(INCL) $(PROJ)/main.cpp clean: rm -rf *.o *~ *.x

More Related Content