C/C++ How to access header files? - linker

I have added some source (header files) in a common folder (..\shared\abc) and my code file from another folder (..\src\xyz) has #include <abc/../foo.hpp>. I get this error:
Cannot open Source file error
I can fix that by giving absolute path but that change needs to be done at many place. What should I see to fix this?
Using VC9 nmake to compile code. This is a Makefile based project.

If the included files are from some library your code is using, you'll want to specify the include path with a compiler option. For the Visual C++ compiler the command-line option to specify additional include directories is /I, e.g.:
cl /I ..\shared foo.cpp
You'll need to modify the compiler options in your Makefile accordingly.

Related

Eclipse requires paths to header files even if the path is in the project's include directories

I'm having to use Eclipse for an existing C project that I am adding to and modding and I cannot work out how to set up include paths so I don't have to do things like this:
#include "../other_folder/foo.h"
I have gone into:
Project Properties | C/C++ General | Paths And Symbols
And selected Includes Tab.
Then selected GNU C for the language and then added relative paths. So, for example, added /other_folder into the list of include paths.
However, I still have to put the relative path in for the headers in the source code.
Am I asking for something Eclipse just can't do in expecting to be able to add these paths and be able to do:
#include "foo.h"
rather than
#include "../other_folder/foo.h"
In any source file?
I've even tried putting absolute paths in the project includes, rather than relative paths, but that doesn't work either. What is even weirder is Eclipse gives a message that relative paths are ambiguous!
I have seen this:
Eclipse can't find header file, even though include paths are set
But his solution isn't relevant to this, that wrench thing doesn't show for me. I've trawled through other similar posts, but can't find anything in them that points to what I'm doing wrong.
An example of an output from the compiler if I don't put the path in the source code for a specific issues is this:
Info: Compiling Registers/registers.c to obj/default/Registers/registers.o
nios2-elf-gcc -xc -MP -MMD -c -I../USB3_Controller_BSP//HAL/inc -I../USB3_Controller_BSP/ -I../USB3_Controller_BSP//drivers/inc -pipe -D__hal__ -DALT_NO_INSTRUCTION_EMULATION -DALT_USE_SMALL_DRIVERS -DSMALL_C_LIB -DALT_SINGLE_THREADED -O -g -Wall -mno-hw-div -mhw-mul -mhw-mulx -mgpopt=global -o obj/default/Registers/registers.o Registers/registers.c
Registers/registers.c:2:22: fatal error: pc_comms.h: No such file or directory
I think the -I is the include paths which does suggest it's just not seeing the include paths I have added. Not sure about the double forward slashes either. eg:
/USB3_Controller_BSP//drivers/inc
That seems odd.
On top of all this, if i delete all entries from the include paths, it keeps telling me there's a whole host of invalid paths such as:
Description Resource Path Location Type
Invalid project path: Include path not found (/home/me/Software/company/USB_Controller_Software/software/USB3_Controller/USB3_Controller/comms). USB3_Controller pathentry Path Entry Problem
Anyone have any suggestions as to what I'm doing wrong please? I suspect I'm missing something fundamental here in the way Eclipse sets up include paths.
(Please don't have a go about the mixed styles of directory naming and file naming, this is not of my doing!)
FWIW Using Mars.2 on Linux for NIOS.

GNU gcc compiler cannot handle partial include statements

Imagine that I have a C-project in the following folder:
C:\microcontroller\stm32\myProject
I have two important folders inside myProject:
- source => here are all my .c and .h files
- build => gcc will write all the object files in here
Note: as you can see, the backward slashes indicate that this is happening on a Windows pc.
The figure below gives an overview:
I will not display my complete makefile here, because that would lead us too far. The rules inside the makefile for all .c => .o files are similar. Let us just focus on the compilation of one specific file: fileA2.c:
--------------------- COMPILATION OF FILE fileA2.c -------------------
Building ./build/folderA/fileA2.o
arm-none-eabi-gcc C:\\microcontroller\\stm32\\myProject\\source\\folderA\\fileA2.c
-o C:\\microcontroller\\stm32\\myProject\\build\\folderA\\fileA2.o
-c
-MMD
-mcpu=cortex-m7
-...
-IC:/microcontroller/stm32/myProject/source/folderA
-IC:/microcontroller/stm32/myProject/source/folderB
Notice that the gcc call ends with two include flags: one for folderA and one for folderB. This enables gcc to use any of the header files from these folders (fileA1.h, fileA2.h or fileB1.h) if fileA2.c has an import statement.
Let us now consider the source code in fileA2.c. We assume that this file needs to include fileA2.h and also fileB1.h.
/*******************************/
/* SOURCE CODE fileA2.c */
/*******************************/
// Some include statements
#include "fileA2.h"
#include "fileB1.h"
// Code
...
These include statements work perfectly. The gcc compiler retrieves the files fileA2.h and fileB1.h in the given folders. But I noticed that the following does not work:
/*******************************/
/* SOURCE CODE fileA2.c */
/*******************************/
// Some include statements
#include "fileA2.h"
#include "folderB/fileB1.h"
// Code
...
The last include statement is a 'partial path' to the file. I get the error when compiling:
fatal error: folderB/fileB1.h: No such file or directory
How can I get gcc to handle this?
PS: It is not my own habit to use 'partial paths'. But they appear a lot in the libraries from the silicon vendor of my chip, so I have to live with it.
You specify two paths to look for includes other than the current directory for the source file:
-IC:/microcontroller/stm32/myProject/source/folderA
-IC:/microcontroller/stm32/myProject/source/folderB
You get the error because neither
C:/microcontroller/stm32/myProject/source/folderA/folderB/fileB1.h nor
C:/microcontroller/stm32/myProject/source/folderB/folderB/fileB1.h exists.
To address the error, you can add the following path:
-IC:/microcontroller/stm32/myProject/source
When using double-quotes to include a header file the compiler first looks in the same directory as the current file. If the header file is not found then it continues with the standard include search paths.
So when the compiler compiles the file source/folderA/fileA2.c the first directory the compiler will look for include files is the source/folderA directory. In the first example the fileB1.h will not be found there, but since you added source/folderB to the standard search path it will be found there as source/folderB/fileB2.h.
In the second example there is no folderB/fileB1.h file on source/folderA so the compiler will search the standard search path. When it comes to source/folderB it will again try folderB/fileB2.h (i.e. source/folderB/folderB/fileB2.h) and it will still not be found, nor will it be found anywhere else.
You need to add -IC:/microcontroller/stm32/myProject/source to be able to find folderB/fileB1.h.
Apart of the two correct responses you have received before this, you have the third chance to specify the path to the file in the #include directive from the curren directory, as with
`#include "../folderB/fileB1.h"

Adding custom path to gcc command

i know there a a few posts already about this but I do not seem to be able to get it right.
I am working on a shared project using geany and gcc. The file structure looks something like this:
`/Documents/.../project/ main directory of project with makefile`
`/Documents/.../project/src here are some sourcefiles and headers`
`/Documents/.../project/src/extended here are some other source and header files`
`/Documents/.../project/src/tools other header and source files`
now lets say I am working on a sourcefile in /tools that includes from extened with
#include"/extended/some_header.h"
because my makefile is configured to search for files from /src. However when I am trying to compile the file I am working on right now (by using geany compile option which just calls gcc) I cannot compile it obviously because it cannot find /extended/some_header.h in the /src folder. I have tried adding
-iquotes/Documents/.../project/src
to the gcc call by geany but it doesn't work either.
The -I flag tells the gcc compiler where it should look for the header files. Passing the -Idir to the compiler is appending the dir path to the head of the search list, effectively making this path higher priority than the previously (or system) defined paths. As for the source path - there is no such an option for gcc itself. Each source file passed to the compiler has to have it's path (absolute or relative). In order to work it around, a Makefile can be provided, defining a list of files to be compiled.

How to link a non-standard header file into a C compiler

I'm trying to use a non-standard header file (http://ndevilla.free.fr/gnuplot). Its used in lots of codes in various different places on my computer. Currently I have to put the header file and the object file in every folder which its needed with the preprocessor directive:
#include "gnuplot_i.h"
In the file. Is there a way by which I can put the header file in one place so I can reference it like other standard header file. Cheers.
Compile with -I<directory>
E.g.
compile with -I/usr/local/gnuplot/inc.
Also it might be worth your reading up on include paths and the difference between:
#include <include_file.h>
and
#include "include_file.h"
Linking in an object file needs to be done explicitly the same way as a C file, which means (I believe) that you need a full path. However if you archive it into a proper library then you can use -l<library name> and -L<library path> instead. E.g.
gcc -I/usr/local/gnuplot/inc -L/usr/local/gnuplot/lib -lgnuplot -o my_prog my_prog.c
Most compilers have a flag -I that lets you add a directory of your choosing to the search path for include files.

Copy C files to include

I have a set of C files that I would like to use. Can I just copy them to my include directory, or do I have to compile them. I would think they would be compiled in the final binary.
You need to compile those C files if you want to use them.
To make use of what's in those C files, you'll nead a header file that declares what's inside them.
Those header files is what you'd put in your include folder, and you'll compile the C files together with your other C files. (Or you could make a library out of those C files)
Yes, they need to be compiled so that they are available at the linking step. C is not an interpreted language, so having the sources present in an include directory would do nothing for execution.
You can keep the source files at the same location. The include files will be in the include directory. You can use the compilation option -I./<include-file-directory> to specify from where to fetch the include files.
The final binary will be compiled version of all your source files which you give to the compiler. You have to explicitly specify every file to be compiled along the with final executable name.
In case you dont do so a default executable is created with the name a.out(i am assuming the platform to be linux and compiler to be gcc) in the directory where you compile.
Check the link for more details on compilation using Makefile.

Resources