0

I try to compile the following simple c++ file(a.cpp) with gcc-4.9.

#include <iostream>
#include <foo>

using namespace std;

int main()
{
  int x = 3;
  cout << f(x) << endl;
  return 0;
}

"foo" is also the following simple c++ file (remove include guard to make code more simple).

int f(int ret){
    return ret + 1;
}

I cannot compile a.cpp with gcc-4.9 because the compiler say:

a.cpp:2:15: fatal error: foo: No such file or directory

I think the compile error is caused because the user defined file is included as standard header file (put parentheses around, "<***>").

Before, I see the same error and know two methods to solve these compile errors.

First one is changing compiler gcc-4.9 to gcc-5.0.

Second one is replacing

#include <foo>

with

#include "foo"

.

Although I don't know why compile error is solved by theses methods, I do it since now.

But now, I see a code that cannot be solved by a second method. It includes many user defined file as standard file in many header file. So, by second method, we must edit the many header files.

Also it is solved by first method, but I wan to compile with gcc-4.9 now. I think If there exists a method to compile "a.cpp" by gcc-4.9 without editing, the codes can be compiled by the same method.

How can I compile "a.cpp" by gcc-4.9 without editing. And why I can compile "a.cpp" by gcc-5.0?

rosetta-jpn
  • 13
  • 1
  • 5

3 Answers3

2

#include "file" will look for file in the same directory as the file including it. #include <file> relies solely on the include path so you need -I. (or whatever the path is) on your compile command line.

John3136
  • 28,809
  • 4
  • 51
  • 69
0

#include <foo> and #include "foo" and not the same thing.

First only looks for foo file in standard places, while the latter looks first in current directory and then in standard places. That's the reason why you should use #include "foo" for local include files and never #include <foo>.

But the list of standard places is configurable with the -I option : gcc -I/path/to/include/dir -other_options source.c -o executable. Just do man gcc, or info gcc if using linux for more details.

And even if -I. option could allow you to always use #include <foo> please do not do that if you want your code to be readable by others.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • *First only looks for foo file in standard places, while the latter looks first in current directory and then in standard places.* You have that backward. – R Sahu Jul 27 '15 at 06:12
0

Where are the files include cannot find? I'm guessing a subdirectory.

In which case the answer is to either add the subdirectory to the search path or change the include to

#include "subdirectory/name_of_file" 
TLOlczyk
  • 443
  • 3
  • 11