0

I've found that a small C++ project, with no dependencies, won't compile under Cygwin with either GCC or Clang. Under Ubuntu there are no problems; and I've been working with this code for a couple of years. I'll introduce a MWE.

The problem arises when including a header in a subdirectory, which itself includes another header, found in that same subdirectory, but specified with the subdirectory in the path provided to the include directive. Executing the following commands can replicate the error:

mkdir foo
echo \#include \"foo/includes.hpp\" > inc.hpp
echo \#include \"foo/bar.hpp\" > foo/includes.hpp
touch foo/bar.hpp
g++ -c inc.hpp

On 64-bit Ubuntu 18.10, the final GCC (or Clang) invocation will produce no errors. On 64-bit Cygwin under Windows 10, the following error message is displayed.

In file included from inc.hpp:1:0:
foo/includes.hpp:1:10: fatal error: foo/bar.hpp: No such file or directory
 #include "foo/bar.hpp"
          ^~~~~~~~~~~~~
compilation terminated.

Can anyone shed some light on the issue? (By the way, I myself do well understand how to properly include header files - this is about the difference between Cygwin and Ubuntu.)

user2023370
  • 10,488
  • 6
  • 50
  • 83

1 Answers1

1

foo/includes.hppshould have local includes if you use "".

That would be:

#include "bar.hpp"

The specification for what paths are used for searching headers is custom for all compilers, although includes with "" should be considered as local for the file where you have the include, not the one that you are compiling.

Basically, it's:

  • look in the current folder of the current header being processed for a file with that name
  • use the same paths as <>after

Of course, as I've said, this could change for a new compiler one day. But it is quite safe to assume that this is the behavior for all compilers (What is the difference between #include <filename> and #include "filename"?).

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • So you are saying that an error or lack of error, in this situation, from different compilers is expected behaviour? For clarity, this is to say that GCC on Cygwin is a different compiler than on Ubuntu, and the same for Clang; which is true. But do you really think this difference is what was intended by the compiler package maintainers? – user2023370 Nov 17 '18 at 13:49
  • There can be an error, but in this case, it's very much obvious that you are not abiding by clang/gcc/msvc rules. You are including from `./foo` a header as if you were in the parent folder (`.`), with `""` and without using `-I.`. – Matthieu Brucher Nov 17 '18 at 13:53
  • It is indeed obvious in this small example. I'm just surprised that there are presumably people out there who specifically organise for this difference with each release of GCC and Clang. Surely Ubuntu's GCC and Clang are not compiled in such a different way? My assumption was that it was somehow the environment which was differing. – user2023370 Nov 17 '18 at 13:59