2

I'll use openssl installed header structure for this example:

/usr/include       (or some folder in include search path on Windows box)
  |
  + --openssl
       |
       +-- e_os2.h
       +-- rsa.h
       +-- sha.h
      ...

/usr/include is in compiler include search path. OpenSSL header are commonly included this way: #include <openssl/sha.h>

As a first line, openssl/sha.h has this include: #include <openssl/e_os2.h>. So, my question: is it really good idea for installed header to refer to header in the same folder that way? When it refers to e_os2.h that way it's possible that it picks up e_os2.h from some other location, not necessarily in the same folder as sha.h. For example, if I had a local copy of openssl includes in some location and included that sha.h this way: #include "../../3rdpath/openssl/sha.h then I may get some nasty bugs in my code by combining incompatible version of headers.

Considering the fact that compilers behave differently regarding #incldue <...> vs #incldue "..." what would be proper way for a lib like openssl to include it's headers?

I thought, that the way openssl does is the most wrong way to do it. The other two ways are:

a) #include "e_os2.h"

and

b) #include "./e_os2.h"

The openssl way is:

c) #include <openssl/e_os2.h>

Is that bad decision to do the way it's done in openssl? Any problems with a) or b)? b) means to include e_os2.h only from the same folder, is that guaranteed with all major compilers (ms cl, armcc, intel cl, gcc et all)?

1 Answer 1

3

Your first option is the most open to doing the wrong thing; it'll find files by the given name in packages of headers belonging to other libraries altogether. Your second one at best gives undefined behavior; I wouldn't expect it to work most of the time. The third way is the correct way; it will only include openssl's version of the file. If you arrange to replace some header by putting another incomplete openssl directory in your include path, then I'd assume you know what you're doing.

Sign up to request clarification or add additional context in comments.

4 Comments

well, gcc and cl for example behave quite differently regarding < > and " "? and I'm pretty sure that it will work in ms compiler properly. The problem is that in some code that I use I need to use different version of libs and if I do it the opensll way I'll get mysterious bugs (because of complete ABI incompatibility).
and why is #include "./file.h" gives undefined behavior? As I understand, all that include preprocessor mechanism is pretty much "do whatever" and is implementation detail of the compiler. My includes are full of references like #include "../../some_file.h" how is that different than #include "./some_file.h"? (this code is compiled without any problem on many compilers/systems)
A path like "../../some_file.h" is not guaranteed to be interpreted relative to the include file it's found in, or relative to the source file, either. It'll be interpreted relative to the include path for those compilers that support this idea. Therefore you have less assurance about what header you're going to get, not more.
just to clarify, I really don't care for all BS in the standard, I simply care for a working solution that works in GCC and MS compiler on a normal OS (windows, linux etc). Considering this, is c) the right way, and the other two are simply wrong? Do I need to have -I. in gcc to get that working properly? Definitely, I have no issues with the MS compiler (it's my primary dev tool). With GCC is seems to work also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.