Help in telling compiler to include header files for C

In summary, the conversation is discussing the process of compiling a program that requires the use of .c files and #include statements. The user is encountering errors when trying different commands to compile the program, and is unsure of the correct command to use with the gcc compiler. They provide information about the files and folders involved, and ask for guidance on how to specify the path to header files in the command line. Some possible solutions are suggested, including changing the format of the include directives in the code and specifying the path to the header files using the -L switch in the gcc command.
  • #1
Atr cheema
69
0
For a program to use I have to compile some .c files which include #include''header.h'' statements and I have tried different commands to compile but it gives me errors like 'No such file or directory'.
Here is start of my file named
einterp_mex.c
Code:
#include "einterp.h"
while start of
einterp.h
file looks like this
Code:
#ifndef _EINTERP_H
#define _EINTERP_H

#include <libutils/config.h>
#include <libutils/utils.h>
#include <libutils/parallel.h>
#include <libutils/debug_defs.h>
#include <libmatlab/mesh.h>

#ifdef MATLAB_MEX_FUNCTION
#include <libmatlab/mexparams.h>
#endif

#include "interp_opts.h"
#include "einterp_tri_templates.h"
#include "einterp_quad_templates.h"

My current folder containing einter_mex.c and einterp.h is 'interp'. The .h files mention inside 'einterp.h' are present in folder 'libutils'. The folder 'mutils' is the parent folder containing both 'interp' and 'libutils' folders.

Can anybody guid me what command with gcc compiler should I use. Thank you.
 
Last edited:
Technology news on Phys.org
  • #2
The #include format:
Code:
 #include <subdirectory/filename.h>
means look in the include directory ( e.g., /usr/include), find sub directory then filename.h in that subdirectory.

It does not mean look in the current working directory.

What platform are you on? ... assuming Linux. I am guessing you have to install MATLAB libraries as well. Header files are NOT libraries -- libraries have names like: filename.so.2 and have to be specified in a run time environment variable LD_LIBRARY_PATH ..in order to be able to be found when the executable is started by the OS.
 
  • #3
I am using Windows 10 and yes I have MATLAB installed. Actually I the function that I need to run requires first compilation of .c file.
 
  • #4
What's the actual wording of the error? Are you sure you're getting a compiler error and not a linker error?

Do you know the difference between "file.h" and <file.h>?

Is interp_opts.h in the same directory as your header?
 
  • #6
@newjerseyrunner Yes I know the difference between #include<header.h> and #include''header.h''. My problem is that what will be the command to compile einterp_mex.c file. The file interp.h and einterp_mex.c are in same folder. When I type
gcc einterp_mex.c
on command line it gives me following error

gcc: error: einter_mex.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.
And yes the three header files
einterp_quad_template.h,
einterp_tri_template.h
interp_opts.h
are also in the same folder.
I believe the problem is that the header file einterp.h also has include statements, and the path to these files like utils.h, parallel.h, debug_defs.h and mesh.h has to be told to in command line while compiling einterp_mex.c file. Please have a look at einterp.h header file provided at my first post.
I am sorry, I have never used C before, so asking very basic question may be.
 
Last edited:
  • #7
@Mark44 I am using the following command
gcc einterp_mex.c einterp.h -lC:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils
but I am still getting following error
In file included from einterp_mex.c:6:0:
einterp.h:4:29: fatal error: libutils/config.h: No such file or directory
#include <libutils/config.h>
^
compilation terminated.
einterp.h:4:29: fatal error: libutils/config.h: No such file or directory
#include <libutils/config.h>
^
compilation terminated.
which means compiler is not finding config.h file located inside libutils folder. However if I copy config.h file and paste it inside my current directory, then this error is not repeated and the compiler reads config.h file. As einterp.h contains a lot of header statements, so I can't simply copy and paste all those header files to my current folder. There should be a way to direct compiler to look for header files at their specific places.
 
  • #8
Sounds more like you simply aren't used to a command line. When you type gcc file.c, is the terminal in the correct directory? Type ls on Linux or dir on windows to make sure.
 
  • #9
@newjerseyrunner I accept the blame of being naive and inexperience. I assume I am in correct directory because when I type dir, it shows me the files like einterp_mex.c and einterp.h which are in my current folder.
 
  • #10
