How to set include path in xcode project - c

I am trying to use a C library in an Objective-C Xcode project.
The libraries directory structure is as follows:
-- include/
|-- config.h
|-- lib/
| |-- file1.h
| |-- file2.h
| |-- file3.h
The library's docs say to include file1.h, and file1.h includes file2.h and file3.h.
I am getting "file not found" errors for the includes of file2.h and file3.h`.
They are included by file1.h in the following manner:
#include <lib/file1.h>
#include <lib/file2.h>
I read here that these angle-brackets instruct the preprocessor to search for include files along the path specified by the INCLUDE environment variable, as opposed to searching in the same directory as the file that contains the #include.
So I added the INCLUDE environment variable in Xcode by going to Product->Edit Scheme.. and set it to /the-whole-path-to/include/ however, I am still getting the file not found errors.
The files are successfully included if I change file1.h to include them like this:
#include "file2.h"
but I'd rather not do that for every file in the library.
How can I fix this?

In version 5.0.2 of XCode, the best way to accomplish this is probably to add it to the target's "Search Paths" pane. Locating this was (for me) incredibly unintuitive. Here's how I got to it, for those as confused as I:
In the left column, click on the Project Navigator icon (looks like a folder). Then, click on the project entry. This should show a bunch of settings in the main pane. At the top of this pane, click on "Build Settings. This shows a bunch of entries... including one called Search Paths... but you can't add a search path here! This made me gnash my teeth for quite a while, until I figured out that the name of the project at the top of this pane was a pull-down; choose the target from this pull-down, and you should now be able to double click on "Header Search Paths" and perform the needed edit.
Oh, the joy of crazy GUIs.

Figured it out.
All you have to do is add the -I flag to your build setting under "Other C Flags"
So in your target's build setting search for "Other C Flags" and add -I/path-to-include/
Here's a screenshot:

Try this:
1 - select your project file in the left Xcode pane
2 - make sure your project is selected in the middle Xcode pane
3 - select "Build Settings" at the top of the middle Xcode pane
4 - be sure "All" & "Combined" are selected just beneath "Build Settings"
5 - type header in the search field just below "Build Settings"
You should see the search path fields ready for editing in the middle pane.

I solved this in Xcode 5.0.1 using the project Build Settings (as John and Ian noted above, but I cannot comment due to <50 rep).
New info:
When adding includes to User Header Search Paths, I also had to change Always Search User Paths to Yes.
When adding includes to (non-User) Header Search Paths, Always Search User Paths is not required.

Although this works, it is probably better to put it under the "Search Paths" tab instead.

Either you can use "Other C Flags" or use "HEADER_SEARCH_PATHS", to specify the include paths, to look for header for your executable.

In 2021, Xcode v. 12.4, the solution seems to be:
Project->Targets->General->"Scan All Source Files For Includes"-> set to "Yes"
This worked for me.
However, this might backfire if you have multiple versions of a specific .h file, probably not a good practice but it's possible if you have lots of sub-directories with their own mini-projects and similarly named include files.

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.

Keil Uvision 5 adding header files and source files?

I started with an example project in keil from Nordic SDK. This question is not about the nordic sdk, but rather how Keil connects header files and source files. After what I can understand Keil hides all header files merged into the main.c file, see figure below.
I want to add my own header file called "physical.h" where I define additional hardware that I added.
Sometimes I want to add a header file and source files "*.c" files.
What can I specifically do to add a new file, and compile this new file with my main.c file? A walkthrough would be appreciated.
I believe Keil is not intuitive in the folder structure sense. However, it is probably good for a modular design as it can easily include files from many places on your computer (git, library etc...).
Let's start with problem one (adding a header file):
The header files in the main.c file is an image of whats already included and "working". Add a new file by right clicking the parent folder of main.c and click "Add new item to group ". From within the menu, choose C-file/H-file or any other appropriate file.
If there is no group yet, just add a group from the parent folder.
Right click and press "add group ...".
Now, what might confuse you is that the .h file will not appear in the folder you just created it. The .c files will appear, but not .h! You will find your file at the open in a new window. Right click that tab with the filename you just created, and click "copy full path".
Here comes the tricky part.
Go to the project tab in Keil menu, and click "options for target " or ALT+F7.
Go to the tab "C/C++" tab. In the "Include Paths" slot, the will be a button indicating "...", click it.
In the top right corner, holding over the first icon will show "New (insert)", click it. Click the new button "..." in the slot you just created, this will open a folder menu. This is where you find your file and add the parent folder of that file. However, you may need to move the file you created in order to find a good structure.
The structure inside Keil Uvision 5 is just virtual folders, and creating a file will just put it in the project folder with no structure. Finding a good solution now, is up to you, comment if you come up with one.
The last part I would like to add is what happens when you just included this file. Well, go into your main.c file, include the header file, as in normal C convention. #include "myfile.h". If you try to compile, it should show no error, and your main.c file will have a sub-file of that same .h file you just included.
Second problem:
For C-files you can just add them, the same way as earlier, however, Keil will dump them all at the source of your project, having no structure at all. For structure, you must do that outside of Keil unfortunately.

Make Eclipse CDT pre-include a header file, to avoid error: "Symbol <symbol> could not be resolved"

Is there a way to make the Eclipse editor presume that a specific C header file has already been included, without having to #include it explicitly?
For example, how can we achieve:
#include "common_type_defs.h"
#include "special_type_defs.h" // Don't want to have to mention this header file
main()
{
common_type var1;
special_type var2;
.....
}
by writing only:
#include "common_type_def.h"
main()
{
common_type var1;
special_type var2; // Eclipse editor: "Symbol 'special_type' could not be resolved"
.....
}
without getting the Eclipse editor annotation error: "Symbol 'special_type' could not be resolved".
The reason is, the project uses a custom scripted build system. The special header files are added automatically by the build system, selected from different libraries. So the build succeeds.
I have added the special header folder to the include paths of the project. This allows me to hit [F3] and jump to the definition of "special_type". It is just that the editor flags an error.
I do not want to silence the error because I want to catch real errors.
Any suggestions?
Go to:
Project properties → C/C++ General → Preprocessor Include Paths, Macros etc. → Entries → GNU C
Select CDT User Setting Entries and than click Add button. Select Include File and enter your preprocessor pre-include file here.
Apply and rebuild indexer.
I am using Oxygen.1a Release (4.7.1a)
Do additional define in your build system and then:
#ifndef CUSTOMBUILDER
#include "special_type_defs.h" // Don't want to have to mention this header file
#endif
I ended up creating different "build configurations", for each build option of the build system. There I can add the background header files, as required.
One disadvantage is that I must maintain the different build configurations to mirror the build system: when new header files are added to the build system, the same files must also be added to the eclipse build configuration. So this solution will be unsuitable for big team projects where multiple people frequently change the included files because you could easily miss a file change or two. But it works well for small teams and infrequent changes.

How to define relative paths in Visual Studio Project?

I have a library and a console application that uses a library. The library has a folder with source and header files.
My project is in a child/inner directory but that library directory that I want to include is in a parent/upper directory.
My project directory:
H:\Gmail_04\gsasl-1.0\lib\libgsaslMain
Includes files are here:
H:\Gmail_04\gsasl-1.0\src
How can I use paths relative to the project directory, to include folders that are in a parent/upper directory?
Instead of using relative paths, you could also use the predefined macros of VS to achieve this.
$(ProjectDir) points to the directory of your .vcproj file, $(SolutionDir) is the directory of the .sln file.
You get a list of available macros when opening a project, go to
Properties → Configuration Properties → C/C++ → General
and hit the three dots:
In the upcoming dialog, hit Macros to see the macros that are predefined by the Studio (consult MSDN for their meaning):
You can use the Macros by typing $(MACRO_NAME) (note the $ and the round brackets).
If I get you right, you need ..\..\src
I have used a syntax like this before:
$(ProjectDir)..\headers
or
..\headers
As other have pointed out, the starting directory is the one your project file is in(vcproj or vcxproj), not where your main code is located.
By default, all paths you define will be relative. The question is: relative to what? There are several options:
Specifying a file or a path with nothing before it. For example: "mylib.lib". In that case, the file will be searched at the Output Directory.
If you add "..\", the path will be calculated from the actual path where the .sln file resides.
Please note that following a macro such as $(SolutionDir) there is no need to add a backward slash "\". Just use $(SolutionDir)mylibdir\mylib.lib.
In case you just can't get it to work, open the project file externally from Notepad and check it.
There are a couple of hints you need to know.
consider your app is running under c:\MyRepository\MyApp
a single dot on your path means the folder where your app runs. So if you like to reach some folder or file under MyApp folder (imagine c:\MyRepository\MyApp\Resources\someText.txt) you can do it like var bla = File.Exists(./Resources/someText.txt)
and you can go one level up with double dots (..) think about a folder under c:\MyRepository\SomeFolder\sometext.txt
for MyApp, it will be like
var bla = File.Exists(../SomeFolder/someText.txt)
and it is possible to go 2,3,4.. levels up like
../../SomeFolder (2 levels up)
../../../SomeFolder (3 levels up)
and path starting with no dots means the drive root. var bla = File.Exists(/SomeFolder/someText.txt) will look for the c:\SomeFolder\someText.txt in our scenario.

How to tell the preprocessor to search for a particular folder for header files, when I say #include <xyz.h>

I have around 120 header files (.h files) , and in all of them each one includes many other header files using #include <abcd/xyz.h>, but as I kept .h files in a specific folder, preprocessor is generating filenotfound error.
I moved all the .h files to the single .C file that is calling the first headerfile.
One way to do is make #include <abcd/xyz.h> as #include "abcd/xyz" , but I need to do this in all the header files wherever there is an include statement, and there are hundreds of them.
I can't include many of them in the headerfiles section in Visualstudio because, some of the headerfiles have the same name, but they reside in different directories. (<abcd/xyz.h>,<efgh/xyz.h>).
Any way to do this?
You should add a path into "Additional include directories" in the "C++" section of the project options (the "General" tab). You can use environment variables as well as "this folder" (.) shortcut and "up one folder" (..) shortcut for this setting to not be bound to a certain directory structure.
and I can't include many of them in the headerfiles section in Visualstudio because , some of the headerfiles have the same name, but they reside in different directories.(,)
That's a pretty big problem unless the files that are including those non-uniquely named headers are in the same directory as the header files themselves.
You have no way to guarantee that the compiler will locate one header before another without modifying the #include directive itself (and adding a relative path as one example).
EDIT: It looks like Visual Studio will allow you to specify different Additional Include Directories for each source file in a project (rt-click on the source file in Solution Explorer and modify C/C++ properties). But I think this would be more work than modifying the #include directives themselves - depends on how many non-unique header filenames you have.
In the project settings (under C/C++ in VS2005/2008) there's an option for "additional include directories". You can add the folders containing your header files here, using relative paths.
You can also do this at the IDE level in Tools -> Options -> Projects and Solutions -> VC++ Directories -> Include Files. Typically this method is reserved for headers included as part of a formal library. The first option is typically preferred as it's portable (you can ship your project file to another developer and, provided you use relative/macro'd paths, they can build the project as-is).
What you're looking for is the -I flag and you give the directory...
If you have a Makefile, you should add it to the CPP_FLAGS something like that....
You can also add an INCLUDE variable to your environment variables.

Resources