When creating a new C project in a particular Eclipse environment which uses GCC, I run into a peculiar linker problem:
Fatal error: my_header.h: No such file or directory.
I get this problem since "my_header.h" resides in a sub folder. After investigation, I found out that you need to include sub folders in the GCC include path (option -I
). How this is done seems to vary between different Eclipse implementations, but it should be something like
Project -> Properties -> C/C++ Build -> Settings -> Compiler -> Includes
Where "compiler" may have a different name in different implementations, and "includes" may be called "input" or similar.
There should be an option to add the include path (option -I
) where you can set the path relative to the specific project, by clicking an "Add" icon followed by Workspace button, then select the directory. Eclipse then generates a path, which should look something like
"${workspace_loc:/${ProjName}/app}"
Do this for all sub folders in the project (and their sub folders).
But despite doing the above for the relevant folder, I still get the "no such file or directory" error. What could be the problem?
(I'm posting this Q&A style since I want to share the solution of this problem with others)
Select C/C++ General -> Path and Symbols. Select Includes tab. In Languages list, select 'GNU C' or whatever C compiler tool chain you use. Press 'Add...' button and add the directory for the include files.
Visual Studio looks for headers in this order: In the current source directory. In the Additional Include Directories in the project properties (Project -> [project name] Properties, under C/C++ | General). In the Visual Studio C++ Include directories under Tools → Options → Projects and Solutions → VC++ Directories.
The reason for this error is that there is no sanity check for the gcc include path. Despite giving a relative path to Eclipse as described, Eclipse will still pass an absolute path to gcc -I
.
Suppose you have your project located at a path like:
C:\åäö\workspace\project
and the sub folder located at
C:\åäö\workspace\project\std
where "åäö" is any string containing any non-standard ASCII letters. In this example I used Swedish, but you'll encounter such non-standard letters in most languages (French, German, Spanish etc).
The problem is that Eclipse passes the full path, rather than the relative one to GCC, and then there is some sort of symbol table mishap. So rather than getting the expected
-I"C:\åäö\workspace\project\std"
you might get random garbage letters such as:
-I"C:\@!#\workspace\project\std"
The path doesn't make sense and the include path is not sanity checked, so you get no diagnostic telling you about this, unless you read the console output in detail. Instead, Eclipse silently pretends that it has added your include path to the list of paths it should check, even though it has not.
The only solution seems to be avoiding placing your projects below paths that contain non-ASCII letters. It would seem that this is a bug in several implementations of Eclipse that use GCC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With