Linux make header files

Makefile with headers including headers

How do I write a target for app.o in a makefile? Should I include all three headers in the prerequisites?

6 Answers 6

Yes, you should list all dependencies of app.c which might need a recompilation of app.o.

You might find g++ -MM app.c useful: -MM will list all dependencies of app.c in a make format. You can even automate it and have your makefile semi-automatically handle dependencies.

app.o : app.c app.h model/world.h controller/controller.h gcc -c app.c 

Yes, you need to include all three headers as dependencies. Makefiles: if anything on the right of : has changed, then it needs to remake the thing on the left. If you fix a bug in your controller and cut out three arguments from one of the functions, and the app calls that function from the controller, then the app needs to be remade to deal with the change.

Otherwise when the app walks over to the controller and asks for function X with 5 arguments, the new and improved controller isn’t going to know what the hell the app is talking about. Error will be thrown. Fire and blood everywhere. And it all ends in tears.

This is an example how I solved this issue. gcc -MM generates rules from the given source file’s include directives. Try it on a source file containing include directives to see it’s output.

# src/x.c src/y.c . => build/x.o build/y.o . OBJ = $(patsubst src/%.c,build/%.o,$(wildcard src/*.c)) # src/x.c src/y.c . => build/x.d build/y.d . # `-> these files will include the generated rules INCLUDE_PREQS = $(patsubst src/%.c,build/%.d,$(wildcard src/*.c)) all: main main: $(OBJ) gcc -o $@ $^ ## compile object from src/x.c to build/x.o # src/x.c src/y.c . => build/x.o build/y.o build/%.o : src/%.c gcc -o $@ -c $ < ## generate header prerequisite rules # -MT $(patsubst build/%.d,build/%.o,$@) # `->set the generated target to be build/%.o build/%.d: src/%.c gcc -MM -MT $(patsubst build/%.d,build/%.o,$@) $ < >$@ ## include the generated rules include $(INCLUDE_PREQS) 

You should never need to manually specify header dependencies in makefiles.

Add -MMD -MP to compiler flags. It will make the compiler output .d files in addition to .o . Those are little makefiles specifying the dependencies. You then -include them into the main makefile.

SOURCES := 1.c 2.c program: $(patsubst %,%.o,$(SOURCES)) gcc $^ -o $@ %.c.o: %.c gcc -MMD -MP $< -c -o $@ -include $(patsubst %,%.d,$(SOURCES)) 
SRC = $(wildcard *.c) OBJ = $(patsubst %.c, %.o, $(SRC)) DEP = $(patsubst %.o, %.mm, $(OBJ)) include $(DEP) app: $(OBJ) $(CC) -o $@ $(OBJ) .FORCE: %.mm: .FORCE $(CC) -MM $*.c > $*.mm 

Here: DEP is a list of dependency files that need to be included (ending in mm). There is a rule to build the mm files that generates the dependency for the file. You force the build of the dependencies that are then included as dependencies to check your actual dependencies

Читайте также:  Расшарить папку linux centos

No, you should not list the headers as dependencies! This is redundant with the specification that already exists in the form of your #include directives. And sooner or later redundancy usually causes problems, because as your project grows you will end up with inconsistent lists.

There is also a widespread though naive misconception that only header files are dependencies. This can lead to wrong builds, because many other reasons like changed command line switches, a changed compiler or changed hardware architecture, get overseen.

The solution to both problems is to use a reliable build tool: makepp handles these cases and can even scan for sub includes in include files that weren't even built yet, when you started it.

There is much more to makepp. Besides doing almost all that GNU make can, there are lots more useful things, and you can even extend your makefiles with some Perl programming.

Источник

Efficient Linux Kernel Development with make: Tips for Working with Header Files

If you're a developer working with the Linux kernel, you know that it can be a daunting task to manage the vast number of header files. The kernel's header files define the interfaces between various kernel components and the user space, and managing them properly is crucial for efficient development. In this article, we'll provide some tips and tricks for working with Linux kernel header files using the make tool, which is widely used in the Linux community.

Understanding the basics of make

Before we dive into the specifics of working with header files, let's review some basics of the make tool. make is a build automation tool that is used to compile and link programs. It reads a makefile, which specifies how to derive the target program from its dependencies. The makefile contains rules that describe how to build a target (executable or object files) and the dependencies (source code files and header files) needed.

Using make to manage header files

One of the most significant advantages of make is that it handles header files automatically. When building an object file, make checks for changes in the dependencies (source code files and header files) and rebuilds the target if necessary. This means that you don't have to manually track changes in the header files and rebuild the entire project each time you make a minor change.

Here is an example of a make rule to build an object file with dependencies:

CC=gcc CFLAGS=-Wall -O2 DEPS=header1.h header2.h OBJ=myfile.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) 

