How to write makefile for C program in UNIX - c

I have a program test.c which needs to include a header file common.h. I am new to writing make files and so far I have the right make file for just test.c (it uses openssl):
INC=/usr/local/ssl/include/
LIB=/usr/local/ssl/lib/
all:
gcc -I$(INC) -L$(LIB) -o test test.c -lcrypto -ldl
How do I edit the above file to also compile common.h ?
Thanks.

You don't compile header files directly. They will be included by #include in the C files. If common.h is located in the same directory as the make file, you won't need to add anything. Otherwise, you might want to edit the makefile to add other folders that include header files:
INC=-I/usr/local/ssl/include/ \
-I/path/to/another/folder/to/include
LIB=/usr/local/ssl/lib/
all:
gcc $(INC) -L$(LIB) -o test test.c -lcrypto -ldl

Related

How do you link two files in make, that both include a different library?

I want to test my code in test.c which includes main.h with all the function declarations from main.c. I want to compile my test.c file with criterion, which on its own works fine like this: cc -c test.c -o testing -I include -L lib -l criterion.3.1.0 and I compile my main.c in the same way and it works fine: cc main.c -o output -I include -L lib -l SDL2-2.0.0. But when I want to include a function from main.c in test.c it gives me a linking error.
The project architecture is like this:
main.c
#include "main.h"
int function1(){
[...]
}
test.c
#include "main.h"
#include <criterion/criterion.h>
Test(sample,test_function1){
cr_assert(function1() == 1);
}
main.h
#include <SDL2/SDL.h>
extern int function1();
How do I have to compile/link these files in order to have my test-file use get access to the functions from main.c?
What you are currently doing is compiling and linking the binary in a single command for each source file in your project. The linker, with how you are doing this, is not aware of your other sources. Instead, you should first build the object files for each source file, and then you can link those together to form your binary.
Building object files:
cc -c main.c -I include -o main.o
cc -c test.c -I include -o test.o
Linking:
cc -o testing main.o test.o -Llib -lcriterion.3.1.0 -lSDL2-2.0.0

How can I compile a header file and a C file together?

I created a file.h and a file.c how can I compile them on Ubuntu?
You only need to compile your .c file(s), not your .h file(s).
To compile file.c on Ubuntu, you can use GCC:
gcc file.c -o my_program
...or Clang:
clang file.c -o my_program
It is possible to precompile your header files, but you only need precompiled headers in particular cases. More information here.
If file.h is not in the same folder as file.c, you can use GCC or Clang's -I option.
Example if file.h is in the include/ folder:
gcc -I include/ file.c -o my_program
In file.c you still have this instruction, with only the filename:
#include "file.h"
You can also use a more generic approach by the usage of a makefile.
Here is a short example of such a file:
# Declaration of variables
CC = gcc
CC_FLAGS = -w -Werror -Wall
# File names
# "prgoram" will be the name of the output produced from the make process
EXEC = program
#Incorporates all the files with .c extension
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
# Main target
$(EXEC): $(OBJECTS)
$(CC) $(OBJECTS) -o $(EXEC)
# To obtain object files
%.o: %.c
$(CC) -c $(CC_FLAGS) $< -o $#
# To remove generated files
clean:
rm -f $(EXEC) $(OBJECTS)
To use this utility just make sure that the file itself is within the directory containing your source files and its name is either "makefile" or "Makefile".
To compile the code simply run the following command from your working directory:
make program
This command will automatically link all the source files within your working directory into one executable file with the name of "program". To run the program itself just use the command:
./program
To clean your project and the created executable you can run the command:
make clean
The makefile is very powerful when dealing with larger projects that contain a larger number of source files. Here you can check for more guidance on how to use makefiles. This is also a very detailed tutorial on the topic.
Use following command to compile your program(For GCC Compiler):
gcc file.c -o file
No need to compile file.h file.

How do I create Makefile for this

