I am new to programming C++ and am trying to learn myself through websites (learncpp.com) although I am already stuck on compiling my first program =( . They use Visual Studio to program their code and because I am using a macbook, I just use vi and terminal (or should I use something else?)
Here's the helloworld.cpp program I wrote based on the tutorial:
#include "stdafx.h"
#include <iostream>
{
std::cout <<"Hello World!" <<std::end1;
return 0;
}
when I compiled (gcc -Wall hello.cpp) I get the error :
helloworld.cpp:1:10: fatal error: 'stdafx.h' file not found
#include "stdafx.h"
^
1 error generated.
Can anyone give me insight on to how to fix this?
stdafx.h
is the precompiled header used by Visual Studio, you do not need this.int main()
functionstd::endl
not std::end1
So something like this:
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
stdafx.h
is a Precompiled Header file and it is specific to the Visual Studio.
Precompiled Header file is worthless unless you are facing slow compilation Time. In your program, you don't need them at all, so you can remove that and everything will be fine.
You might be guessing if it is not needed then why we include them?
I will Explain it:
Whenever we add header files (#include
), The compiler will walk through it, Examine it and then compile the header file whenever CPP file is compiled.
This process is repeated for each and every CPP file that has header file included.
In case if you have 1000 CPP files in a project which has to say xyz.h header file included then the compiler will compile xyz.h
file 1000 times. it may take a noticeable time.
To avoid that compiler gives us the option to "precompile" the header file so it will get compiled only once to speed up the compilation time.
Two problems: a) stdafx.h is not needed (as others noted). b) 'end1' should be 'endl' (note the letter 'l' vs. the number '1').
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With