This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Can someone please explain difference between g++ linking and using ld?


Hi again.

About a week ago I asked a question about whether exceptions worked from
within shared libraries on a Redhat 5.1 system. Unfortunately no one
answered
so I was going to ask again. Before doing so though instead of
explicitly using
the linker ld to produce the shared library I used g++ instead.
Amazingly or
not to those who might know why, exceptions work from the shared library
thus produced. So now my question isn't about whether shared libraries
and exceptions are compatible. Instead I was wondering if someone could
please take the time to explain how invoking the linker through g++
is different than invoking the linker directly.

For concreteness here's the code I used and the way I compiled/linked
it.

**** from a file called testexcept.h of course ******
#ifndef _TESTEXCEPT_H
#define _TESTEXCEPT_H

#include <exception>

class Object
{
public:
	Object() {}
	virtual ~Object() {}
	void testexcept() throw(exception);
};

#endif


***** and file testexcept.cc *******

void
Object::testexcept() throw(exception)
{
	throw exception();
}

**** teste.cc file *******

#include <iostream.h>
#include "testexcept.h"

int main(int argc,char** argv)
{
	Object o;

	try {
		o.testexcept();
	}
	catch(exception e) {
		cout << "exception occurred\n";
	}
}

So given the above code I ran these two tests.

1) Compile testexcept.cc using g++ -shared.
	g++ testexcept.cc -shared -o libtestexcept.so

2) Compile and link main file
	g++ teste.cc -L. -ltestexcept -o teste

3) After making sure LD_LIBRARY_PATH includes current directory
run teste
	./teste
produces the output you expect namely "exception occurred".

Next instead of doing 1 and 2 above I did.

1) Compile testexcept.cc using g++ -c,
	g++ testexcept.cc -c
2) Link into shared object using ld
	ld -shared -o libtestexcept.so testexcept.o
3) Recompile teste, shouldn't really have to and doesn't
make a difference, but I did anyway so...
	g++ teste.cc -L. -ltestexcept -o teste
4) run teste
	./teste
produces "Aborted(core dumpted).

So what's up? What's the difference between linking with g++
and ld? I really would like to know since I can't guarantee
that g++ will be on all the systems I want my code to run
on.

Anyway, please excuse the length of the message, I thought
it best to include an example so there aren't any misunderstandings.

Thanks in advance.
-- 
Gerald Gryschuk
gerald.gryschuk@home.com
MidNightOil Consulting - "We burn the midnight oil so you don't have
to."


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]