C++ on ubuntu hello world - c++

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

Related

How to correctly link C++ objects using GLFW functions in Linux?

I've created a very simple program containing multiple files and I'm now trying to link the GLFW library to it. I've only added GLFW functions to the file main.cpp and only include the library there, and only compiling and executing this file using the command g++ main.cpp -lglfw goes fine.
Before I added this library, compiling and linking the entire program also went fine, but even though there are no GLFW functions used in the other files when I want to link everything together (g++ -Wall -g -std=c++11 -lglfw main.o hello_world.o console.o) I suddenly get the error 'undefined reference to' every GLFW function I used. (I got no error while compiling main.cpp: g++ -Wall -g -std=c++11 -lglfw -c main.cpp)
This is the file main.cpp:
#include "basis.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
// Open-GL
#include <GLFW/glfw3.h>
/// Setup:
/// sudo apt-get update
/// sudo apt-get install libglfw3
/// sudo apt-get install libglfw3-dev
/// Compile:
/// g++ main.cpp -lglfw
using namespace std;
void errorCallback(int error, const char* description) {
fprintf(stderr, "Error: %s\n", description);
}
void test() {
GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
if (!window) {
cout << "Window creation failed!" << endl;
}
runConsole();
}
int main(int argc, char* argv[]) {
glfwSetErrorCallback(errorCallback);
if (!glfwInit()) {
cout << "GLFW initialization failed!" << endl;
exit(EXIT_FAILURE);
}
test();
glfwTerminate();
return 0;
}
Furthermore, I do not know where the library is located on my machine. Also, the Linux machine is Virtual Box, and the source code is in a shared folder located on my host windows system, but this his never created problems with libraries before as I compile them on Linux.
Oops, while re-reading the question I thought of a new solution, and it worked :) The correct linker command is: g++ -Wall -g -std=c++11 main.o hello_world.o console.o -lglfw

extended initializer lists being seen as functions

I'm trying to compile an opensource project but I'm getting this problem from g++
error: function definition does not declare parameters
the code is something like this
#include <iostream>
namespace hi {
class hello {
public:
bool first { true };
};
}
int main(int argc, char *argv[])
{
hi::hello h
std::cout << "output: " << h.first << std::endl;
return 0;
}
which produces the same compilation problem as the code of the opensource project when compiled with
g++ -O2 bools.cpp -o bools -std=c++0x
however if I try to compile this code with the same options it compiles and runs as it should
#include <iostream>
int main(int argc, char *argv[])
{
bool value { true };
std::cout << "output: " << value << std::endl;
return 0;
}
I'm using g++ 4.6.3 on Ubuntu 64bit.
thanks for your time.
Support for non-static data member initializers was added in gcc 4.7. You can see the list of what C++0x/C++11 features gcc supports with what versions here. The line for this feature says:
Non-static data member initializers | N2756 | GCC 4.7
This works perfectly fine in gcc 4.7 and greater, see it live here.

cout doesn't display anything in terminal

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.

Segmentation Fault with Global Variable in MPICH 1.6

Consider the following simple program:
#include <mpi.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <vector>
using std::cout;
using std::string;
using std::vector;
vector<float> test;
#ifdef GLOBAL
string hostname;
#endif
int main(int argc, char** argv) {
int rank; // The node id of this processor.
int size; // The total number of nodes.
#ifndef GLOBAL
string hostname;
#endif
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
cout << "Joining the job as processor: " << rank << std::endl;
{
char buf[2048] = "HELLO";
hostname.assign(buf, 2048);
}
test.push_back(1.0f);
cout << "Hostname: " << hostname << "::" << test[0] << std::endl;
MPI_Finalize();
return 0;
}
If I compile/run this with:
mpicxx -c test.cc && mpicxx -lstdc++ test.o -o test && ./test
there is no segmentation fault, but if I run it with:
mpicxx -DGLOBAL -c test.cc && mpicxx -lstdc++ test.o -o test && ./test
then there is a segmentation fault at the hostname.assign() line. In addition, if I remove this assignment, there is a segmentation fault in the string destructor once the main method returns so the assign method isn't the actual culprit.
Notice that the only difference is where the "global" variable hostname gets declared.
I am compiling with MPICH2 version 1.6, and don't really have the option to change this since I am running this on a supercomputer.
If I remove MPI_Init, etc. the error goes away leading me to believe that there is something unexpected happening with MPI and this global variable.
I found some other examples of this happening to people online, but they all resolved their issues by installing a new version of MPICH, which again is not a possibility for me.
Moreover, I want to know WHY this happening more than just a way around it.
Thanks for your time.
Ok, after quite a bit of debugging I have found that the MVAPICH2-1.6 library defines a variable called hostname in:
mpid/ch3/channels/mrail/src/rdma/ch3_shmem_coll.c
Here is the line (55 in this version of the file):
char hostname[SHMEM_COLL_HOSTNAME_LEN];
The compiler didn't complain about the name clash here, but this is almost certainly the culprit since changing the variable name in my program removed the error. I imagine this is changed in later versions of MVAPICH2, but I will file the bug if not.

BOOST_THROW_EXCEPTION causing Abort Trap

I'm trying to use boost::exception and have condensed my prototype code down to the example in the Boost exception tutorial however when running the code with the BOOST_THROW_EXCEPTION macro I get an abort from the program.
#include <iostream>
#include <boost/exception/all.hpp>
typedef boost::error_info<struct tag_my_info,int> my_info;
struct my_error: virtual boost::exception, virtual std::exception { };
void f() {
BOOST_THROW_EXCEPTION(my_error() << my_info(42));
// Uncomment the below (and comment the above) for the program to work
//throw my_error() << my_info(42);
}
int main(int argc, char** argv) {
try {
f();
}
catch(my_error& x) {
if(int const* mi = boost::get_error_info<my_info>(x)) {
std::cout << "My info: " << *mi << std::endl;
}
}
return 0;
}
Running the code with the BOOST_THROW_EXCEPTION macro:
$ ./a.out
Abort trap
If as the comment says, I swap the code, all is well
$ ./a.out
My info: 42
Below is the output from the g++ preprocessor for f()
void f() {
::boost::exception_detail::throw_exception_(my_error() << my_info(42),__PRETTY_FUNCTION__,"main.cpp",14);
}
Software versions are:
$ g++ -v
Using built-in specs.
Target: x86_64-apple-darwin10
Thread model: posix
gcc version 4.4.6 (GCC)
$ port list boost
boost #1.47.0 devel/boost
I'm on OSX SL using the tools provided by MacPorts. I've double checked the g++ search paths and there's only one copy of the boost hpp files and that's the ones that belong to the aforementioned boost package.
I have no idea why the abort trap is being called. I admit I'm newish to C++ .
The problem was caused by using the MacPorts version of g++. There are plenty of tickets related to exceptions and Abort Traps in the MP system (and plenty of examples on Google).
Using the version of g++ that comes with XCode enabled this problem to go away.