Posts

Compiling, linking, Makefile, header files

   Compiling, linking, Makefile, header files Splitting your program into multiple files The programs we have seen so far have all been stored in a single source file. As your programs become larger, and as you start to deal with other people's code (e.g. other C libraries) you will have to deal with code that resides in multiple files. Indeed you may build up your own library of C functions and data structures, that you can re-use in your own scientific programming and data analysis. Here we will see how to place C functions and data structures in their own file(s) and how to incorporate them into a new program. We saw in the section on functions, that one way of including custom-written functions in your C code, is to simply place them in your main source file, above the declaration of the  main()  function. A better way to re-use functions that you commonly incorporate into your C programs is to place them in their own file, and to include a statement above  main()  to  include

fatal error: stdlib.h: No such file or directory 4 | #include

make file for compiling c and c++ in linux

  Let's understand with an example: Suppose, we have 3 files       main.c  (main source file),      misc.c  (source file that contains function definition),       misc.h  (that contain function declaration).  Here we will declare and define a function named  myFunc()  to print something –  this function will be defined and declared in  misc.c  and  misc.h  respectively. misc.c 1 2 3 4 5 6 7 8 #include <stdio.h> #include "misc.h"   /*function definition*/ void myFunc( void ) {      printf ( "Body of myFunc function.\n" ); } misc.h 1 2 3 4 5 6 7 #ifndef MISC_H      #define MISC_H             /*function declaration.*/      void myFunc( void );        #endif main.c 1 2 3 4 5 6 7 8 9 10 11 #include <stdio.h> #include "misc.h"   int main() {      printf ( "Hello, World.\n" );      myFunc();      fflush (stdout);        return 0; } Makefile to compile these files 1 2 3 4 #make file - this is a comment section   all:    #target name