www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - optlink and weak symbols

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
I am interfacing with some C code [python.dll], which has some structs 
declared like so:

PyTypeObject PyType_Type;

I wish to be able to link to PyType_Type like so:

extern(C) __gshared PyTypeObject PyType_Type;

in linux, I can do exactly that, but optlink is generating a new memory 
location for PyType_Type.

strings output suggests that my lib file contains the symbol PyType_Type.

Is this sort of thing supposed to work?
Oct 16 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-10-17 07:07, Ellery Newcomer wrote:
 I am interfacing with some C code [python.dll], which has some structs
 declared like so:

 PyTypeObject PyType_Type;

 I wish to be able to link to PyType_Type like so:

 extern(C) __gshared PyTypeObject PyType_Type;

 in linux, I can do exactly that, but optlink is generating a new memory
 location for PyType_Type.

 strings output suggests that my lib file contains the symbol PyType_Type.

 Is this sort of thing supposed to work?
You need to declare the variable as "extern" if it's defined in the C code: extern(C) extern __gshared PyTypeObject PyType_Type; http://dlang.org/interfaceToC.html#C%20Globals -- /Jacob Carlborg
Oct 16 2012
parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 10/16/2012 11:16 PM, Jacob Carlborg wrote:
 You need to declare the variable as "extern" if it's defined in the C code:

 extern(C) extern __gshared PyTypeObject PyType_Type;

 http://dlang.org/interfaceToC.html#C%20Globals
nice tip, but adding extern doesn't change link behavior at all.
Oct 17 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-10-18 05:12, Ellery Newcomer wrote:

 nice tip, but adding extern doesn't change link behavior at all.
Hmm, are you linking with the DLL (the import library) ? In not, you need to use dlopen, or what the corresponding Windows function is. -- /Jacob Carlborg
Oct 17 2012
next sibling parent "Regan Heath" <regan netmail.co.nz> writes:
On Thu, 18 Oct 2012 07:41:16 +0100, Jacob Carlborg <doob me.com> wrote:

 On 2012-10-18 05:12, Ellery Newcomer wrote:

 nice tip, but adding extern doesn't change link behavior at all.
Hmm, are you linking with the DLL (the import library) ? In not, you need to use dlopen, or what the corresponding Windows function is.
LoadLibrary[Ex] R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Oct 18 2012
prev sibling parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 10/17/2012 11:41 PM, Jacob Carlborg wrote:
 On 2012-10-18 05:12, Ellery Newcomer wrote:

 nice tip, but adding extern doesn't change link behavior at all.
Hmm, are you linking with the DLL (the import library) ? In not, you need to use dlopen, or what the corresponding Windows function is.
I am using python27_digitalmars.lib, which is generated from libs\python27.lib with coffimplib, and it is linking my executables with python.dll.
Oct 18 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-10-18 13:55, Ellery Newcomer wrote:

 I am using python27_digitalmars.lib, which is generated from
 libs\python27.lib with coffimplib, and it is linking my executables with
 python.dll.
Ok. Do you know how the corresponding C code would look like? Maybe you need to use the "export" attribute. It's the same as "__declspec(dllimport)", have a look: http://dlang.org/htomodule.html If that doesn't work then I'm out of suggestions, Windows is not my primary platform. -- /Jacob Carlborg
Oct 18 2012
parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 10/18/2012 11:36 AM, Jacob Carlborg wrote:
 On 2012-10-18 13:55, Ellery Newcomer wrote:

 I am using python27_digitalmars.lib, which is generated from
 libs\python27.lib with coffimplib, and it is linking my executables with
 python.dll.
Ok. Do you know how the corresponding C code would look like? Maybe you need to use the "export" attribute. It's the same as "__declspec(dllimport)", have a look: http://dlang.org/htomodule.html If that doesn't work then I'm out of suggestions, Windows is not my primary platform.
ha HA! extern(C) extern export PyTypeObject PyType_Type; seems to do the trick (I'm using a boiled down case just now)! High five to the [non-me] non-windows dev!
Oct 18 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-10-19 02:30, Ellery Newcomer wrote:

 extern(C) extern export PyTypeObject PyType_Type;

 seems to do the trick (I'm using a boiled down case just now)!

 High five to the [non-me] non-windows dev!
Hehe, high five :) -- /Jacob Carlborg
Oct 18 2012