In this example, make builds the myfile.o object file from the myfile.c source code file and the header1.h and header2.h header files. If either of the header files changes, make automatically rebuilds the target myfile.o .

Читайте также:  Настройка mysql linux ubuntu

Keeping header files organized

To make make more efficient, it's essential to keep your header files organized correctly. One way to do this is to store your header files in a separate directory, away from the source code files. This helps keep the source code directory clean and makes it easier to manage header file dependencies.

src/ myfile.c otherfile.c include/ header1.h header2.h 

To include a header file in your source code, you can use the -I flag to specify the include directory.

CC=gcc CFLAGS=-Wall -O2 -I../include DEPS=header1.h header2.h OBJ=myfile.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) 

Avoiding circular dependencies

One possible issue with header files is circular dependencies, where two or more header files depend on each other. This can lead to compilation errors and makefile issues. Suppose header file A includes header file B, and header file B includes header file A. In that case, you will have a circular dependency that make cannot resolve.

To avoid circular dependencies, you should use forward declarations whenever possible. A forward declaration is a declaration of a function or data type without defining it. This notifies the compiler of the type's existence without including the header file that defines it.

/* header1.h */ #ifndef HEADER1_H #define HEADER1_H /* Forward declaration */ struct my_struct; void func(struct my_struct *ms); #endif /* HEADER1_H */ 

By using a forward declaration in header1.h , you can avoid the circular dependency with header2.h .

/* header2.h */ #ifndef HEADER2_H #define HEADER2_H #include "header1.h" struct my_struct < int i; >; #endif /* HEADER2_H */ 

Conclusion

Managing header files in Linux kernel development can seem like a daunting task, but with make , it's much more manageable. By keeping your header files organized, using forward declarations, and avoiding circular dependencies, you can streamline your development process and spend more time writing code and less time managing header files.

Источник

How to add headers to a Makefile?

I have the following Makefile . How to add also some header files that I have in the project folder (e.g. header1.h with its relative header1.c ) ?

CC := gcc CFLAGS := -Wall -Wextra -Wpedantic -O3 CFILES := $(shell ls *.c) PROGS := $(CFILES:%.c=%) all: $(PROGS) .PHONY: all clean clean: rm -f *~ $(PROGS) 

Even adding them one by one would be ok (no need to use the wildcard). I suppose I should edit the following line:

@Jean-FrançoisFabre I edited the OP, I don't need to use the wildcard! I just need gcc to get the correct dependencies

You don't provide headers to gcc but instead the location where it will look for headers. This is done with the -I command line parameter. So add to your CFLAGS something like this : -I "path/to/headers"

Читайте также:  Wireless tools linux установка

1 Answer 1

First, don't use $(shell ls *.c) but better $(wildcard *.c) . Please take time to read the documentation of GNU make

Then, you usually don't want headers in Makefile -s, you want dependencies on headers.

For example, if you know that foo.o needs both foo.c and header.h (because you have some #include "header.h" in your foo.c ) you would add a dependency like

in your Makefile . See also this example.

There is some way to automate such dependencies, using GCC options; read about Invoking GCC, notably preprocessor options like -M

(details depend upon your project and coding conventions)

For a simple project of a few dozen of files totalizing a few thousand lines, you should first write your Makefile explicitly, with the dependencies. Later you might automatize that (but in simple projects that is not worth the trouble).

In some cases, a header file *.h or a C file *.c is generated by some utility (e.g. swig, bison, . or your own script or program). Then you add appropriate specific rules in your Makefile .

You might use make --trace or remake with -x to debug your Makefile .

Look for inspiration into the source code of some existing free software project (e.g. on github).

Источник

How to create /usr/src/linux-headers- files

I am trying to compile a custom kernel for Nvidia's Jetson TK1 board, and it seems that as well as the kernel itself (zImage) I will need /usr/src/linux-headers- and /lib/modules/ . However I can't see how to create the linux-headers. I have tried running make headers_install but it does not seem to create the same type of output as what I see in other /usr/src/linux-headers directories. I've seen other similar questions such as here, however this assumes you are building the same version as you are running, where I am trying to build with a different LOCAL_VERSION. I am starting from a "Linux4Tegra" distro, however, I am attempting to make a custom system based on Ubuntu Core. To clarify, I am trying to create a kernel with my own LOCAL_VERSION, i.e., my own version, not an existing version in any repository. I would like to generate the headers that match this. So installing a linux-headers-xxx package will not address my problem. My question is how are these packages initially created from the linux source? When I look at the files in the existing headers directories, it contains references to that kernel version, so I am assuming that this has been generated from that version of the linux source. How can I generate these? When someone is compiling a version of the kernel, surely they generate these files somehow?

Not at my computer until tomorrow, but the output is basically completely different. No commonality whatsoever as far as I could see.

Источник

Оцените статью
Adblock
detector