Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does compilation of this simple C++ program using 'cpp' fail?

Tags:

c++

c

I am a beginner in C++

I am average at C.

I have written the following code in C++ (file.cpp)

#include <iostream>

int main(){

   std::cout<<"My name is ANTHONY";
}

Then I tried to compile the above code using cpp file.cpp but got some errors. I don't know whats wrong

When I tried to compile my C program (changed <iostream> to <stdio.h> and std::cout to printf) using cc file.c, I didn't get any errors.

What is happening here?

like image 292
Anthony Avatar asked Sep 17 '10 05:09

Anthony


1 Answers

Then I tried to compile the above code using cpp file.cpp but got some errors.

That is because cpp is C(C++) preprocessor. It is a separate program invoked by the compiler (g++) as the first part of translation.

Try compiling your code using g++ file.cpp. :)

like image 158
Prasoon Saurav Avatar answered Oct 21 '22 16:10

Prasoon Saurav