Reputation: 109
I am trying to compile and create a Makefile with my source code organized as per the following structure:
|-- build
|-- include
| \-- functions.h
\-- src
|-- functions.c
\-- main.c
The code written there has been tested on an IDE, so I know it works. I just need to compile it but I don't know how to execute 2 files with extensions .c and the .h file. ALSO, I need to create a makefile for this.
How?
Upvotes: 1
Views: 852
Reputation: 1
A Makefile
is just some textual file (where tab characters are significant at least when they start a command line). Use some source code editor to create or improve it; I prefer GNU emacs so recommend it, but you could use some other editor (e.g. vim, gedit
, etc...), it is a matter of taste. BTW, don't forget to use some version control system (I recommend git).
Before creating your Makefile
take some time to read the documentation of make
. Be aware of the numerous built-in rules (which you could obtain by running make -p
) and take advantage of them.
how to execute 2 .c files and the .h file.
You don't execute .c
files, but you feed them to your C compiler. I recommend using GCC (or perhaps Clang/LLVM) as your C compiler.
You probably should set the CC
make variable (it gives the default C compiler) to gcc
, and the CFLAGS
variable (it gives default compiler flags).
So read documentation about Invoking GCC. You surely want to compile with all warnings and debug info.
Hence, I recommend to have the following two lines in your Makefile
CC= gcc
CFLAGS= -Wall -Wextra -g
Of course, you need other lines in your Makefile
. I leave you to read more and experiment. This and that should be inspirational.
You probably want to mention the dependency on your header file(s) in your Makefile
.
So I leave you to write your Makefile
. Consider it as source code, so backup and version control it carefully.
BTW, for a tiny program like yours, I don't think that having src/
and include/
directories is useful, and I believe that introducing a build/
directory is just adding unnecessary complexity. YMMV.
The code written there has been tested on a IDE, so I know it works.
This could be an illusion. Sometimes you can have the bad luck of having some undefined behavior which simply "appears" to work but is a bug. See this answer and follow the links I gave there.
There are many free software projects built with make
; look on github and elsewhere to find some. Sometimes Makefile
s are themselves generated (e.g. using cmake, qmake, or autoconf), but that is not worthwhile in your simple case.
PS. Don't expect StackOverflow to do your homework.
Upvotes: 1