Keil Uvision 5 adding header files and source files? - c

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.
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.

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 ...".
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.

Related

header files in iks01a2_conf.h template

I was trying to use code generated by STM32CubeMX. I've generated project for SW4STM32, but I have problem with iks01a2_conf_template.h file. Im supposed to replace the header file names with the ones of the target platform and rename the file to iks01a2_conf.
There are 3 headers:
#include "stm32yyxx_hal.h"
#include "nucleo_xyyyzz_bus.h"
#include "nucleo_xyyyzz_errno.h"
I changed first one to "stm32f1xx_hal.h", but I don't know what to do with rest of them. I've tried several names but none of them were found. What are the header files i'm supposed to use there?
My hardware is: STM32F103RB and X-NUCLEO-IKS01A2. When it comes to expansion board i need to use LPS22HB barometer to measure pressure and temperature.
Look into inc project folder. Your files shuold be there.
Add this folder to the include directories in your project settings or makefile

CodeBlocks - How to add an icon to a C program?

I have a small C console program and I want to add an .ico file to it, so that the executable looks nice.
How can I do this in CodeBlocks with MinGW/gcc?
I could not find relevant help via google that a total beginner (like me for C) could follow, so I will Q&A this topic.
First of all you need an .ico file. Put it in the folder with your main.c file.
In CodeBlocks go to File -> New -> Empty File and name it icon.rc. It has to be visible in the Workspace/Project otherwise CodeBlocks will not be aware of this file. It will show up there in a project folder called Resources .
Put the following line in it: MAINICON ICON "filename.ico". MAINICON is just an identifier, you can choose something different. More info 1 & More info 2.
Save the files and compile - CodeBlocks will do everything else for you
What will happen now, is windres.exe (the Resource Compiler) compiling the resource script icon.rc and the icon to an object binary file to obj\Release\icon.res. And the linker will add it to the executable.
It's so easy yet it took me quite a while to find it out - I hope I can save someone else having the same problem some time.

NASM setting in Vistual Studio13

I am creating a library, which require some assembly level code.
I am using using NASM to write and integrate my .asm file.
Now the problem is, I already have a project created in VS13. Now I want to add and integrate an assembly level code to my project.
I have already added a .asm file in my source directory, but when I am trying to run my test case, the compiler is unable to find my assembly code.
I want to know how can I link my .asm file with my .c file.
Structure of my project:
->Project1(Generates a Library)
--->Source
----->File1.c
----->File2.c
----->nasm.asm
->Project2 (Test case to use the library and generate .exe)
-->Source
---->main.c
Now, nasm.asm binaries should get attached with the .lib generated by project1
and Project2 should able to access project1.lib
Apologize if question is bit unclear, its a bit complex for me to make it clear in written. Please let me know if you want any clarification or extra information.
Thanks a lot
For each of you assembly files:
Right click it in the Solution Explorer and choose Properties
Make sure the selected Configuration is either All Configurations or the configuration you are using (this bites me every time!)
In the Configuration Properties>General change the Item type to Custom Build Tool
From the Configuration Properties>Custom Build Tool>General set the following items:
Command Line. Use this as an example: nasm -fwin32 "%(FullPath)" -o %(Filename).obj
Outputs. This is necessary, VS check for this files. I usually use %(Filename).obj.
Link Objects. Yes. If you name your output files with obj extension they are automatically included in the link phase.
To check that you set everything right, select your assembly file, right click and choose Compile.

How to set include path in xcode project

