C++ on Linux

I have been learning the Old C++ standard in my
school. I knew of the new C++11 standard but didn't start learn it until now. But I can't get any of my programs to run. I use the g++ compiler on a Linux Ubuntu system. I tried various methods to for my programs to show the output including ''getch ()'' method using the conio.h file but it isn't included in the new standard. I also tried "system ("PAUSE"); " and the cin.get method. None work. The program compiles and produces an "a.out" file but it doesn't open. I have very less knowledge of the gnu compiler as we used an ancient Borland Turbo IDE in school. Can anyone help me with how to get the programs to run. Also my prime focus is Web development and I would like to know how C++ can be used in Web development if it can. Thank you.

Please show your code.
closed account (z0My6Up4)
Not all features of the new standard have been implemented.
> Can anyone help me with how to get the programs to run
http://linux.about.com/od/linux101/l/blnewbie3_1_3.htm
open a terminal
$ cd the/directory/that_contain/the_executable/
$ ./a.out
where `a.out' is the name of the program


Also http://askubuntu.com/questions/142092/how-do-i-run-an-executable-file-with-a-double-click
but I don't remember if it would spawn a terminal


> using the conio.h file but it isn't included in the new standard.
it was never included

> I also tried "system ("PAUSE");
PAUSE: command not found
The PAUSE command is a Windows command. Not so sure if it's on *nix.

Either way, we can't really help you unless we see your code.
The 'pause' and 'getch' are mere workarounds for that system, which closes the terminal of a console application on exit -- just to keep the terminal alive long enough to see the output.

There has never been a real need for them, because one can execute applications from aeternal terminal. This is not OS or compiler-specific.
are you building classes or is this a C procedural program? if it is a c++ class that creates an object then you need to put C guards around the C code. See below for the syntax of adding guards. this is because the compiler will try to mangle the symbols into C++. Sometimes the compiler is forgiving but often it is not. if you are building an object, I would recommend this code.

1
2
3
4
5
6
7
8
9
10
11
12
//// H file foo.h
#ifndef _FOO_H__
#define _FOO_H__

class foo 
{
    public:
    foo(){}  
    virtual ~foo(){}
   void bar() const;
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
////CPP file foo.cpp
#include <iostream>
// you can bring in the entire std namespace by calling using namespace std;
// However I prefer to be very specific to the facility I need. 
// that way I don't get more clutter in the program namespace than needed.

using std::cin;
using std::cout;
void foo::bar()
{
    // a place to wait for a char input. example is y/n as an answer to a prompt using cout.
    char answer=0;
    cin >> answer; ///< wait for console input from stdin
   
    //do something with response.
   cout << answer; ///< simple print answer to stdout

}


C guards in C++ code.
1
2
3
4
5
6
7
8
9
#ifdef __cplusplus
extern "C" {
#endif

/* C code */

#ifdef __cplusplus 
}
endif

It is recommended to use the C++ versions of many of the C libraries if using a c++ compiler. For instance <cstdio> instead of <stdio.h>.

As an after thought C++ 11 mostly adds new features, such as weak pointers and updates to other functions like using the braces {} for variable assignments.
Last edited on
Firstof all, get yourself a modern compiler. GCC 4.8's g++ is a good choice and so is Clang's clang++.

As for the code to keep the terminal open, you could just add a:

1
2
3
4
5
6
// your code here

cin.clear();
cin.get();

return 0;


When it comes to C++ Web Frameworks I think "Wt" is the most common and one that's well maintained.

As of the:

system ("PAUSE");

I would refrain from using any form of system() call, as it is slow and not considered good practice. It spawns a separate process (as in program) and therefor will be a possible point of memory leak in your code if something bad happens before the control is returned to your program.

For the Web Framework go to:

http://www.webtoolkit.eu/wt#/

Good luck and happy hacking!
Last edited on
Topic archived. No new replies allowed.