gookrot.blogg.se

How to create makefile for multiple files
How to create makefile for multiple files





how to create makefile for multiple files

  • Suppose you have a program, which is made up of these 5 source files: main.c (include data.h, io.h) data.c (include data.h) data.h io.c (include io.h) io.h.
  • o file needs to be regenerated then everything relinked o file is OK (unchanged source), leave it.
  • We can save time by only updating (compiling) the parts that need updating.
  • We need a better way to control compilation.
  • HOW TO CREATE MAKEFILE FOR MULTIPLE FILES CODE

    Can distribute pieces to other so they can incorporate into their code without changing their code (just link it in)Įxample (Separate Compilation) mainprog.c shapes.h shapes.c math.h mainprog gcc -c shapes.c gcc –c mainprog.c gcc –lm mainprog.o shapes.o –o mainprogĪnother way (Compile all at once) mainprog.c shapes.h shapes.c math.h mainprog gcc –lm shapes.c mainprog.c –o mainprog.For large, complex programs, it is a good idea to break your files into separate.Linking is important as it allows multiple, separate files to be brought togetherĮxpanded C code Linking preprocessor gcc -E Compiler gcc -c Machine Code (.o file) C source file Linker Executable gcc –lm code.o –o code Library Files.Resolves references to calls from one file to another.The linker (ld) takes multiple object files (.o) and puts them together into one executable file.

    how to create makefile for multiple files

    Object file may not be directly executable.To create the assembly code: gcc -c preproc.c C source file.Each line represents either a piece of data or a machine-level instruction.Compiler translates the C source code into object code – what the machine actually understands.solve.h: #ifndef SOLVE_H #define SOLVE_H #include “hanoi.h” void Autosolve(int tower) #endifĬompiling Expanded C code Object Code (.o file) gcc –c code.c Compiler preprocessor (# commands) Hanoi.c: Include Guards #include #include #include #include “solve.h” #include “hanoi.h” bool CheckDone(int tower) hanoi.h: #ifndef HANOI_H #define HANOI_H #define NumDisks 6 #define NumPins 3 #endif Include Guards: Conditional compilation code that protects a section of code in a header from being compiled more than once. Hanoi.c: What would happen? #include #include #include #include “solve.h” #include “hanoi.h” bool CheckDone(int tower) hanoi.h: #define NumDisks 6 #define NumPins 3 solve.h: #include “hanoi.h” void Autosolve(int tower) TT C Hanoi.c: Example #include “hanoi.h” bool CheckDone(int tower) #if 0 bool DebugStuff(int x, int b) #endif hanoi.h: #define NumDisks 6 #define NumPins 3 TT B What does gcc do? code.c #include #include #define NumIter 5 int main() In particular, we would like to be able to make multiple, separate programs which can be combined into one executable Gcc is not a single program gcc is a conglomeration of programs that work together to make an executable file We need to know what it does and how we can better control it. Take an existing file, split it into multiple separate programs, and write a Makefile to simplify compilation of the files.What are the different switches available?.







    How to create makefile for multiple files