I am trying to use a C library in an Objective-C Xcode project.
The libraries directory structure is as follows:
-- include/
|-- config.h
|-- lib/
| |-- file1.h
| |-- file2.h
| |-- file3.h
The library's docs say to include file1.h, and file1.h includes file2.h and file3.h.
I am getting "file not found" errors for the includes of file2.h and file3.h`.
They are included by file1.h in the following manner:
#include <lib/file1.h>
#include <lib/file2.h>
I read here that these angle-brackets instruct the preprocessor to search for include files along the path specified by the INCLUDE environment variable, as opposed to searching in the same directory as the file that contains the #include.
So I added the INCLUDE environment variable in Xcode by going to Product->Edit Scheme.. and set it to /the-whole-path-to/include/ however, I am still getting the file not found errors.
The files are successfully included if I change file1.h to include them like this:
#include "file2.h"
but I'd rather not do that for every file in the library.
How can I fix this?
In version 5.0.2 of XCode, the best way to accomplish this is probably to add it to the target's "Search Paths" pane. Locating this was (for me) incredibly unintuitive. Here's how I got to it, for those as confused as I:
In the left column, click on the Project Navigator icon (looks like a folder). Then, click on the project entry. This should show a bunch of settings in the main pane. At the top of this pane, click on "Build Settings. This shows a bunch of entries... including one called Search Paths... but you can't add a search path here! This made me gnash my teeth for quite a while, until I figured out that the name of the project at the top of this pane was a pull-down; choose the target from this pull-down, and you should now be able to double click on "Header Search Paths" and perform the needed edit.
Oh, the joy of crazy GUIs.
Figured it out.
All you have to do is add the -I flag to your build setting under "Other C Flags"
So in your target's build setting search for "Other C Flags" and add -I/path-to-include/
Here's a screenshot:
Try this:
1 - select your project file in the left Xcode pane
2 - make sure your project is selected in the middle Xcode pane
3 - select "Build Settings" at the top of the middle Xcode pane
4 - be sure "All" & "Combined" are selected just beneath "Build Settings"
5 - type header in the search field just below "Build Settings"
You should see the search path fields ready for editing in the middle pane.
I solved this in Xcode 5.0.1 using the project Build Settings (as John and Ian noted above, but I cannot comment due to <50 rep).
New info:
When adding includes to User Header Search Paths, I also had to change Always Search User Paths to Yes.
When adding includes to (non-User) Header Search Paths, Always Search User Paths is not required.
Although this works, it is probably better to put it under the "Search Paths" tab instead.
Either you can use "Other C Flags" or use "HEADER_SEARCH_PATHS", to specify the include paths, to look for header for your executable.
In 2021, Xcode v. 12.4, the solution seems to be:
Project->Targets->General->"Scan All Source Files For Includes"-> set to "Yes"
This worked for me.
However, this might backfire if you have multiple versions of a specific .h file, probably not a good practice but it's possible if you have lots of sub-directories with their own mini-projects and similarly named include files.

How to tell the preprocessor to search for a particular folder for header files, when I say #include <xyz.h>

I have around 120 header files (.h files) , and in all of them each one includes many other header files using #include <abcd/xyz.h>, but as I kept .h files in a specific folder, preprocessor is generating filenotfound error.
I moved all the .h files to the single .C file that is calling the first headerfile.
One way to do is make #include <abcd/xyz.h> as #include "abcd/xyz" , but I need to do this in all the header files wherever there is an include statement, and there are hundreds of them.
I can't include many of them in the headerfiles section in Visualstudio because, some of the headerfiles have the same name, but they reside in different directories. (<abcd/xyz.h>,<efgh/xyz.h>).
Any way to do this?
You should add a path into "Additional include directories" in the "C++" section of the project options (the "General" tab). You can use environment variables as well as "this folder" (.) shortcut and "up one folder" (..) shortcut for this setting to not be bound to a certain directory structure.
and I can't include many of them in the headerfiles section in Visualstudio because , some of the headerfiles have the same name, but they reside in different directories.(,)
That's a pretty big problem unless the files that are including those non-uniquely named headers are in the same directory as the header files themselves.
You have no way to guarantee that the compiler will locate one header before another without modifying the #include directive itself (and adding a relative path as one example).
EDIT: It looks like Visual Studio will allow you to specify different Additional Include Directories for each source file in a project (rt-click on the source file in Solution Explorer and modify C/C++ properties). But I think this would be more work than modifying the #include directives themselves - depends on how many non-unique header filenames you have.
In the project settings (under C/C++ in VS2005/2008) there's an option for "additional include directories". You can add the folders containing your header files here, using relative paths.
You can also do this at the IDE level in Tools -> Options -> Projects and Solutions -> VC++ Directories -> Include Files. Typically this method is reserved for headers included as part of a formal library. The first option is typically preferred as it's portable (you can ship your project file to another developer and, provided you use relative/macro'd paths, they can build the project as-is).
What you're looking for is the -I flag and you give the directory...
If you have a Makefile, you should add it to the CPP_FLAGS something like that....
You can also add an INCLUDE variable to your environment variables.

Resources