Header files in application

Discussions and questions about the Crazyflie Nano Quadcopter
Post Reply
nicuvlad94
Beginner
Posts: 28
Joined: Sun Sep 08, 2019 12:44 pm
Contact:

Header files in application

Post 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!
kristoffer
Bitcraze
Posts: 630
Joined: Tue Jun 30, 2015 7:47 am

Re: Header files in application

Post 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
nicuvlad94
Beginner
Posts: 28
Joined: Sun Sep 08, 2019 12:44 pm
Contact:

Re: Header files in application

Post 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.
tobias
Bitcraze
Posts: 2339
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: Header files in application

Post 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"
nicuvlad94
Beginner
Posts: 28
Joined: Sun Sep 08, 2019 12:44 pm
Contact:

Re: Header files in application

Post 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.
kristoffer
Bitcraze
Posts: 630
Joined: Tue Jun 30, 2015 7:47 am

Re: Header files in application

Post 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
nicuvlad94
Beginner
Posts: 28
Joined: Sun Sep 08, 2019 12:44 pm
Contact:

Re: Header files in application

Post 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?
kristoffer
Bitcraze
Posts: 630
Joined: Tue Jun 30, 2015 7:47 am

Re: Header files in application

Post by kristoffer »

Interesting!
I pretty much have no idea why, sorry!
Post Reply