Reputation: 2987
I have a question on the relative position of C header files. I noticed that my code will work if the C header files were placed in a particular position, otherwise it will fail without errors.
Any reason why? Sorry to ask, newbie here.
Upvotes: 1
Views: 664
Reputation:
It depends on where your compiler is taught to check for them. On most typical Linux systems, if you do this:
#include <stdio.h>
The compiler will assume that you meant:
#include </usr/include/stdio.h>
Whereas if you type:
#include "config.h"
And config.h is not in the current directory, and you haven't taught the compiler to (via an -I
switch on my compiler) to look for it elsewhere, it won't be able to find it.
Or, perhaps you want to tell the compiler that you don't want to use the hosted C library headers via a switch like -nostdinc
, which means educating it on the location of everything that you want included.
It may be worth your while to spend some time looking at your compiler's documentation, if only to learn more interesting things that it can do :)
Note, we're talking headers and include paths here, not linkage.
Upvotes: 0
Reputation: 56381
The problem is that importing headers in C and C++ are basically code insertion, similar to a macro, into that point in the file that is being compiled.
If two different header files define the same symbol (for example, there are dozens of different possible failure scenarios with header files) you'll get a compiler error. You don't provide enough information to track down your specific problem, but in general, header file order should not matter -- unless the header file is poorly written.
I suggest you perform a very thorough review of your header files.
Upvotes: 4
Reputation: 34517
Not enough information; I'd suggest compiling with a lot of warnings (e.g. -Wall if using gcc) and paying attention to them. For instance you could be redefining some type, e.g. a struct, with a different size and corrupting memory. Assuming by "fail w/o errors" you mean crashing. The warning will tell you that you are redefining something.
Always pay attention to all compiler warnings.
Upvotes: 3