Adding custom path to gcc command - c

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.

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.

Can I save a C static library anywhere I want?

I have created a library in C and want to link it when compiled. Do I have to save in a certain folder in my computer's file system or can I create my own file structure within my application to save it?
Update: My error turned out to be not properly including a header file. My files and linker were fine but it was simple syntax error.
You can save it wherever you want. Just make sure that the compiler knows the location. In the case of gcc for example, you can use:
gcc -L path/to/libdir -l:mylib.a ...
(assuming mylib.a is in path/to/libdir)
Or even:
gcc path/to/libdir/mylib.a ...

New .h file in /usr/include linux

I developed small c application in linux. For this application i placed .h file in
linux standard path (/usr/include). Again i am compiling the same program
Output:
FATA ERROR : xyz.h(my own header file) not found
Do i need to update any variable in gcc or way to solve this problem
Thank You
Place the header file in the same directory as your .c file and use -I. when compiling
gcc -I. main.c -o myprog
You shouldn't place your header files in /usr/include that is meant for the system headers.
Note: you don't actually need the -I. because the current directory is searched by default, nevertheless, it doesn't hurt to add it.
Files specified by include directives are meant to be located in one of the search paths of the complier which are specified with the -I option in many cases (at least for gcc, is it the same for other compilers?). The search paths are verified in the order of definition in the command line.
There are 2 kinds of include directives:
double quoted ones (#include "xyz.h")
angle bracket ones (#include <xyz.h>)
IIRC, the default and first search path for the former is the working directory. For the later, it's compiler dependant, but it's usually /usr/include/.
Depending of the include directive you used, you should pick the right location for your file. Or better, put your file in a good location (say the same place as the including file), and add a search path to your gcc command.
You should separate your header .h files, from system and repository built headers so you don't break anything.
I would recommend making a folder in your home directory called include and just adding it to your path, that way you never have to worry about it again and no need for the -I/flag

C/C++ How to access header files?

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.

Including a library (lsusb) in a C program

I am still fairly new to programming with C and I am working on a program where I want to control the power to various ports on a hub I have. That is, however, not the issue I am having right now.
I found a program online that does what I want I am trying to compile it. However it uses #include<lsusb.h>. lsusb is located in a totally different folder than the file I am wanting to run (and not in a sub folder) and when I try to compile it, I, logically enough, get the error that the file lsusb.h is not found.
How can I link to this file so that it can be found?
This is more of a GCC toolchain question than a C question (although most C compilers do use the same Unixy flags).
The braces around the include file (<>) indicate you want the compiler to search its standard search path for the include file. So you can get access to that new include file either by putting it into a directory on your standard include file search path yourself, or by adding its directory to the file search path. With GCC you do the latter by giving gcc the flag -I"directoryname" where "directoryname" is the full file path to where you are keeping that new include file of yours.
Once your compiler finds it, your linker may have the exact same problem with the library file itself ("liblsusb.a"?). You fix that the same way. The flag GCC's linker will want is -L instead of -I.
See the "-I" parameter in the gcc man page. It allows you specify a directory in which to find a header file. See also -l and -L.
Or try #include "../../path_to_the_file/lsusb.h"

Resources