Atr cheema said:
@Mark44 I am using the following command
gcc einterp_mex.c einterp.h -lC:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils
I don't use GCC, but from what little I know about it, you don't list the include files, such as einterp.h, on the command line. The reason for the -L switch is to inform gcc where to look for include files. You used the -l (lower case el), which is used to inform the compiler where to find library files, not include files.
 
  • #11
@Mark44 I actually used -L and it was almost same error. With following command
gcc einterp_mex.c -LC:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils
The error is
In file included from einterp_mex.c:6:0:
einterp.h:4:29: fatal error: libutils/config.h: No such file or directory
#include <libutils/config.h>
^
compilation terminated.
Which means compiler does not find config.h
 
  • #12
I believe the problem is actually in your code for einterp_mex.c

C:
#include <libutils/config.h>
#include <libutils/utils.h>
#include <libutils/parallel.h>
#include <libutils/debug_defs.h>
Change these include directives by removing the libutils part of each.
In other words, change to this:
C:
#include "config.h"
#include "utils.h"
#include "parallel.h"
#include "debug_defs.h"
I don't think you should be using the form #include <xxx> for any header files other than the standard library headers (like stdio.h and others). You should be using the form #include "utils.h" for these headers.

Besides these, you have a lot of other files being included, in different directories. These could be causing problems, as well.
 
  • #13
@Mark44 You are right that changing <> to "" and bringing all those header files in my current folder solves the problem. But then I will have to bring a lot of header files to my current folder every time I compile a .c file. Second some of those header files contain further #include statements, which means copying also all those files to my current folder and there are a lot of .c files which I have to compile.
 
  • #14
Atr cheema said:
@Mark44 You are right that changing <> to "" and bringing all those header files in my current folder solves the problem. But then I will have to bring a lot of header files to my current folder every time I compile a .c file. Second some of those header files contain further #include statements, which means copying also all those files to my current folder and there are a lot of .c files which I have to compile.
I didn't say anything about moving the include files. Use the -L switch to let the compiler know where to find the include files.
 
  • #15
@Mark44 Ok. I did it and this is how my top of einterp.h file looks
Code:
#ifndef _EINTERP_H
#define _EINTERP_H

#include "libutils/config.h"
#include "libutils/utils.h"
#include "libutils/parallel.h"
#include "libutils/debug_defs.h"
#include <libmatlab/mesh.h>

#ifdef MATLAB_MEX_FUNCTION
#include <libmatlab/mexparams.h>
#endif

#include "interp_opts.h"
#include "einterp_tri_templates.h"
#include "einterp_quad_templates.h"
and it gives following error
In file included from einterp_mex.c:6:0:
einterp.h:4:29: fatal error: libutils/config.h: No such file or directory
#include "libutils/config.h"
^
compilation terminated.
 
  • #16
What command did you use to compile?
 
  • #17
jim mcnamara said:
What command did you use to compile?
for above mentioned error and file I used following command
gcc einterp_mex.c -LC:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils
 
  • #18
Atr cheema said:
@Mark44 Ok. I did it and this is how my top of einterp.h file looks
Code:
#ifndef _EINTERP_H
#define _EINTERP_H

#include "libutils/config.h"
#include "libutils/utils.h"
#include "libutils/parallel.h"
#include "libutils/debug_defs.h"
#include <libmatlab/mesh.h>
You didn't read what I wrote carefully enough. I said to remove the "libutils" part.
You have two options here for your include files:
1)Use the full path, as in
Code:
#include "C:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils\config.h"
You don't need to use the -L switch on the gcc command line if you do this.
2)Add the path on the gcc command line, as in gcc ... -LC:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils ... <other switches>
Then have this in your file:
#include "config.h"
Atr cheema said:
C:
#ifdef MATLAB_MEX_FUNCTION
#include <libmatlab/mexparams.h>
#endif

#include "interp_opts.h"
#include "einterp_tri_templates.h"
#include "einterp_quad_templates.h"
and it gives following error
 
  • Like
Likes Atr cheema
  • #19
When you have this:
gcc einterp_mex.c -LC:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils

