Link a Headerfile without c file - c

I want to link a headerfile which has no corresponding c-file.
How can I do that this is my current Makefile.
CC=powerpc-linux-gcc
LIBS = libdrmko.o libdrm.o libs3gko.o
C_SRC = main.c lstLib.c vxTypes.h
C_OBJ = main.o lstLib.o vxTypes.o
graphics_test: $(LIBS) $(C_OBJ)
$(CC) -o graphics_test $(LIBS) $(C_OBJ)

You do not link header files, you include them in your relevant C files:
#include "myheader.h"
See GCC docs.

Header files are normally #include'd (similar to #include <stdio.h>). If the header file only contains prototypes, but not actual implementation, whatever you do you will end up with compilation errors.

I assume you mean you want to get some header files into your project that are not in your .c files as #includes.
If your .h files are in a directory appart from the source you can include it with:
CFLAGS=-I/directory/to/add
and you'll pick up all the .h files in there.

Related

Makefile with two .c files and no header

Trying to write a makefile with two .c files and no header. However, it seems examples online have shown the makefile with a header.
So i tried to write a header file and it would tell me a function is redefined somewhere else. my header consisted of declarations of functions in both my .c files.
#ifndef HEADER_H
#define HEADER_H
void calc(void parameters);
int main(int argc, char* argv[]);
struct compArray
{
int start;
int end;
int thr;
int m;
int r;
};
#endif
I'm positive that's not how you write a header but ideally I'd like to have my makefile without a header. Below is my Makefile:
all: thr_atomic.o thr_reduce.o
gcc -o make thr_atomic.o thr_reduce.o -lm -pthread
thr_atomic.o: thr_atomic.c
gcc -c thr_atomic.c
thr_reduce.o: thr_reduce.c
gcc -c thr_reduce.c
Is it possible to create a makefile without a header? My two .c files are independent of each other. If so how do I go about doing that? And if not, what would I put in my header.h to tell the computer that my variables are not being redefined and are independent from each other?
You can use the following Makefile:
all: thr_atomic thr_reduce
thr_atomic: thr_atomic.c
gcc thr_atomic.c -lm -pthread
thr_reduce: thr_reduce.c
gcc thr_reduce.c -lm -pthread <newline>
However, since it seems the two programs are totally independent, I would rather make two separate makefiles.
From what I can understand from your question, you don't need a header file in order to compile C code using a Makefile. Makefiles don't have anything to do with source code. All a Makefile does is run a command(s) if the file(s) you've specified have been updated.
# Makefile
outfile: infile1 infile2
command1
command2
# ^ commands MUST begin with a tab
If you run make outfile, this is what happens:
If outfile does not exist, then make runs command1, and then runs command2.
If outfile exists, but infile1 and infile2 have not been updated since then, make will do nothing.
If outfile exists and infile1 or infile2 have been updated since outfile was last modified, then make will run command1 and then run command2.
That's the basics of make and Makefiles. The files (outfile, infile1, infile2 etc.) and the commands (command1, command2) can be anything you like. There can also be any number of them present.
When it comes to compiling code, make is pretty useful because you can use it to recompile a binary file (also called an executable file) if the source files are changed, which is exactly what you want.
For example, this makefile creates a binary file (executable file) from two source files (and no header files):
# Makefile
# executable file is called "prog"
prog: a.o b.o
gcc -o prog a.o b.o
# ^^^^^^^
# tell gcc to name the output file "prog"
# compile the first source file "a.c" to produce the object file "a.o"
a.o: a.c
gcc -c a.c
# compile the second source file "b.c" to produce the object file "b.o"
b.o: b.c
gcc -c b.c
If you do have header files, then you would need to change the compilation lines to this:
# Makefile
# ...
# compile a.c, which has an associated header file a.h
a.o: a.c a.h
# ^^^ add the header file here
gcc -c a.c
If you are not clear about what header files are, then look them up somewhere or ask another question about them.

How do you indicate relative (or absolute) path for pattern rule in makefile? and How do you detect included headers from a source file?

