Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: 'stdafx.h' file not found

Tags:

c++

c

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?

like image 901
JackieZ3895 Avatar asked Mar 24 '14 22:03

JackieZ3895


3 Answers

  1. stdafx.h is the precompiled header used by Visual Studio, you do not need this.
  2. You seem to have missed out the int main() function
  3. It is std::endl not std::end1

So something like this:

#include <iostream>
int main() {
     std::cout << "Hello World!" << std::endl;
     return 0;
}
like image 191
Chris Drew Avatar answered Oct 16 '22 23:10

Chris Drew


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.

like image 21
Jayesh Baviskar Avatar answered Oct 16 '22 22:10

Jayesh Baviskar


Two problems: a) stdafx.h is not needed (as others noted). b) 'end1' should be 'endl' (note the letter 'l' vs. the number '1').

like image 1
Andy Avatar answered Oct 16 '22 23:10

Andy