and your code contains this:
#include "libutils/config.h"
the compiler will be looking for another directory named libutils under the libutils directory -- no such directory exists, so the compler generates a fatal error and quits.
 
  • #20
@Mark44 Used the 2nd approach you mentioned. This is my einterp.h file now
Code:
#ifndef _EINTERP_H
#define _EINTERP_H

#include "config.h"
#include "utils.h"
#include "parallel.h"
#include "debug_defs.h"
#include <libmatlab/mesh.h>

#ifdef MATLAB_MEX_FUNCTION
#include <libmatlab/mexparams.h>
#endif

#include "interp_opts.h"
#include "einterp_tri_templates.h"
#include "einterp_quad_templates.h"
with following command
gcc einterp_mex.c -LC:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils
But result is same, same error as what I mentioned before.
In file included from einterp_mex.c:6:0:
einterp.h:4:20: fatal error: config.h: No such file or directory
#include "config.h"
^
compilation terminated.
 
  • #21
Is the file config.h actually in this directory -- LC:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils ?
 
  • #22
@Mark44 Yes file config.h is in this directory
C:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils
I copied this path directly and pasted here. Furthermore this is how config.h looks like
Code:
#include "osystem.h"

#ifdef _MSC_VER
#define SNPRINTF _snprintf
#else
#define SNPRINTF snprintf
#endif

#ifdef  _GNU_SOURCE
# define __USE_GNU      1
# define __USE_XOPEN2K8 1
#endif
 
  • #23
This is a stumper. What's the gcc command line you were using most recently?
 
  • #24
Mark44 said:
What's the gcc command line you were using most recently?
This one
>gcc einterp_mex.c -LC:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils
 
  • #26
Atr cheema said:
This one
>gcc einterp_mex.c -LC:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils
Since that is the command you are using, assuming the header files are located in that folder, try this:
Code:
gcc einterp_mex.c -IC:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils
Note that it is a capital i (I). Not a lower case L. I write the note because some fonts make them look very similar.

Also, if you are 100% sure that all those third party headers provided are included with #include "file" and not #include <file>, you can be even more specific and use:
Code:
gcc einterp_mex.c -iquoteC:\Users\atrch\Documents\MATLAB\geos\mvep2\mutils-0.4\mutils\libutils

Mark44 said:
gcc -Ldir -- Here dir is the full path where your include files are.
A little correction in case anyone wants to know the actual use :smile:, -Ldir is actually used for the subprogram ld for linking, not header includes. I say it because the referenced documents doesn't explicitly say it.
 
  • Like
Likes Atr cheema
  • #27
Psinter said:
A little correction in case anyone wants to know the actual use :smile:, -Ldir is actually used for the subprogram ld for linking, not header includes. I say it because the referenced documents doesn't explicitly say it.
OK, that makes sense that -L would be used for library files, not include files.
 
  • Like
Likes Psinter
  • #28
Right,

-Idirectory (uppercase letter "eye") specifies where to look for include-files when compiling;

-Ldirectory specifies where to look for previously-compiled libraries when linking.
 

What is the purpose of including header files in C?

Including header files in C allows the compiler to access the declarations and definitions of functions, variables, and data structures that are needed for a specific program to run. This helps to avoid errors and makes the code more organized and readable.

How do I tell the compiler to include a header file in my program?

To include a header file in your program, you can use the preprocessor directive #include followed by the name of the header file in angle brackets or double quotes. This will instruct the compiler to copy the contents of the header file into your program before compiling.

Can I include multiple header files in my C program?

Yes, you can include multiple header files in your C program. However, it is important to avoid including the same header file multiple times to prevent errors. You can use header guards or conditional compilation to ensure that a header file is only included once.

What happens if I don't include a necessary header file in my program?

If you don't include a necessary header file in your program, the compiler will not be able to access the declarations and definitions of functions, variables, and data structures that are needed for your program to run. This will result in compilation errors and your program will not be able to execute properly.

Do I need to include header files for standard library functions in my C program?

No, you do not need to include header files for standard library functions in your C program as they are automatically included by the compiler. However, it is still good practice to include them for better organization and readability of your code.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
Back
Top