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
Answer from Dirk is no longer here on Stack OverflowStylistically, 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
Need help with C on Ubuntu
How to run a " C " program in Linux with Visual Studio Code.
Videos
gcc creates an executable file called a.out by default. Run that instead.
Alternatively, create a sensible named executable:
gcc -o hello hello.c
And even simpler, using the default rules built into make:
make hello
You need to install the corresponding package with the headers (normally libc6-dev) with
sudo apt-get install libc6-dev
And for sure install build-essential with
sudo apt-get install build-essential
Then compile your hello.c with the command:
gcc hello.c -o hello
and run it:
./hello
Hi, I'm a beginner to C. I see a lot of people from video tutorials using Visual Studio on Windows or other IDEs on Mac. And there they are doing some linking to sdl2 for example. I don't really know what the linking is. Could someone explain to me what that is? Do I need a special IDE for specific things for C on Ubuntu? Or can I do all those things by myself? (If so, how?) I would really appreciate it