Search Path for C Code Project - c

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)

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.

Windows Command line c file compile error

I work with C codes using gcc compiler and Clion IDE
So, I have a src folder for .c codes and include folder of .h files which contains a bunch of #define
I include some of the .h files in my main.c programe
when I run it in my Clion project, everything work fine
but when I try to run with command line (cuz I need command arguments), it says
Fatal Error: MyFunction.h, no such file or directory MyFunction.h IS MY FILE, NOT DEFAULT library file
What I typed in the command line is $gcc C:\Users\Admin\CLionProjects\project\src\main.c
What I get is "C:\Users\Admin\CLionProjects\project\src\main.c 10: Fatal Error: MyFunction.h No such file or directory.
Then I try to change the #include <MyFunction.h> to #include "MyFunction.h", hoping it will solve the problem, but no.
Then I did a test, I call a function of another .c file in the same src folder, and use command line to run main.c, but it tells me the function is not even defined.
cmd can recognized all the default library files like stdio.h, but none of those created by me. Any idea how to solve this problem? I know it must be some kind of path error
You have to tell gcc preprocessor where to find files you want to include.
You have two kinds of header:
System headers: included with #include <header>.
Local headers: included with #include "header".
Preprocessor searchs in header search path to find system headers.
Preprocessor searchs in current directory, then in header path to find local headers.
You can add folders in header search path using -I option in gcc invocation.
So in your case, you can compile with these commands:
$gcc -I C:\Users\Admin\CLionProjects\project\include C:\Users\Admin\CLionProjects\project\src\main.c
or
$cd C:\Users\Admin\CLionProjects\project\src
$gcc -I ..\include main.c

Can Lint resolve the include path of Header Files

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)

Make / Build with geany

I have problem on build.
My error is something like that: "test.c" error: test.h error"
I have on source code:
#include <test.h>
How to add this on build?
To be spesific How to link this library? Can i link it through Geany?
Operating System ubuntu + Geany as compile
Follow these simple steps:
Create a directory and place your test.c and test.h in the same directory.
In C source file you should write:
#include "test.h" which will search for the header file in current directory as well as the include library path, instead of, the #include <test.h> which will search for test.h in include libraries path only.
Build as usual, using geany.

Header files linked to from header file not found.

I have a problem with Nvidia's OpenCl/Cuda framework, but I think it is a gcc linking issue.
The opencl_hello_world.c example file uses following header file:
#include "../OpenCL/common/inc/CL/opencl.h"
with opencl.h using these header files:
#include <../OpenCL/common/inc/CL/cl.h>
#include <../OpenCL/common/inc/CL/cl_gl.h>
#include <../OpenCL/common/inc/CL/cl_gl_ext.h>
#include <../OpenCL/common/inc/CL/cl_ext.h>
So all the header files are in the same folder.
When I then compile with gcc opencl_hello_world.c -std=c99 -lOpenCL I get following error messages:
error: ../OpenCL/common/inc/CL/cl.h: No such file or directory
error: ../OpenCL/common/inc/CL/cl_gl.h: No such file or directory
...
Even though cl.h and the other header files are located in this folder.
Having searched SO, I then changed the includes in the opencl.h to
#include "cl.h"
#include "cl_gl.h"
how I have read here: gcc Can't Find a Included Header.
But messing around with the frameworks header files does not seem like the way to go? What would be the proper way to handle this problem?
You're using both #include "" form and #include <>, which don't search in the same paths. "" is local to your project, and the -i command line specified to gcc, <> is the 'system' path specified by -I to gcc.
You probably need to set the include path with -Ipath/to/includes in gcc's command line.

Resources