Multiple File Compilation and Linking
This document explains how to compile and link multiple C source files using pthreads along with custom headers. It includes a sample code demonstrating the integration of two headers, `mars.h` and `venus.h`, and showcases basic threading and function definitions. The program calculates the square of a number using `doA` from `mars.h` and executes a threaded function `doB` from `venus.h`. Students and developers will learn the structure of multi-file projects, linking libraries, and handling threads in C.
Multiple File Compilation and Linking
E N D
Presentation Transcript
Multiple File Compilation and Linking pthread.h stdio.h mars.h venus.h #ifndef STDIO_H #define STDIO_H #endif #ifndef PTHREAD_H #define PTHREAD_H #endif #ifndef MARS_HEADER #define MARS_HEADER #define MIN 1000 int doA(int nbr); #endif #ifndef VENUS_HEADER #define VENUS_HEADER typedef struct { // fields } gravityType; void doB(char symbol); #endif programX.c #include <stdio.h> #include <pthread.h> #include “mars.h” #include “venus.h” main() { gravityType value; pthread_t threadID; int result; printf(“Hello”); result = doA(5); doB(‘d’); } // End main mars.c venus.c #include <stdio.h> #include “mars.h” int doA(int nbr) { return nbr * nbr; } // End doA #include “venus.h” #include <pthread.h> #define MAX 100 void doB(char symbol) { pthread_t threads[MAX]; } // End doB programX.o libc.a libpthread.a mars.o venus.o Object code file The Standard C Library Threads library Object code file Object code file a.out gcc programX.c mars.c venus.c Executable File