1

I'm following a tutorial on how the various processes of the compiling work and in order to learn the process I'm producing the executable "by hand" by creating the .i file first. In order to do so I'm doing the command:

cpp a.cpp > a.i

a.cpp:

#include<iostream>

int main() {
    return 0;
}

But an error occurs:

a.cpp:1:9: fatal error: 'iostream' file not found
#include<iostream>
        ^~~~~~~~~~
1 error generated.

even though the file a.i still gets created with the content:

# 1 "a.cpp"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 383 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "a.cpp" 2


int main() {
  return 0;
}

Am I supposed to specify the location of iostream library in order to produce the .i file? The tutorial doesn't mention it anywhere and I wonder why is it needed at this point.

Chris
  • 26,361
  • 5
  • 21
  • 42
checkd
  • 23
  • 3
  • 1
    Side note: The tutorial brings up Cygwin a lot. Unless you need Cygwin's POSIX compatibility layer, consider [using MSYS2 instead](https://stackoverflow.com/a/30071634/4581301). If you do need POSIX, a Linux container or VM is usually more effective than Cygwin. – user4581301 Aug 12 '22 at 22:39

1 Answers1

3

In absence of specification, cpp (the C PreProcessor) is assuming the language you're using is C.

Try passing the -x argument to tell it your language is C++.

cpp -xc++ a.cpp > a.i
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 1
    Side note: cpp in this case stands for C PreProcessor, not C Plus Plus. – user4581301 Aug 12 '22 at 22:46
  • now it gives me +20 erros at `/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/AvailabilityInternal.h`like `#endif /* #if defined(__has_builtin) */` – checkd Aug 12 '22 at 22:52
  • 1
    Always deal with the first error first, but that sounds like material for a different question. – Chris Aug 12 '22 at 23:01