I'm just trying to get my c++ code to output properly in terminal on my mac, but it doesn't show anything. I'm using xcode as a text editor, saving the file as Code.cpp, and then typing g++ Code.cpp into terminal. Before it was showing errors when my code had bugs, but now that it runs correctly it doesn't show any output. Any thoughts? Here's my code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
Here's what I put into terminal, and it just skips down to the next line without the "Hello World" output.
jspencer$ g++ Code.cpp
jspencer$
Thanks in advance for the help!!
g++ is a compiler. It turns your source code into an executable program, but doesn't run it.
You must run the program yourself. The default name of the program generated by g++ is a.out (for historical reasons), so you would run it as
$ ./a.out
If you want to choose a different name for your program, you use the -o option:
$ g++ Code.cpp -o myProgram
$ ./myProgram
But here's how I would write your program:
#include <iostream>
int main() {
std::cout << "Hello World\n";
return 0;
}
See here and here for some reasons.
Related
I have a C++ program which I am compiling on a x64 machine running Windows 10 with g++ x86_64-win32-seh-rev2 v7.1.0, using the command g++ -g main.cpp. When I run my program, I get the error 0xc000007b. This is my code
#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}
When I compile with this code
#include <stdio.h>
using namespace std;
int main() {
printf("Hello World");
return 0;
}
It works fine. When I run it in gdb, it runs fine.
I have seen other posts where there are dlls being used that do not support the architecture, but I don't think I am using any dlls in this application, unless they are being added by g++
This is my code in the C++ file:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
My compiler is Clang. Yes I do put "clang++" in the terminal. The problem is, is that it runs it and doesn't output anything. It just starts a new line.
I am running Clang version 3.8.0.
As you said clang++ is the compiler, that is, it turns your code into a binary file that can be executed. It doesn't execute the program itself.
By default the compiled file name is a.out, so after compiling, to execute the program you should run:
./a.out
You could specify the output file name like so:
clang++ infile.cpp -o outfile
and then run the binary:
./outfile
I'm trying to write my first code on ubuntu terminal using c++
.I created a new cpp file named aaa by
"nano aaa.cpp"
then inside I wrote
#include<iostream>
using std::cout;
using std::endl;
int main(int argc, car** argv)
{
cout << "hello" << endl;
return 0;
}
i saved and got out but when i tried typing
g++ aaa.cpp
I got the error
error: ‘endl’ was not declared in this scope
cout << "hello" << endl;
where did I go wrong
I tried
$ sudo apt-get remove g++ libstdc++-6.4.7-dev
$ sudo apt-get install build-essential g++-multilib
but it was no good
any help?
Stylistically, I prefer to be explicit: std::cout and std::endl.
#include <iostream>
int main(int argc, char** argv) {
std::cout << "hello" << std::endl;
return 0;
}
This also fixes a tyo of yours: char, not car and repairs the #include.
This works as expected:
$ g++ -Wall -pedantic -o foo2 foo2.cpp
$ ./foo2
hello
$
If you wanted to, you could also use
using namespace std;
but as stated, I prefer to more explicit form.
Edit: Nothing as much fun as debating the beancounters. OP question is likely having _another error he is not sharing. His code, repaired for char actually builds:
$ cat foo3.cpp
#include <iostream>
using std::cout;
using std::endl;
int main(int argc, char** argv) {
cout << "hello" << endl;
return 0;
}
$ g++ -Wall -pedantic -o foo3 foo3.cpp
$ ./foo3
hello
$
Ubuntu 16.04, g++ 5.4.0
First, make sure you have the tools you need to be able to compile a C++ code on Ubuntu. For that run the following code in the command line :
This line will install all the basic stuff you need for compiling a C++ code, it will install C, C++, and make.
sudo apt-get install build-essential
Now that you have all you need, I will suggere to explicetely using std::cout / std::endl . That way you don't import all the stuff available under the namespace std that you are not using. Using std::cout / std::endl shows clearly the origin the instance you are using.
Notice : you have an error in the main function argument, namely : car, it should be char
#include<iostream>
int main(int argc, char** argv)
{
std::cout << "hello" << std::endl;
return 0;
}
Now you can compile and run your code this way :
in this example I'm calling the executable file "hello"
g++ -Wall -o hello aaa.cpp
./hello
In my Homebrew installation my libraries are compiled with clang, whereas I would like to, for performance reasons, compile my scientific code with gcc. In order to understand this problem better, I have created a minimal test:
// FILE print.cxx
#include <string>
#include <iostream>
void print_message(const std::string& message)
{
std::cout << message << std::endl;
}
// FILE test.cxx
#include <string>
void print_message(const std::string&);
int main()
{
std::string message = "Hello World!";
print_message(message);
return 0;
}
This code I compile with:
// SCRIPT compile.sh
clang++ -stdlib=libstdc++ -c print.cxx
g++ test.cxx print.o
The example that I have added works, but is it possible to make it work with libraries that are compiled without the -stdlib=libstdc++ flag and instead use the libc++?
I downloaded Eclipse from Yoxos. This Eclispe includes: c, cpp, java etc..
However, when I opened a new cpp project with MinGW GCC Toolchains, and created a cpp file: hello.cpp, and wrote the following little program:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
when I run the file, it said - hello.exe has stopped working.
However, When I changed the program to:
(first line in comment)
//#include <iostream>
#include <stdio.h>
int main() {
printf("dsd");
return 0;
}
It worked well!
and when I removed the first line from comment like this:
#include <iostream>
#include <stdio.h>
int main() {
printf("dsd");
return 0;
}
the problem was back.. :(
Someone, Help..?
Thanks in advance! :)
Build Console Output:
20:41:10 **** Incremental Build of configuration Release for project hello ****
Info: Internal Builder is used for build
g++ -O3 -Wall -c -fmessage-length=0 -o helo.o "..\\helo.cpp"
g++ -o hello.exe helo.o
20:41:11 Build Finished (took 610ms)