Can Lint resolve the include path of Header Files - c

I have setup one Project Folder in which i have main Project Directory, say Main_Proj. In that folder, I have created two folders for Header_Files and Source_Files.
Folder Header_Files only contains all header files. lets say module_1a.h and so on. Whereas, folder Source_Files contains main.c and again module wise folders, lets say Module_1 which only contains required .c files of particular module. So for example, folder Module_1 contains Module_1a.c and Module_1b.c.
So the path for Module_1a.c would be =
Main_proj\Souce_Files\Module_1\Module_1a.c
and in all source file I have included the Header Files like below -
//Code for Module_1a.c
#include "..\..\Header_Files\Module_1a.h"
....
My IDE is MP LAB and this code and all modules are working fine with MP LAB xc8 compiler.
Problem occurs when i started Linting my code, its giving me error like -
Error 322: Unable to open include file
'....\Header_Files\Module_1a.h'
I am using PC Lint for C and C++ (version 9). I searched for resolution of this error in Regference manual and got to know to include that directory with -i option.
I also checked with set INCLUDE=<directory Path> but its not working.
Is there any thing i can do with my std.lnt file or do i have to change the folder structure for my Project?

Any source code tree organization where headers or paths contain .. is broken as designed.
The way to go and do away with a lot of problems is
have a single project root directory
Use -I. when compiling, linting, preprocessing, static analyzing, ...
all file references in headers and the project makefile are as seen from the project root
I.e. a header includes other headers using
#include "subdir/whatever/foo_module.h"
and all compilation happens with the working directory being the project root, e.g.
subdir/whatever/foo_module.o: subdir/whatever/foo_module.c
$(CC) $(CFLAGS) -I. -o $# $<
This keeps -I lists extremely short; ideally only -I..

One solution is to use the flag
+fdi // #include search in the directory of the including file
see Error 322 at https://www.kessler.de/prd/gimpel/pclint-meldungen.htm
or
https://www.bezem.de/pdf/htwpl.pdf
or
https://www.gimpel.com/archive/pub90/read90.txt (Section Microsoft's nested #include search)

Related

C and MinGW: How do I fix my "No such file or directory" error?

I have made a python "compiler" that helps me compile my C code with gcc, for example it fetches all my header files and source files. So my cmd commmand is gcc {headers} {source} -o {build_dir}/build.exe -lgdi32 -w where {headers} is a string like -Ipath/to/headers/foo.h -Ipath/to/other/headers.foo2.h and where {source} is the same but with .c files. It seems that the compiler finds the header files, but when compiling my code it fails.
(btw I am trying to make a portable programming environment on my flash drive so python and mingw are both portable)
This is the error: fatal error: test.h: No such file or directory #include "test.h"
My project tree
I have put the third party library files into the mingw directory instead of making a custom one and then linking it in the gcc command.
The -I option takes the path to the directory containing the header files or more specifically with an argument -Ipath and a directive #include<a/b.h>, the compiler will try to look for the header file at path/a/b.h.
So you should not give it paths to header files, only to the directory or directories relative to which you use include directives.

Search Path for C Code Project

