Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help in telling compiler to include header files for C

  1. Oct 27, 2016 #1
    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
    Code (C):
    #include "einterp.h"
     
    while start of
    file looks like this
    Code (C):
    #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: Oct 27, 2016
  2. jcsd
  3. Oct 27, 2016 #2

    jim mcnamara

    User Avatar

    Staff: Mentor

    The #include format:
    Code (Text):
     #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.
     
  4. Oct 27, 2016 #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.
     
  5. Oct 27, 2016 #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. Oct 27, 2016 #5

    Mark44

    Staff: Mentor

  7. Oct 28, 2016 #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
    on command line it gives me following error

    And yes the three header files
    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: Oct 28, 2016
  8. Oct 28, 2016 #7
    @Mark44 I am using the following command
    but I am still getting following error
    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.
     
  9. Oct 28, 2016 #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.
     
  10. Oct 28, 2016 #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.
     
  11. Oct 28, 2016 #10

    Mark44

    Staff: Mentor

    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.
     
  12. Oct 28, 2016 #11
    @Mark44 I actually used -L and it was almost same error. With following command
    The error is
    Which means compiler does not find config.h
     
  13. Oct 28, 2016 #12

    Mark44

    Staff: Mentor

    I believe the problem is actually in your code for einterp_mex.c

    Code (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:
    Code (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.
     
  14. Oct 28, 2016 #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.
     
  15. Oct 28, 2016 #14

    Mark44

    Staff: Mentor

    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.
     
  16. Oct 28, 2016 #15
    @Mark44 Ok. I did it and this is how my top of einterp.h file looks
    Code (C):
    #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
     
  17. Oct 28, 2016 #16

    jim mcnamara

    User Avatar

    Staff: Mentor

    What command did you use to compile?
     
  18. Oct 28, 2016 #17
    for above mentioned error and file I used following command
     
  19. Oct 28, 2016 #18

    Mark44

    Staff: Mentor

    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 (Text):
    #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"
     
  20. Oct 28, 2016 #19

    Mark44

    Staff: Mentor

    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.
     
  21. Oct 28, 2016 #20
    @Mark44 Used the 2nd approach you mentioned. This is my einterp.h file now
    Code (C):
    #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
    But result is same, same error as what I mentioned before.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Help in telling compiler to include header files for C
Loading...