undefined reference to vtable
Shaun Jackman
sjackman@telus.net
Tue Aug 10 19:55:00 GMT 2004
I'm writing a C++ program that calls a Java shared library. I'm seeing
undefined references to the vtable for a class defined using C++.
$ make
g++-3.4 -g -O2 -o HelloWorld HelloWorld.o -lswt -lswt-pi
HelloWorld.o(.text+0x136): In function `main':
/home/sjackman/work/hello/swt-cni/HelloWorld.cc:62: undefined reference to `java::lang::Object::Object()'
HelloWorld.o(.text+0x13e):/home/sjackman/work/hello/swt-cni/HelloWorld.cc:62: undefined reference to `vtable for ShellSelectionListener'
collect2: ld returned 1 exit status
make: *** [HelloWorld] Error 1
I suspect this will be a rather obvious error on my part. In the mean
time, however, I'm stumped.
As an aside, I think it's positively amazing that it's so simple to
write an application in C++ that uses a library written in Java that
had no intention by the authors of ever being used in this manner.
Gcj is absolutely amazing work!
Thanks,
Shaun
[HelloWorld.cc]
#include <org/eclipse/swt/SWT.h>
#include <org/eclipse/swt/events/SelectionEvent.h>
#include <org/eclipse/swt/events/SelectionListener.h>
#include <org/eclipse/swt/layout/RowLayout.h>
#include <org/eclipse/swt/widgets/Button.h>
#include <org/eclipse/swt/widgets/Display.h>
#include <org/eclipse/swt/widgets/Label.h>
#include <org/eclipse/swt/widgets/Menu.h>
#include <org/eclipse/swt/widgets/Shell.h>
#include <gcj/cni.h>
using namespace org::eclipse::swt;
using namespace org::eclipse::swt::events;
using namespace org::eclipse::swt::layout;
using namespace org::eclipse::swt::widgets;
using namespace java::lang;
/** Main shell. */
Shell* shell;
/** Closes the shell. */
class ShellSelectionListener : public SelectionListener
{
public:
virtual void widgetSelected( SelectionEvent* event)
{
shell->close();
}
virtual void widgetDefaultSelected( SelectionEvent*) {}
static Class class$;
};
Class ShellSelectionListener::class$;
/** Displays "Hello, world!" using SWT. */
int
main()
{
JvCreateJavaVM( NULL);
JvAttachCurrentThread( NULL, NULL);
Display* display = new Display;
shell = new Shell( display);
shell->setText( JvNewStringUTF( "Hello"));
Menu* menu = new Menu( shell, SWT::BAR);
shell->setMenuBar( menu);
Label* label = new Label( shell, SWT::NONE);
label->setText( JvNewStringUTF( "Hello, world!"));
Button* button = new Button( shell, SWT::PUSH);
button->setText( JvNewStringUTF( "Ok"));
button->addSelectionListener( new ShellSelectionListener);
RowLayout* rowLayout = new RowLayout();
rowLayout->type = SWT::VERTICAL;
shell->setLayout( rowLayout);
shell->open();
while( !shell->isDisposed()) {
if( !display->readAndDispatch())
display->sleep();
}
display->dispose();
JvDetachCurrentThread();
return 0;
}
More information about the Java
mailing list