We can add pattern rules such as %.c or %.o which mimics *.c or *.o in bash (it searches for all files that have extension .c or .o. This is very useful if you have several targets, so you don't have to write all rules. I would like to know how to use this trick if your target files (.c or .o) are in the previous directory. In bash, one can write ../*.c, but ../%.c does not work in makefile as I tested. How do you do such thing in makefile?
My second question: sometimes one would like to add header dependencies like this:
HEADER=factorial.h
%.o: %.c $(HEADERS)
gcc -o program $%
It is a good idea to add a header dependency because sometimes you don't know whether or not the included libraries have some change.
Here we have to manually type the file names for HEADER.
How do I make it so it can scan the target file's included headers?
For example: my main.c has #include "dog.h"
How do I make it so it detects main.c has included dog.h.

Makefile for Multiple .c and .h files

I am having a 1.c 2.c....n.c files and having its dependencies .h file also... i know to create make file for multiple c files.But i don't how to create make file for which the c files are linking to .h files. If i try the makefile which i know it will give error like
make: *** No rule to make target '2.h', needed by '2.o' .Stop.
and I don't need this type of makefile also.
program: main.o dbAdapter.o
gcc -o program main.o dbAdapter.o
main.o: main.c dbAdapter.h
gcc -c main.c
dbAdapter.o dbAdapter.c dbAdapter.h
gcc -c dbAdapter.c
This will be good for 4 or 5 files. But if I have a large number of files, what is the best solution?
You can link all your .h in the Makefile by this way :
Put all the .h in a same file (that we called "Include" for the exemple)
Add this in your Makefile : gcc *.c -I/path/Include -iInclude
Ps: Your way to compile your .c file is a bit strange.
Usually we use this:
SRC = 1.c
2.c
n.c
OBJ = $(SRC:.c=.o)
all: $(OBJ)
gcc $(SRC) -I/path/Include -iInclude (where path is the location of your file called "Include")
As long as I'm working with C, I never wrote make files that includes header files (.h) the header files are here to expose some of the data structure and methods, constants that are needed in other C modules.
You don't need to create rules for header files, all you have to do is build the .o objects then the liker will do the magic for you when you create the executable file.
If you need some help crating a make file you can explain here what you wanna build and I'll send you a hint.
Cheers.
If your header files are not in current directory and you included it in Makefile, Make starts looking for header files in default location but it is not able to find them in your case.
you should put 2.h header files in current directory to avoid this search.

How to link multiple implementation files in C

I have a number of .c files, i.e. the implementation files say
main.c
A.c
B.c
Where functions from any of the files can call any function from a different files. My question being, do I need a .h i.e. header file for each of A and B's implementation where each header file has the definition of ALL the functions in A or B.
Also, main.c will have both A.h and B.h #included in it?
If someone can finally make it clear, also, how do I later compile and run the multiple files in the terminal.
Thanks.
Header contents
The header A.h for A.c should only contain the information that is necessary for external code that uses the facilities defined in A.c. It should not declare static functions; it should not declare static variables; it should not declare internal types (types used only in A.c). It should ensure that a file can use just #include "A.h" and then make full use of the facilities published by A.c. It should be self-contained, idempotent (so you can include it twice without any compilation errors) and minimal. You can simply check that the header is self-contained by writing #include "A.h" as the first #include line in A.c; you can check that it is idempotent by including it twice (but that's better done as a separate test). If it doesn't compile, it is not self-contained. Similarly for B.h and B.c.
For more information on headers and standards, see 'Should I use #include in headers?', which references a NASA coding standard, and 'Linking against a static library', which includes a script chkhdr that I use for testing self-containment and idempotency.
Linking
Note that main.o depends on main.c, A.h and B.h, but main.c itself does not depend on the headers.
When it comes to compilation, you can use:
gcc -o program main.c A.c B.c
If you need other options, add them (most flags at the start; libraries at the end, after the source code). You can also compile each file to object code separately and then link the object files together:
gcc -c main.c
gcc -c A.c
gcc -c B.c
gcc -o program main.o A.o B.o
You must provide an header file just if what is declared in a .c file is required in another .c file.
Generally speaking you can have a header file for every source file in which you export all the functions declared or extern symbols.
In practice you won't alway need to export every function or every variable, just the one that are required by another source file, and you will need to include it just in the required file (and in the source paired with the specific header file).
When trying to understand how it works just think about the fact that every source file is compiled on its own, so if it's going to use something that is not declared directly in its source file, then it must be declared through an header file. In this way the compiler can know that everything exists and it is correctly typed.
It would depend on the compiler, but assuming you are using gcc, you could use something like this:
gcc -Wall main.c A.c B.c -o myoutput
Look at http://www.network-theory.co.uk/docs/gccintro/gccintro_11.html (first google answer) for more details. You could compile it into multiple object files/ libraries:
gcc -c main.c
gcc -c A.c
gcc -c B.c
gcc -o mybin main.o A.o B.o
You want to use
gcc -g *.c -lm
It saves typing and will allow you to link all your c files in your project.

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