extern(C++) linker errors

bitwise via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 22 08:17:34 PDT 2015


On Wed, 22 Apr 2015 02:14:40 -0400, Dan Olson <zans.is.for.cans at yahoo.com>  
wrote:

> bitwise <bitwise.pvt at gmail.com> writes:
>> 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?
>
> Hi, I think both examples need libstdc++ added when you link
> (-L-lstdc++).  That should resolve the missing C++ operators.
>
> For script1.sh with ldc2 I see an extra underscore in C++ mangled names.
> You must be on OS X.  ldc was just fixed in merge-2.067 branch to remove
> an extra underscore on OS X.  If you want to experiment, you can build
> https://github.com/ldc-developers/ldc/tree/merge-2.067 and check it out.
> There are still a few tests to reolve, but it works pretty well for me.
> --
> Dan

Awesome, Thanks!

I am now getting the expected error from clang/LDC, and the GCC/DMD build  
is working. I'll probably switch to LDC at some point, but I just need to  
get up and running right now.

My updated(objective-C++) and working code:

///////////////// test.mm
#include <stdio.h>
#import <Foundation/Foundation.h>

@interface XYZPerson : NSObject
- (void)sayHello;
@end

@implementation XYZPerson
- (void)sayHello {
     printf("Hello, World!\n");
}
@end

class Test {
public:
	virtual void Foo(){
		XYZPerson *person = [[XYZPerson alloc] init];
		[person sayHello];
		[person release];
	}
};

Test* CreateTest() {
	return new Test;
}

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

/////////////////  Test.d
same

/////////////////  main.d
same

///////////////// script2.sh
g++ test.mm -olibTest.o -c -stdlib=libc++
ar rcs libTest.a libTest.o
dmd main.d Test.d libTest.a -ofTest -L/usr/lib/libc++.dylib -L-framework  
-LFoundation
./Test


More information about the Digitalmars-d-learn mailing list