Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple source files in C- How exactly do makefiles work?

Tags:

c

file

makefile

I'm a relative beginner to C and I need to learn how makefiles work and I'm a bit confused on how the combination of C files work. Say we have a main.c, a foo.c, and a bar.c. How should the code be written so that main.c recognizes functions in the other files? Also, in foo.c and bar.c, is all of the code written in the main function there or do we need to write other functions for what we need them to do? I've read tutorials on how makefiles are written, and it makes sense for the most part, but I'm still a bit confused on the basic logistics of it.

like image 502
Aeonstrife Avatar asked Dec 03 '10 20:12

Aeonstrife


People also ask

How do makefiles work C?

Makefile is a set of commands (similar to terminal commands) with variable names and targets to create object file and to remove them. In a single make file we can create multiple targets to compile and to remove object, binary files. You can compile your project (program) any number of times by using Makefile.

What do makefiles do?

Makefile sets a set of rules to determine which parts of a program need to be recompile, and issues command to recompile them. Makefile is a way of automating software building procedure and other complex tasks with dependencies. Makefile contains: dependency rules, macros and suffix(or implicit) rules.

What relationships between files does the makefile define?

The specification file, or makefile, describes the relationship between the source, intermediate, and executable program files so that make can perform the minimum amount of work necessary to update the executable.

Does the order of makefiles matter?

The order of rules is not significant, except for determining the default goal: the target for make to consider, if you do not otherwise specify one. The default goal is the target of the first rule in the first makefile. If the first rule has multiple targets, only the first target is taken as the default.


1 Answers

Generally what will happen is you will define your functions for the other files in a header file, which can then be included in main.c. For example, consider these snippets:

main.c:

#include "foo.h"  int main(int argc, char *argv[]) {     do_foo();     return 0; } 

foo.h:

void do_foo(); 

foo.c:

#include <stdio.h> #include "foo.h"  void do_foo() {     printf("foo was done\n"); } 

What will happen is that main.c will be turned into an object file (main.o), and foo.c will be turned into an object file (foo.o). Then the linker will link these two files together and that is where the do_foo() function in main.c is 'associated' with the function in foo.o.

Example GCC command: gcc -o myprogram main.c foo.c

Example makefile

myprogam: main.o foo.o     gcc -o myprogram main.o foo.o  main.o: main.c foo.h     gcc -c main.c  foo.o: foo.c foo.h     gcc -c foo.c 
like image 54
Gavin H Avatar answered Sep 28 '22 03:09

Gavin H