Search code examples
cheader-fileskeil

Keil Uvision 5 adding header files and source files?


I started with an example project in keil from Nordic SDK. This question is not about the nordic sdk, but rather how Keil connects header files and source files. After what I can understand Keil hides all header files merged into the main.c file, see figure below.

Keil merges header files hierarchical into the main.c file

I want to add my own header file called "physical.h" where I define additional hardware that I added. Sometimes I want to add a header file and source files "*.c" files. What can I specifically do to add a new file, and compile this new file with my main.c file? A walkthrough would be appreciated.


Solution

  • I believe Keil is not intuitive in the folder structure sense. However, it is probably good for a modular design as it can easily include files from many places on your computer (git, library etc...).

    Let's start with problem one (adding a header file): The header files in the main.c file is an image of whats already included and "working". Add a new file by right clicking the parent folder of main.c and click "Add new item to group ". From within the menu, choose C-file/H-file or any other appropriate file.

    If there is no group yet, just add a group from the parent folder. Right click and press "add group ...".

    enter image description here

    Now, what might confuse you is that the .h file will not appear in the folder you just created it. The .c files will appear, but not .h! You will find your file at the open in a new window. Right click that tab with the filename you just created, and click "copy full path".

    Here comes the tricky part. Go to the project tab in Keil menu, and click "options for target " or ALT+F7. Go to the tab "C/C++" tab. In the "Include Paths" slot, the will be a button indicating "...", click it. In the top right corner, holding over the first icon will show "New (insert)", click it. Click the new button "..." in the slot you just created, this will open a folder menu. This is where you find your file and add the parent folder of that file. However, you may need to move the file you created in order to find a good structure.

    The structure inside Keil Uvision 5 is just virtual folders, and creating a file will just put it in the project folder with no structure. Finding a good solution now, is up to you, comment if you come up with one.

    The last part I would like to add is what happens when you just included this file. Well, go into your main.c file, include the header file, as in normal C convention. #include "myfile.h". If you try to compile, it should show no error, and your main.c file will have a sub-file of that same .h file you just included.

    Second problem: For C-files you can just add them, the same way as earlier, however, Keil will dump them all at the source of your project, having no structure at all. For structure, you must do that outside of Keil unfortunately.