digitalmars.D - How to create C function pointers
- Mildred (33/33) Sep 07 2006 Hi,
- Don Clugston (9/20) Sep 07 2006 extern (C) {
- Mildred (8/18) Sep 07 2006 Thanks for the answer,
-
Stewart Gordon
(20/38)
Sep 19 2006
Hi, In my application, I want to be able to extand it using regular C objects, loaded using for example dlfcn for POSIX systems (maybe another way on Windows). So, I need to have C functions pointers but I don't know how to declare them correctly : /////////// test.d /////////// module test; extern(C){ int c_function(int param){ return param; } } int main(){ // create a pointer to c_function extern (C) int function(int) ptr = &c_function; extern (C) int (*ptr2)(int) = &c_function; return 0; } ////////////////////////////// $ dmd test.d test.d(35): cannot implicitly convert expression (& c_function) of type int(C *)(int param) to int(*)(int) test.d(36): cannot implicitly convert expression (& c_function) of type int(C *)(int param) to int(*)(int) So, How do we do that , I did not find neither on the documentation nor while browsing the web for the solution. Thanks Mildred -- Mildred <xmpp:mildred jabber.fr> <http://mildred632.free.fr/> Clef GPG : <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key> Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Sep 07 2006
Mildred wrote:Hi, So, I need to have C functions pointers but I don't know how to declare them correctly :It's easiest to typedef them; the typedef picks up the extern(C).module test; extern(C){ int c_function(int param){ return param; } }extern (C) { typedef int function(int) CFuncPtr; } void main() { CFuncPtr ptr = &c_function; } BTW, this type of question really belongs in the 'D.learn' newgroup.
Sep 07 2006
extern (C) { typedef int function(int) CFuncPtr; } void main() { CFuncPtr ptr = &c_function; } BTW, this type of question really belongs in the 'D.learn' newgroup.Thanks for the answer, I didn't knew in which newsgroup I had to post, so I posted here, thanks for that clarification. I'll post there the next time. Mildred -- Mildred <xmpp:mildred jabber.fr> <http://mildred632.free.fr/> Clef GPG : <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key> Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Sep 07 2006
Don Clugston wrote:Mildred wrote:Or alias them.Hi, So, I need to have C functions pointers but I don't know how to declare them correctly :It's easiest to typedef them; the typedef picks up the extern(C).<snip> I see. So extern (C) int function(int) ptr = &c_function; merely gives the symbol ptr C linkage, and doesn't affect the linkage of the function involved? There ought to be a way of giving function pointer types linkage attributes inline. Maybe int extern (C) function(int) ptr = &c_function; or something...? Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.module test; extern(C){ int c_function(int param){ return param; } }extern (C) { typedef int function(int) CFuncPtr; }
Sep 19 2006