Compile C++ Code Using The Terminal Directly Without Save File.cpp [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I need to compile C++ code directly in the terminal or CLI without saving the file
When is use the below way, It shows me an error.
gcc -x c - <<eof
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
eof

You are trying to compile a C++ program using a C compiler.
This works:
g++ '-xc++' - <<eof
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
eof

If you are trying to run a C++ program from a source,
this works:
g++ -xc++ - && ./a out <<eof
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
eof

Related

Problem with the most simple program in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I've encountered an error with the hello world program in c++ vscode, it doesn't print out Hello World also warns me the i don't have gcc
#include <iostream.h>
using namespace stl;
integer main()
{
output << "Hello World"
return 0;
}
The source code won't compile, here is a working hello world program:
#include <iostream>
int main()
{
std::cout << "Hello World\n";
return 0;
}
In addition to this, please make sure you have a compiler.
Looks like u don't have a compiler to compile the code, consider installing gcc or any other c++ compiler first. Your source code also seems to have some syntax errors, try this hello world program:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!\n";
return 0;
}

Can I mix C and C++ in a single program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
can i write code with mixing of c and c++ header file in a programming language?
SUPPOSE
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
cout <<"Hello world"; << endl;
printf("",);
}
What you have is not a mix of C and C++ code. It's C++ code with some elements written using C-style coding, but not actual C language.
You cannot mix C and C++ in a single source file. You can combine C and C++ files in the same program, but each source file must be in one language.
A header file can be written such that the code it contains can be used as both C and C++ code. When included in a C fie it is treated as C and when included in C++ file it is treated as C++. Most headers of C libraries are written this way today. One must do some (small) extra work on a C header towards C++ compatibility. A C header that ignores existence of C++ is likely to be incompatible with C++.
Yes, you can
/* code.cc */
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
cout << "Hello world" << endl;
printf("Hello world\n");
}
However, you need to use c++ to compile the code
c++ -o code code.cc
./code
Hello world
Hello world
In case described by François you need to go slightly different way
/* code_c.h */
extern "C" {
void printMessage();
}
Code that needs to be compiled using C
/* code_c.c */
#include <stdio.h>
void printMessage() {
printf("Hello world!\n");
}
C++ code
/* code.cc */
#include <iostream>
#include "code_c.h"
using namespace std;
int main()
{
cout << "Hello world" << endl;
printMessage();
}
Gluing all together
cc -c code_c.c
c++ -c code.cc
c++ -o code code.o code_c.o
./code
Hello world
Hello world!
Crazy mode [on]
In fact, you can even "mix" C++ with "bash" ;)
code_c.c
echo =1/*"beat that ;)" | tail -c13
#*/;int fun(){return printf("beat that ;)\n");}
code.cc
extern "C" int fun();
int main() { return fun(); }
compilation and execution
> c++ -c code.cc
> cc -c code_c.c
> c++ -o code code.o code_c.o
> ./code
beat that ;)
> bash ./code_c.c
beat that ;)
Crazy mode [off]

c++ : invalid operands to binary expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I don't know what's wrong here ?
It's just running errors !!!
#include <iostream>
using namespace std;
int main()
{
cout << string("hello world");
return 0;
}
Read more about C++. So read first Programming -- Principles and Practice Using C++.
Then read C++ reference documentation, notably the one about std::string-s.
You need to #include <string>
You should enable all warnings when compiling. If using GCC, compile with g++ -Wall -g
You don't need that string before the actual string:
#include <iostream>
using namespace std;
int main()
{
cout << "hello world";
return 0;
}
Or, alternatively, if you're trying to store a string:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str = "hello world";
cout << str;
return 0;
}

Error while compiling a C++ program with G++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
v This is main.cpp:
#include <stdio.h>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
To compile it, I go into cmd and type g++ main.cpp but it gives me 2 errors, saying that both cout and endl aren't declared. I can only imagine that it's because it can't find the namespace std or can't include <stdio.h>. How would I make this work?
You have to include iostream, not stdio.h to use std::cout and std::endl.

"{" missing function header (old style format list) - expected a declaration [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm really really new to C++ and this is my first program on Visual Studio 2015, It shows me 2 errors:
"{" missing function header (old style format list)
expected a declaration
#include "stdafx.h"
#include <iostream>
int main();
{
cout << "Hello World";
return 0;
}
int main() remove ; at the end.
#include "stdafx.h"
#include <iostream>
int main(); // this ';' is giving problem remove it.
{
std::cout << "Hello World"; // use std::cout
return 0;
}