Page 1 of 1

Header files in application

Posted: Thu Mar 17, 2022 10:34 am
by nicuvlad94
Hello,

I have a question about using header files with an app. I saw there are some modifications in the firmware now, and the source files need to be specified by appending the "obj-y" variable. But what should one do for the header files? I see that it doesn't work anymore to add them to "INCLUDES" in the Makefile of the app - if I do that, they are not visible during compilation.
Thank you!

Re: Header files in application

Posted: Fri Mar 18, 2022 8:35 am
by kristoffer
Hi!

Object (.o) files are added to the build using Kbuild files in the new make system. Each directory must have one Kbuild file that adds the files in the directory, or a sub-directory.

In the HelloWorld example app for instance, there is a Kbuild file in the root (https://github.com/bitcraze/crazyflie-f ... rld/Kbuild) that includes the /src sub dir with

Code: Select all

obj-y += src/
In the /src dir there is another Kbuild file (https://github.com/bitcraze/crazyflie-f ... src/Kbuild) that includes the object file

Code: Select all

obj-y += hello_world.o

Re: Header files in application

Posted: Fri Mar 18, 2022 9:10 am
by nicuvlad94
Yes, this was clear. However, in case my app also contains some sub-folders that contain header files .h, how do I specify in the Makefile the location of these .h files? Because I currently get a compilation error: the .h files are not found.

Re: Header files in application

Posted: Mon Mar 21, 2022 1:45 pm
by tobias
I think easiest is to use the path to the .h file. I think that path is relative to the .c file that is including it, e.g.

Code: Select all

#include "inc/my_header_file.h"

Re: Header files in application

Posted: Mon Mar 21, 2022 4:35 pm
by nicuvlad94
Thank you for the reply! The solution would for sure work, but if multiple .c files include some .h files from an external library, each user would have to modify all .c files and adjust the library path. It was quite nice before when one could specify in the Makefile where the compiler should look for the .h files.

Re: Header files in application

Posted: Tue Mar 22, 2022 12:02 pm
by kristoffer
I found a way

You can add

Code: Select all

EXTRA_CFLAGS += -I$(PWD)/other
to your make file

An example added in https://github.com/bitcraze/crazyflie-f ... /pull/1001

Re: Header files in application

Posted: Fri Mar 25, 2022 1:55 pm
by nicuvlad94
Thank you! I managed to get it working when the .h files are all in one folder. However, when I have two folders with .h files, for instance, other0 and other1, it will only detect the .h files from the folder that was first assigned to EXTRA_CFLAGS.

For example, if I have

Code: Select all

EXTRA_CFLAGS += -I$(PWD)/other0 -I$(PWD)/other1
Now only the .h files in other0 will be seen, still giving error for not finding the ones in other1.

If I change the order to

Code: Select all

EXTRA_CFLAGS += -I$(PWD)/other1 -I$(PWD)/other0
this will only see the ones in other1.

I also tried

Code: Select all

EXTRA_CFLAGS += -I$(PWD)/other0
EXTRA_CFLAGS += -I$(PWD)/other1
but I get the same. Do you have any idea why this could happen?

Re: Header files in application

Posted: Mon Mar 28, 2022 12:58 pm
by kristoffer
Interesting!
I pretty much have no idea why, sorry!