Here is my header file
#include <stdio.h>
#include <stdlib.h>
#include "gd.h"
#include "gdfontmb.h"
#include "gdfontl.h"
#include "gdfontg.h"
When I run this program I usually type 'gcc -o test test.o -lm -lpng -lgd'
It works fine for only one .c file, but this is just for testing. I want to link this with others c file in my project (Actually I'm really new to use gd.h)
Here is my Makefile (but It isn't work!!)
ifeq ($(OSTYPE),WINDOWS)
EXECEXT =.exe
COMP =__MINGCC__
PLATFORM =mingw
else
EXECEXT =
COMP =__GCC__
PLATFORM =linux
endif
EXECUTABLES= test$(EXECEXT)
all : $(EXECUTABLES)
test.o : test.c
gcc -c test.c
test$(EXECEXT) : test.o
gcc -o test$(EXECEXT) test.o -lm -lpng -gd
clean :
-rm *.o
-rm $(EXECUTABLES)
Using this Makefile, I got all error about undefined reference to whatever that are in the gd library.
What did I do wrong and How can I fix this?
Your own cc command already gives the answer. You need -lgd, not -gd.
E.g. set in the start:
LIBS=-lm -lpng -lgd
CC=gcc
(the latter can be the full OS-dependent path as well, and then the CC should be part of the OS specific part, and be specified as a full path).
and change the gcc line later to
$(CC) -o test$(EXECEXT) test.o $(LIBS)
And the rule for test.o is (usually) not really needed, as it is a default way to make a .o file from a .c file.

Source file cannot find the header files

I have looked at these links : This one
and This
and a couple of other similar ones.
None of the answers given here are working methods are working.
I have a two source files a1.c , a2.c and two header files a1.h and a2.h . I want to include the header files in both these files (and a2.c in a1.c as there is a function I need to use from a2.c)
I have included
#include "a1.h"
#include "a2.h"
in the source files of a1.c
I'm using GCC on Ubuntu. and using the command gcc a1.h -o a1.out -lm
and that didn't work.
I tried with
gcc -c -I/Home/Documents/ctests/ a1.c -o a1.out
as well as
gcc -c a1.c -I/Home/Documents/ctests/ -o a1.out
My spellings are okay as well (there's hardly any room for error there with one letter and a number as the filename anyway).
Also, all the files are in the same folder.
I know this may be a trivial question but I am stuck on this one and would appreciate any help. I am relatively new to programming and completely new to Linux and Unix as far as using the command line goes.
Many thanks!
gcc -c
tells gcc to compile the file to object (the .o files you see everywhere). To be linked later with some other .o files to an executable.
So what you want to do is either compile the two files separately and link them later. like this.
gcc -I"/Home/Documents/ctests/" -c a1.c
gcc -I"/Home/Documents/ctests/" -c a2.c
gcc -o myprogram a1.o a2.o
Or just compile and link at the same time.
gcc -I"/Home/Documents/ctests/" a2.c a1.c -o myprogram
And then run your program like
path_to/myprogram
Compile everything, and link it together.
If all files are in one directory, this should work:
gcc a1.c a2.c -o myapp
When you want to create separate object files, do this:
gcc -c a1.c a2.c
Then you can then link together to create an application:
gcc a1.o a2.o -o myapp
Your gcc command should be like this
gcc -I/Home/Documents/ctests/ -o a1.out a1.c
and you have to include a1.h and a2.h header file in your a1.c like this
#include "a1.h"
#include "a2.h"
If you are calling some function from a2.c in your a1.c then you have to build your program in this way
gcc -I/Home/Documents/ctests/ -o a1.out a2.c a1.c

C header files and dynamic linking error

I have created a dynamically linked library. The only problem I have is that my main program does not recognize my header file. The header file is in a separate folder from my main program. I have tried #include "myheader.h" as well as #include "/folder/dir/myheader.h"
Here is what my .h consist of
extern int afunction(int,int);
extern int afunction(int,int);
So far this code works
gcc -fPIC -c filename1.c
gcc -fPIC -c filename2.c
gcc -shared -o libMylib.so filename1.o filename2.o
I then copy the lib to /usr/local/lib, and then
gcc main.c -L. -lMylib -o exeName -ldl
and I get
" myheader.h : no such file or directory.
Here is my directory structure:
directory1 ----------------folder1(main program.c)
directory1 ----------------folder2(myheader.h, along with functions for the pgm)
A push in the right direction would help, as I have written all my code and I am just at the last phase.
You need gcc ... -I<some directory to myheader.h>. That will enable the compiler to find the header file.
You can put your lib header files in the same folder with your current program.
or like #Ed Heal said.. adding -I<path> for include header folder.

Resources