extern(C++) linker errors

bitwise via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 21 20:57:24 PDT 2015


Hello!

I am trying to interface to C++, and getting linker errors. Below are my 3  
source files and 2 build scripts with their associated errors. Can anyone  
see what I'm doing wrong?


/////////////////  test.cpp
#include <stdio.h>

class Test {
public:
	virtual void Foo(){
		printf("Foobar\n");
	}
};

Test* CreateTest() {
	return new Test;
}

void DestroyTest(Test *test) {
	delete test;
}

/////////////////  Test.d
module TestMod;

extern(C++) {
	interface Test {
		void Foo();
	}

	Test CreateTest();
	void DestroyTest(Test test);
}

/////////////////  main.d
module main;
import TestMod;

void main(string[] args)
{
	Test test = CreateTest();
	test.Foo();
	DestroyTest(test);
	test = null;
}

/////////////////  script1.sh
clang++ test.cpp -olibTest.o -c -emit-llvm
ar rcs libTest.a libTest.o
ldc2 main.d Test.d libTest.a -ofTest

Error:
Undefined symbols for architecture x86_64:
   "___Z10CreateTestv", referenced from:
       __Dmain in main.o
   "___Z11DestroyTestP4Test", referenced from:
       __Dmain in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see  
invocation)
Error: /usr/bin/gcc failed with status: 1


/////////////////  script2.sh
g++ test.cpp -olibTest.o -c
ar rcs libTest.a libTest.o
dmd main.d Test.d libTest.a -ofTest

Error:
Undefined symbols for architecture x86_64:
   "vtable for __cxxabiv1::__class_type_info", referenced from:
       typeinfo for Test in libTest.a(libTest.o)
   NOTE: a missing vtable usually means the first non-inline virtual member  
function has no definition.
   "operator delete(void*)", referenced from:
       DestroyTest(Test*) in libTest.a(libTest.o)
   "operator new(unsigned long)", referenced from:
       CreateTest() in libTest.a(libTest.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see  
invocation)
--- errorlevel 1

 


More information about the Digitalmars-d-learn mailing list