I have a third party Curl project, inside there is a lib folder containing the source file,
and also a include folder, inside include folder there is a curl folder which a bunch of .h files
/lib/***.c and /lib/***.h files
/include/curl/curl.h
Inside the sources files of the /lib there are many calls to
#include <curl/curl.h>
I copy the /lib and /curl folder into my new projects. Now When I compiled there's an error on
Lexical or preprocessor error on #include <curl/curl.h>
I added the Header search path and User search path
"$(SRCROOT)/curl"
which points to /curl folder containing curl.h file.
but the project still has compile issue on
ANd I don't want to change that to just because there are too many occurrences. ANy ideas?
Use -I compilation flag to specify the include path.
e.g.
gcc -I/var/lib curl.c
If the #include is #include <curl/curl.h> then the header file will be found in directory /var/lib/curl.
Try This
gcc curl.c -I/(PATH TO HEADER FILE)
if you have any shared libraries to link then try this
gcc curl.c -I/(PATH TO HEADER FILE) -L/(path to lib)

How To Include Files From Multiple Directories In C on Linux?

gcc main.c -o main -I include
I am creating a small c application with following directory structure:
app=>
=>src (a directory, with all source files)
=>include (a directory, with all header files)
=>common (a directory, with all common files)
=>main.c
Now I am trying to run main.c which contains #include directive to include header files from include directory and function calls to .c files in both common and src directories. I am using -I option but it is useful only for one directory path indication.
How does the compiler will look in all src, common and include directories to resolve the calls.
Kindly suggest me a command or make file to provide path of multiple directories while compiling with gcc.
Multiple -I options are permitted. The description of the -I option from Options for Directory Search
states:
Add the directory dir to the head of the list of directories to be searched for header files. This can be used to override a system header file, substituting your own version, since these directories are searched before the system header file directories. However, you should not use this option to add directories that contain vendor-supplied system header files (use -isystem for that). If you use more than one -I option, the directories are scanned in left-to-right order; the standard system directories come after.
For example:
gcc main.c -o main -Iinclude -Isrc/include -Icommon/include
Note that if main.c is using functions implemented in another .c file(s) then the other .c files will also need compiled and linked into the final program binary. For example:
gcc main.c src/another.c -o main -Iinclude -Isrc/include -Icommon/include

Linking .h & .c Files to Main.c with WinAVR

I am using WINAVR to progam an Amtel ATMEGA328 Chip.
I am trying to Link a library to my file but I am not sure exactly how to do it, and what I need to edit in the make file.
I have a lcd_lib.h and lcd_lib.c file that I want to include
in my main.c i have #include "lcd_lib.h";
and I have those files in the same directory as my make filer & main.c file.
Jeeze I am so used to being spoilt with C# and Java
My Solution:
in the make file:
find the line
SRC = $(TARGET).c [ADD FILENAME HERE]
so mine was:
SRC = $(TARGET).c lcd_lib.c

Header Files in Multiple Directories: Best Practices

I'm a C Newb
I write lots of code in dynamic languages (javascript, python, haskell, etc.), but I'm now learning C for graduate school and I have no idea what I'm doing.
The Problem
Originally I was building all my source in one directory using a makefile, which has worked rather well. However, my project is growing and I would like to split the source into multiple directories (unit tests, utils, core, etc.). For example, my directory tree might look like the following:
.
|-- src
| |-- foo.c
| |-- foo.h
| `-- main.c
`-- test
`-- test_foo.c
test/test_foo.c uses both src/foo.c and src/foo.h. Using makefiles, what is the best/standard way to build this? Preferably, there would be one rule for building the project and one for building the tests.
Note
I know that there are other ways of doing this, including autoconf and other automatic solutions. However, I would like to understand what is happening and be able to write the makefiles from scratch despite its possible impracticality.
Any guidance or tips would be appreciated. Thanks!
[Edit]
So the three solutions given so far are as follows:
Place globally used header files in a parallel include directory
use the path in the #include satement as in #include "../src/foo.h"
use the -I switch to inform the compiler of include locations
So far I like the -I switch solution because it doesn't involve changing source code when directory structure changes.
For test_foo.c you simply need to tell the compiler where the header files can be found. E.g.
gcc -I../src -c test_foo.c
Then the compiler will also look into this directory to find the header files. In test_foo.c you write then:
#include "foo.h"
EDIT:
To link against foo.c, actually against foo.o, you need to mention it in the object file list. I assume you have already the object files, then do after that:
gcc test_foo.o ../src/foo.o -o test
I also rarely use the GNU autotools. Instead, I'll put a single hand-crafted makefile in the root directory.
To get all headers in the source directory, use something like this:
get_headers = $(wildcard $(1)/*.h)
headers := $(call get_headers,src)
Then, you can use the following to make the object-files in the test directory depend on these headers:
test/%.o : test/%.c $(headers)
gcc -std=c99 -pedantic -Wall -Wextra -Werror $(flags) -Isrc -g -c -o $# $<
As you can see, I'm no fan of built-in directives. Also note the -I switch.
Getting a list of object-files for a directory is slightly more complicated:
get_objects = $(patsubst %.c,%.o,$(wildcard $(1)/*.c))
test_objects = $(call get_objects,test)
The following rule would make the objects for your tests:
test : $(test_objects)
The test rule shouldn't just make the object files, but the executables. How to write the rule depends on the structure of your tests: Eg you could create an executable for each .c file or just a single one which tests all.
A common way of doing this is for header files used by a single C file to be named the same as that C file and in the same directory, and for header files used by many C files (especially those used by the whole project) to be in a directory include that is parallel to the C source directory.
Your test file should just include the header files directly using relative paths, like this:
#include "../src/foo.h"

Resources