craig65535
craig65535

Reputation: 3572

Is there a way to ask gcc to treat #include <> like #include ""?

Is there a compiler or preprocessor flag that will force gcc to treat #include <x.h> like it would #include "x.h"? I have a bunch of generated code that uses #include <> for files in the current directory, and gcc reports No such file or directory for these files. I'm looking for a workaround that doesn't involve editing the code.

EDIT: -I. doesn't do it. Let's say I have the following files:

foo/foo.h:

#include <foo2.h>

foo/foo2.h:

#define FOO 12345

xyz/xyz.c

#include <stdio.h>
#include "foo/foo2.h"

int main(void)
{
    printf("FOO is %d\n", FOO);
    return 0;
}

If, inside the xyz directory, I compile with gcc -o xyz I.. xyz.c, the compile fails:

In file included from xyz.c:2:
../foo/foo.h:1:18: error: foo2.h: No such file or directory
xyz.c: In function ‘main’:
xyz.c:6: error: ‘FOO’ undeclared (first use in this function)
xyz.c:6: error: (Each undeclared identifier is reported only once
xyz.c:6: error: for each function it appears in.)

Adding -I. doesn't change anything.

But, if I change foo/foo.h to:

#include "foo2.h"

Then the compile works. I know I could add -I../foo to my command line, but I was looking for a more generic way to treat #include <> as #include "". Does one exist?

Upvotes: 3

Views: 151

Answers (2)

ech
ech

Reputation: 138

The -I- option might help you. From gcc's man page:

   -I- Split the include path.  Any directories specified with -I options
       before -I- are searched only for headers requested with
       "#include "file""; they are not searched for "#include <file>".  If
       additional directories are specified with -I options after the -I-,
       those directories are searched for all #include directives.

Upvotes: 3

Greg Hewgill
Greg Hewgill

Reputation: 992947

Yes, you can pass the switch -I . to the compiler to add the current directory to the include search path.

Upvotes: 6

Related Questions