• C

c language help - file paths

Hi,

I have code for reference to header files. But, I get below error:

No such file or directory
compilation terminated.

Code is like below:

#include <header/abc-logging.h>

#include <header2/soap-log.h>

Open in new window


The path of files is like this:

home>code>examples>logexample   // this is source code path while in source code header like above are defined

The path for "header" folder is  home>header and for header2 , path is home>header2.

Is it wrong, I tried to put it in different paths but I always get error for file not found. Is there permission issues?

Please guide.

Thanks
Johny12345Asked:
Who is Participating?
 
ozoCommented:
If you are using gcc or similar, compile with -Ihome or set CPATH
0
 
Johny12345Author Commented:
How to do it? I am using Terminal in ubuntu
0
 
ozoCommented:
cc -I/home /home/code/examples/logexample.c
0
 
satsumoSoftware DeveloperCommented:
Given the file layout you describe this should work:

#include "../../header/abc-logging.h"

#include "../../header2/soap-log.h"
But that is a more inflexible way to do it. It requires the header files to be in a specific location relative to the source file. The method that ozo is suggesting is better. It adds the directory containing the header to the include search path so that the compiler can find them.

In C #include<foo.h> means find foo.h in the include search path (or a relative path) #include "foo.h" means find foo.h in the current directory (or a relative path).
0
 
sarabandeCommented:
It requires the header files to be in a specific location relative to the source file. The method that ozo is suggesting is better. It adds the directory containing the header to the include search path so that the compiler can find them.
actually both concepts normally would be used for a c or c++ project.

if the header folders belong to the one project they should be relative to the folder where the sources of the project are. the home folder doesn't make so much sense for this case. typically they would be in the same folder as the sources to compile. if it is too many files you may have two folders 'header' and 'source' below the project folder.

if the header files should be used by multiple projects you should put the folders below a root folder that is above all the project folders. it is also good practice to have a 'common' folder, then. you would use the common folder either by  

#include "../../common/header/xx.h"

or better add the absolute path to the common file to 'include' environment variable (for example by setting and exporting the 'include' variable in the .bashprofile). if the folder could be found by environment variable you could do the include statements like

#include <xx.h>

note, the <> brackets which is a syntax to indicate include files being stored at known include folders. most compilers do not (no longer) be sensitive to the kind of brackets but it is still good programming if you do it in your sources.

headers which come from 3rd libraries always should be added to folders that were known by include variable or by -I compiler option.

Sara
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.