C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.

Slides:



Advertisements
Similar presentations
The make Utility Programming Tools and Environments Winter 2006.
Advertisements

Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
Makefiles  Provide a way for separate compilation.  Describe the dependencies among the project files.  The make utility.
David Notkin Autumn 2009 CSE303 Lecture 20 static make.
Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
1 CSSE 332 Standard Library, Storage classes, and Make.
1 CS 201 Makefile Debzani Deb. 2 Remember this? 3 What is a Makefile? A Makefile is a collection of instructions that is used to compile your program.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Basic linux shell commands and Makefiles. Log on to engsoft.rutgers.edu Open SSH Secure Shell – Quick Connect Hostname: engsoft.rutgers.edu Username/password:
Guide To UNIX Using Linux Third Edition
CS465 - Unix C Programming (cc/make and configuration control)
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
Makefiles, and.h files, and.c files, and.o files, OH MY! For projects with more complexity. (Great.. Just what we needed)
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
TAMU CSCE 313 (the basics). Basic Unix/Linux programming Accessing CS systems  PuTTY (putty.exe) – a Telnet and SSH client  Common hosts: unix.cs.tamu.edu.
Scons Writing Solid Code Overview What is scons? scons Basics Other cools scons stuff Resources.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Chapter Ten g++ and make1 System Programming Software Development: g++ and make.
C Tutorial - Program Organization CS Introduction to Operating Systems.
Makefile M.A Doman. Compiling multiple objects Card.cpp -> Card.o Deck.cpp -> Deck.o main.cpp -> main.o main.o Deck.o Card.o -> Dealer.exe.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Separate Compilation make and makefiles
CSE 251 Dr. Charles B. Owen Programming in C1 Compilation and Makefiles.
Data Display Debugger (DDD)
A brief introduction to javadoc and doxygen. What’s in a program file? 1. Comments 2. Code.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Modular Programming. Introduction As programs grow larger and larger, it is more desirable to split them into sections or modules. C allows programs to.
Lecture 8  make. Using make for compilation  With medium to large software projects containing many files, it’s difficult to: Type commands to compile.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
Emacs, Compilation, and Makefile C151 Multi-User Operating Systems.
Make Make is a system utility that automatically compiles your programs for you Make looks for a file named Makefile (or makefile) in the current directory.
Makefiles1 MAKEFILES Purpose: contain UNIX commands and will run them in a specified sequence. Syntax Definition : { Section-name: {unix command #1} …
C P ROGRAMMING T OOLS. C OMPILING AND R UNNING S INGLE M ODULE P ROGRAM.
Announcements Assignment 1 will be regraded for all who’s score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.
Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects can become huge My research consists of 1600.
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
Makefiles CSSE 332 Operating Systems
The make utility (original presentation courtesy of Alark Joshi)
Automating Builds with Makefiles
CSE 303 Lecture 17 Makefiles reading: Programming in C Ch. 15
Compilation and Debugging
Compilation and Debugging
Makefiles Caryl Rahn.
SCMP Special Topic: Software Development Spring 2017 James Skon
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Introduction to javadoc
Makefiles and the make utility
Makefiles and Notes on Programming Assignment PA2
Computer Science 210 Computer Organization
Separating Definition & Implementation
Data Structures and Programming Techniques
CMPSC 60: Week 4 Discussion
SCMP Software Development Spring 2018 James Skon
Introduction to javadoc
Writing Large Programs
Build Tools (make) CSE 333 Autumn 2018
Makefiles and the make utility
Compiler vs linker The compiler translates one .c file into a .o file
CSC 253 Lecture 15.
Makefiles, GDB, Valgrind
SCMP Software Development Spring 2018 James Skon
The make utility (original presentation courtesy of Alark Joshi)
Presentation transcript:

C code organization CSE 2451 Rong Shi

Topics C code organization Linking Header files Makefiles

#include #include works with two types of files – Library files #include – Local files #include “filename” #include “directory/subdirectory/filename” By convention, the names of the standard library header files end with a.h suffix Location of library files – /usr/include

C code organization

Working with multiple files

Compiling The -c option on the gcc command compiles to object code Then gcc with -o option merges object files into one executable file Note that “makefunction.h” is only referred to in a #include statement, never in a compilation command

Another example – circular #includes main.c #include “a.h” #include “b.h” void main() { a(10); } a.h #include #include “b.h” void a(int num) { if(num != 0) { num = b(num-1); printf(“%i”, num-1); } b.h #include #include “a.h” int b(int num) { if(num != 0) { num = a(num/2); printf(“%i”, num); return num; } return 0; }

Conditional inclusion #ifndef HEADER_FILE_NAME_H_ #define HEADER_FILE_NAME_H_ // contents of header_file_name.h goes here // … #endif

Makefile overview Makefiles are a commonly used tool to handle compilation of large software projects in UNIX Makefiles contain UNIX commands and will run them in a specified sequence Mandatory makefile file names: makefile or Makefile You can only have one makefile per directory Anything that can be entered at the UNIX command prompt can be in the makefile Each command must be preceded by a TAB and is immediately followed by a carriage return To execute, be in the directory where the makefile is: – % make tag-name (also called section name) – examples: make make all make clean

Makefile example # this line is a comment all: mainprog mainprog: makefunction.o mainprog.o gcc makefunction.o mainprog.o -o mainprog makefunction.o: makefunction.c gcc -c makefunction.c mainprog.o: mainprog.c gcc -c mainprog.c clean: rm -rf *.o mainprog

Makefile details We could compile our example like this: – gcc -o mainprog mainprog.c makefunction.c But what if we made changes to only makefunction.c? A makefile is often composed of lines with the following format: – target: dependencies [tab] system command – The make program uses the makefile data base and the last-modification times of the files to decide which of the files need to be updated. – make with no arguments executes the first rule in the file.

Makefile dependencies Useful to use different targets – Because if you modify a single project, you don’t have to recompile everything, only what you modified In the example makefile: – All has only dependencies, no system commands – For make to execute correctly, it has to meet all the dependencies of the called target – Each of the dependencies are searched through all the targets available and executed if found. – make clean Get rid of all the object and executable files – Free disk space – Force recompilation of files

Additional information on makefiles make.html make.html Variables in makefiles