I am trying to understand the C++ compilation process in more detail, so I tried to see how the result of C++ pre-processor looks like:
#include <iostream>
int main()
{
// I am a comment!
std::cout << "Hi!" << std::endl;
return 0;
}
I then ran:
g++ -E main.cpp > preprocessed
To run just the preprocessor.
The output is a very long file, since the <iostream>
header got expanded along with everything it includes. However, the end of the file looks like this:
...
namespace std __attribute__ ((__visibility__ ("default")))
{
# 60 "/usr/include/c++/4.9/iostream" 3
extern istream cin;
extern ostream cout;
extern ostream cerr;
extern ostream clog;
extern wistream wcin;
extern wostream wcout;
extern wostream wcerr;
extern wostream wclog;
static ios_base::Init __ioinit;
}
# 2 "main.cpp" 2
int main()
{
std::cout << "Hi!" << std::endl;
return 0;
}
Expectedly, the comment disappeared, but the preprocessor added some information on lines starting with #.
Are lines beginning with # legal C++? I thought that the sole purpose of # is to designate preprocessor directives. I thought that # is some esoteric way to designate a comment, but this code:
#include <iostream>
int main()
{
# I am something that follows the hash symbol.
std::cout << "Hi!" << std::endl;
return 0;
}
Does not compile.
EDIT:
Apparently # symbols are ok outside the function scope:
g++ -E main.cpp > preprocessed.cpp
g++ preprocessed.cpp -o preprocessed
produces the same executable.