Why am I getting a segmentation error?

I am trying to compile a simple hello world function in C ++. After compiling it, I run it and get a “Segmentation Error”. Can someone shed some light on this?

I compile this from the Linux command line using the following command:

g ++ hello.cpp

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}
+5
source share
4 answers

The program itself looks fine. I would suggest that there is some kind of quirk in your compilation environment that causes segfault.

It is best to run this in the debugger (gdb) - this will tell you where it is crashing, which will help you figure out what the problem is.

To do this, do the following compilations:

g++ -g -o hello hello.cpp

then run gdb:

gdb hello

and in the gdb type prompt

run

. ,

bt

, , , , .

+6

, , .

+1

Compile it as follows

g ++ -Bstatic -static hello.cpp

and then run. /a.out

If this is not caused by an error, LD_LIBRARY_PATH is your culprit.

+1
source

It may be longshot, but try changing int main()toint main(int argc, char *argv[])

0
source

All Articles