Building C++ exception-using applications

I’m missing something about building applications which use C++
exceptions. Consider the following program:

#include
#include

int
main()
{
using namespace std;

try
{
throw std::runtime_error(“Some error”);
}
catch (const exception& e)
{
cout << “Caught std::exception” << endl;
}

return 0;
}

If I compile this with a single command, it compiles and runs as one
might expect.

$ cc -o test1 ExceptionTest.cpp
$ ./test1
Caught std::exception

However, if I build this in two steps (first compile, then link), the
resulting application dies badly.

$ cc -o ExceptionTest.o -c ExceptionTest.cpp
$ cc -o test2 -lm -lstdc++ ExceptionTest.o
$ ./test2
Abort (core dumped)

Can someone suggest a way to modify the two step method so that it
creates a correct executable? I’d appreciate any suggestions or pointers
to relevant documentation. I’ve been unable to find salient references
myself.

Thanks in advance,
Eric

The problem is that when you compile in separate phases, there is no
indication to qcc that you object files are C++ objects, and so it
doesn’t link in the runtime exception handling libraries.

You can indicate that you are linking C++ with the -lang-c++ option
to qcc, or by invoking qcc as QCC (or CC)

In qdn.public.qnxrtp.newuser Eric Berdahl <berdahl@intelligentparadigm.com> wrote:

I’m missing something about building applications which use C++
exceptions. Consider the following program:

#include <iostream
#include <stdexcept

int
main()
{
using namespace std;

try
{
throw std::runtime_error(“Some error”);
}
catch (const exception& e)
{
cout << “Caught std::exception” << endl;
}

return 0;
}

If I compile this with a single command, it compiles and runs as one
might expect.

$ cc -o test1 ExceptionTest.cpp
$ ./test1
Caught std::exception

However, if I build this in two steps (first compile, then link), the
resulting application dies badly.

$ cc -o ExceptionTest.o -c ExceptionTest.cpp
$ cc -o test2 -lm -lstdc++ ExceptionTest.o
$ ./test2
Abort (core dumped)

Can someone suggest a way to modify the two step method so that it
creates a correct executable? I’d appreciate any suggestions or pointers
to relevant documentation. I’ve been unable to find salient references
myself.

Thanks in advance,
Eric


cburgess@qnx.com