Re: C functions

2013-10-19 Thread Maxthon Chan
libclang is more than that.

Xcode code highlighting and code indexing is based on lib clang, as well as 
delta compilation. (and I have a remote plan of cloning Xcode for GNUstep)

Also, there was a friend of mine that created a translator that converts code 
from a new language to C, and I modified it to plug libclang at its output and 
that made it a full compiler that emits object files (and links)

On Oct 19, 2013, at 23:43, Jean-Daniel Dupas  wrote:

> 
> Le 19 oct. 2013 à 15:01, Uli Kusterer  a écrit :
> 
>> On 19 Oct 2013, at 01:17, Shane Stanley  wrote:
>>> On 19 Oct 2013, at 3:15 AM, Uli Kusterer  
>>> wrote:
>>> 
 this is what you'd do if you wanted to make e.g. CoreFoundation APIs 
 accessible to a scripting language
>>> 
>>> That's along the lines of what I had in mind, although in this case for 
>>> basic things like the trig functions in Math.h.
>> 
>> So, is this supposed to just directly hand off some standard functions you 
>> decide to map into your library, like PHP exposes most of the standard C 
>> library? In that case I’d probably just write a little script that generates 
>> wrappers that translate between your language’s calling convention and the 
>> one of the C library, and enter them in a global variable with a struct that 
>> maps the plain-text name to the function pointer.
>> 
>> Whether you actually want to regex-parse the headers (like I did here for my 
>> Hammer programming language: 
>> https://github.com/uliwitness/Forge/blob/master/headerimport.php) or whether 
>> you want to just hand-write a short, easily parsed list of the functions and 
>> their argument types is up to you. The latter requires less maintenance if 
>> you plan to only selectively import a few native calls, as regex-parsing of 
>> C/ObjC headers is rather optimistic and tends to break every couple of 
>> system releases.
>> 
> 
> To parse C/Obj-C/C++ headers, you can also use libclang which is design to do 
> that. I used it to parse C header and generate boilerplate code some times 
> ago, and it worked quite well with very few code to write on my side.
> 
>> If you, on the other hand, want to support creation of actual headers by 
>> users of your language and adding of new functions w/o recompiling, you’re 
>> prolly best off with a foreign function interface like libffi.
>> 
>> Cheers,
>> -- Uli Kusterer
>> “The Witnesses of TeachText are everywhere...”
>> http://stacksmith.org
>> 
>> 
>> ___
>> 
>> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>> 
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>> 
>> Help/Unsubscribe/Update your Subscription:
>> https://lists.apple.com/mailman/options/cocoa-dev/devlists%40shadowlab.org
>> 
>> This email sent to devli...@shadowlab.org
> 
> -- Jean-Daniel
> 
> 
> 
> 
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/xcvista%40me.com
> 
> This email sent to xcvi...@me.com



signature.asc
Description: Message signed with OpenPGP using GPGMail
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-19 Thread Jean-Daniel Dupas

Le 19 oct. 2013 à 15:01, Uli Kusterer  a écrit :

> On 19 Oct 2013, at 01:17, Shane Stanley  wrote:
>> On 19 Oct 2013, at 3:15 AM, Uli Kusterer  
>> wrote:
>> 
>>> this is what you'd do if you wanted to make e.g. CoreFoundation APIs 
>>> accessible to a scripting language
>> 
>> That's along the lines of what I had in mind, although in this case for 
>> basic things like the trig functions in Math.h.
> 
> So, is this supposed to just directly hand off some standard functions you 
> decide to map into your library, like PHP exposes most of the standard C 
> library? In that case I’d probably just write a little script that generates 
> wrappers that translate between your language’s calling convention and the 
> one of the C library, and enter them in a global variable with a struct that 
> maps the plain-text name to the function pointer.
> 
> Whether you actually want to regex-parse the headers (like I did here for my 
> Hammer programming language: 
> https://github.com/uliwitness/Forge/blob/master/headerimport.php) or whether 
> you want to just hand-write a short, easily parsed list of the functions and 
> their argument types is up to you. The latter requires less maintenance if 
> you plan to only selectively import a few native calls, as regex-parsing of 
> C/ObjC headers is rather optimistic and tends to break every couple of system 
> releases.
> 

To parse C/Obj-C/C++ headers, you can also use libclang which is design to do 
that. I used it to parse C header and generate boilerplate code some times ago, 
and it worked quite well with very few code to write on my side.

> If you, on the other hand, want to support creation of actual headers by 
> users of your language and adding of new functions w/o recompiling, you’re 
> prolly best off with a foreign function interface like libffi.
> 
> Cheers,
> -- Uli Kusterer
> “The Witnesses of TeachText are everywhere...”
> http://stacksmith.org
> 
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/devlists%40shadowlab.org
> 
> This email sent to devli...@shadowlab.org

-- Jean-Daniel





___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-19 Thread Uli Kusterer
On 19 Oct 2013, at 01:17, Shane Stanley  wrote:
> On 19 Oct 2013, at 3:15 AM, Uli Kusterer  wrote:
> 
>> this is what you'd do if you wanted to make e.g. CoreFoundation APIs 
>> accessible to a scripting language
> 
> That's along the lines of what I had in mind, although in this case for basic 
> things like the trig functions in Math.h.

 So, is this supposed to just directly hand off some standard functions you 
decide to map into your library, like PHP exposes most of the standard C 
library? In that case I’d probably just write a little script that generates 
wrappers that translate between your language’s calling convention and the one 
of the C library, and enter them in a global variable with a struct that maps 
the plain-text name to the function pointer.

 Whether you actually want to regex-parse the headers (like I did here for my 
Hammer programming language: 
https://github.com/uliwitness/Forge/blob/master/headerimport.php) or whether 
you want to just hand-write a short, easily parsed list of the functions and 
their argument types is up to you. The latter requires less maintenance if you 
plan to only selectively import a few native calls, as regex-parsing of C/ObjC 
headers is rather optimistic and tends to break every couple of system releases.

 If you, on the other hand, want to support creation of actual headers by users 
of your language and adding of new functions w/o recompiling, you’re prolly 
best off with a foreign function interface like libffi.

Cheers,
-- Uli Kusterer
“The Witnesses of TeachText are everywhere...”
http://stacksmith.org


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-19 Thread Uli Kusterer
On 18 Oct 2013, at 20:38, Kyle Sluder  wrote:
> CFBundleGetFunctionPointerForName just calls dlsym.

 Sure. NSLog also eventually calls syslog. I still wouldn’t drop down to syslog 
for most Cocoa logging needs.

CFBundleGetFunctionPointerForName takes a CFStringRef and if you’re e.g. 
looking at a .framework or other bundled library, it’ll pick out the right 
executable for you.

Cheers,
-- Uli Kusterer
“The Witnesses of TeachText are everywhere...”
http://zathras.de


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-18 Thread Charles Srstka
On Oct 18, 2013, at 7:30 PM, ChanMaxthon  wrote:

> I think I know why it did not work: strip command can remove debug symbols, 
> or unused functions as well.

Yep.

Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-18 Thread ChanMaxthon
I think I know why it did not work: strip command can remove debug symbols, or 
unused functions as well.

Sent from my iPhone

> On 2013年10月19日, at 7:53, Charles Srstka  wrote:
> 
>> On Oct 18, 2013, at 6:42 PM, ChanMaxthon  wrote:
>> 
>> Try add this line:
>> 
>> extern void foo(void);
> 
> Already did; it doesn't work. See my follow-up post.
> 
> Charles
> 
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-18 Thread Charles Srstka
On Oct 18, 2013, at 6:42 PM, ChanMaxthon  wrote:

> Try add this line:
> 
> extern void foo(void);

Already did; it doesn't work. See my follow-up post.

Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-18 Thread ChanMaxthon
Try add this line:

extern void foo(void);

Sent from my iPhone

> On 2013年10月19日, at 1:21, Charles Srstka  wrote:
> 
>> On Oct 18, 2013, at 6:15 AM, Dmitry Markman  wrote:
>> 
>> I don't thinks strip remove info used by dynamic linker
>> Thus dlsym should work
>> As long as symbol is external (not with hidden visibility) dlsym is able to 
>> find the symbol
>> (stripped or not)
> 
> Not in my testing:
> 
> #import 
> #include 
> 
> void foo() {
> 
> }
> 
> int main(int argc, const char * argv[]) {
>@autoreleasepool {
>NSLog(@"The correct address is %p", &foo);
>NSLog(@"dlsym returns %p", dlsym(RTLD_SELF, "foo"));
>}
>return 0;
> }
> 
> In debug mode:
> 
> 2013-10-18 12:16:37.027 dlsymtest[95106:303] The correct address is 
> 0x10e40
> 2013-10-18 12:16:37.031 dlsymtest[95106:303] dlsym returns 0x10e40
> Program ended with exit code: 0
> 
> Doing an Archive in Xcode and running the resulting binary in the Terminal:
> 
> 2013-10-18 12:17:17.891 dlsymtest[95151:707] The correct address is 
> 0x10574fe6a
> 2013-10-18 12:17:17.902 dlsymtest[95151:707] dlsym returns 0x0
> 
> It won't work with the default build settings. Other commenters are correct, 
> however, that it can work if you move the symbols you want to look up into a 
> library.
> 
> Charles
> 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-18 Thread Shane Stanley
On 19 Oct 2013, at 3:15 AM, Uli Kusterer  wrote:

> this is what you'd do if you wanted to make e.g. CoreFoundation APIs 
> accessible to a scripting language

That's along the lines of what I had in mind, although in this case for basic 
things like the trig functions in Math.h.

-- 
Shane Stanley 
'AppleScriptObjC Explored' 


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-18 Thread Kyle Sluder
On Fri, Oct 18, 2013, at 10:39 AM, Uli Kusterer wrote:
> Oh, one more thing: Instead of dlsym(), you can also use
> CFBundleGetFunctionPointerForName() and its cohorts might also be useful
> for this if you want to go a bit more high-level. But even then, it needs
> to be an exported symbol in a dylib, can't be just any internal function.

CFBundleGetFunctionPointerForName just calls dlsym.

--Kyle Sluder
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-18 Thread Uli Kusterer
On Oct 18, 2013, at 6:15 PM, Uli Kusterer  wrote:
> On Oct 18, 2013, at 4:48 AM, Shane Stanley  wrote:
>> is there any way to build a call to a C function on the fly? I mean 
>> something like pass a string to a method, and have it call the function of 
>> that name?
> 
> 
> Short: No. Long: Maybe.
> 
> 1) You can put a function in a dynamic library and export it, and load that 
> library. If you know its signature, you can then call it.
> 
> 2) You can build a look-up table of name -> function pointer mappings inside 
> your application, then look up the function pointer by name and call it. 
> You'd need to know its signature then, too.
> 
> 3) You can build a dynamic library that has this function as its main entry 
> point and call that. Essentially a variant of #1. You'll still need to know 
> its signature beforehand then
> 
> 4) You can use a library like libffi to call a function pointer (using the 
> methods in #1 through #3 to get one from a name) even without knowing its 
> calling conventions beforehand. As long as you know the signature the moment 
> you're calling it and have the proper values, that'll work (this is what 
> you'd do if you wanted to make e.g. CoreFoundation APIs accessible to a 
> scripting language)
> 
> Cheers,
> -- Uli Kusterer
> "The Witnesses of TeachText are everywhere..."
> http://www.zathras.de


Oh, one more thing: Instead of dlsym(), you can also use 
CFBundleGetFunctionPointerForName() and its cohorts might also be useful for 
this if you want to go a bit more high-level. But even then, it needs to be an 
exported symbol in a dylib, can't be just any internal function.

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-18 Thread Charles Srstka
On Oct 18, 2013, at 6:15 AM, Dmitry Markman  wrote:

> I don't thinks strip remove info used by dynamic linker
> Thus dlsym should work
> As long as symbol is external (not with hidden visibility) dlsym is able to 
> find the symbol
> (stripped or not)

Trying to set the symbol to external doesn't seem to change things:

#import 
#include 

extern __attribute__((visibility("default"))) void foo() {

}

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"The correct address is %p", &foo);
NSLog(@"dlsym returns %p", dlsym(RTLD_SELF, "foo"));
}
return 0;
}

Debug mode:

2013-10-18 12:28:41.565 dlsymtest[95471:303] The correct address is 0x10e40
2013-10-18 12:28:41.576 dlsymtest[95471:303] dlsym returns 0x10e40

Archived:

2013-10-18 12:30:42.232 dlsymtest[95518:707] The correct address is 0x10cd74e6a
2013-10-18 12:30:42.236 dlsymtest[95518:707] dlsym returns 0x0

Looking at the Mach-O binary, _foo is listed as an external symbol in the 
binary built in Debug mode, but not for the archived binary. It may be that the 
strip program removes external symbols that it sees as unnecessary.

Charles


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-18 Thread Charles Srstka
On Oct 18, 2013, at 6:15 AM, Dmitry Markman  wrote:

> I don't thinks strip remove info used by dynamic linker
> Thus dlsym should work
> As long as symbol is external (not with hidden visibility) dlsym is able to 
> find the symbol
> (stripped or not)

Not in my testing:

#import 
#include 

void foo() {

}

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"The correct address is %p", &foo);
NSLog(@"dlsym returns %p", dlsym(RTLD_SELF, "foo"));
}
return 0;
}

In debug mode:

2013-10-18 12:16:37.027 dlsymtest[95106:303] The correct address is 0x10e40
2013-10-18 12:16:37.031 dlsymtest[95106:303] dlsym returns 0x10e40
Program ended with exit code: 0

Doing an Archive in Xcode and running the resulting binary in the Terminal:

2013-10-18 12:17:17.891 dlsymtest[95151:707] The correct address is 0x10574fe6a
2013-10-18 12:17:17.902 dlsymtest[95151:707] dlsym returns 0x0

It won't work with the default build settings. Other commenters are correct, 
however, that it can work if you move the symbols you want to look up into a 
library.

Charles


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-18 Thread Jeffrey Oleander
On 2013 Oct 18,, at 04:48, Shane Stanley  
wrote:
is there any way to build a call to a C function on the fly? I mean 
something like pass a string to a method, and have it call the 
function of that name?


This at least used to be shown in the "Objective-C 2.0 Runtime 
Programming Guide".

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-18 Thread Uli Kusterer
On Oct 18, 2013, at 4:48 AM, Shane Stanley  wrote:
> is there any way to build a call to a C function on the fly? I mean something 
> like pass a string to a method, and have it call the function of that name?


 Short: No. Long: Maybe.

 1) You can put a function in a dynamic library and export it, and load that 
library. If you know its signature, you can then call it.

 2) You can build a look-up table of name -> function pointer mappings inside 
your application, then look up the function pointer by name and call it. You'd 
need to know its signature then, too.

 3) You can build a dynamic library that has this function as its main entry 
point and call that. Essentially a variant of #1. You'll still need to know its 
signature beforehand then

 4) You can use a library like libffi to call a function pointer (using the 
methods in #1 through #3 to get one from a name) even without knowing its 
calling conventions beforehand. As long as you know the signature the moment 
you're calling it and have the proper values, that'll work (this is what you'd 
do if you wanted to make e.g. CoreFoundation APIs accessible to a scripting 
language)

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-18 Thread Jens Alfke

On Oct 17, 2013, at 8:49 PM, Charles Srstka  wrote:

> You shouldn't rely on dlsym() working in production code. If the binary is 
> stripped (as it is by default for release builds, I believe), it won't work.

You could work around that by exporting the symbol, e.g. by adding it to a 
“.exp” file.

And if the function comes from a library’s API, it’ll by definition be 
available.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-17 Thread ChanMaxthon
You can certainly move everything into a library, also you can prevent 
executables from being stripped in Xcode.

Sent from my iPhone

> On 2013年10月18日, at 11:54, Charles Srstka  wrote:
> 
> Loadable bundles and libraries don't get stripped. Executables, since they 
> don't need to be loaded by another process, usually are.
> 
> Charles
> 
>> On Oct 17, 2013, at 10:51 PM, ChanMaxthon  wrote:
>> 
>> Then, problem, how did Core Foundation bundle loading work?
>> 
>> Sent from my iPhone
>> 
 On 2013年10月18日, at 11:49, Charles Srstka  wrote:
 
 On Oct 17, 2013, at 10:40 PM, Maxthon Chan  wrote:
 
 You actually can, by using dlsym(3) to resolve the symbol, cast it to the 
 appropriate function pointer and call it.
 
 For example:
 
 int (*myfunc)(int, int) = dlsym(RTLD_DEFAULT, myfunc_name);
 if (myfunc)
  printf(“%d”, myfunc(2, 3));
 else
  fprintf(stderr, “error: cannot resolve symbol: %s”, myfunc_name);
 
 This works on all POSIX operating systems.
>>> 
>>> You shouldn't rely on dlsym() working in production code. If the binary is 
>>> stripped (as it is by default for release builds, I believe), it won't work.
>>> 
>>> Charles
> 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-17 Thread Charles Srstka
On Oct 17, 2013, at 10:40 PM, Maxthon Chan  wrote:

> You actually can, by using dlsym(3) to resolve the symbol, cast it to the 
> appropriate function pointer and call it.
> 
> For example:
> 
> int (*myfunc)(int, int) = dlsym(RTLD_DEFAULT, myfunc_name);
> if (myfunc)
> printf(“%d”, myfunc(2, 3));
> else
> fprintf(stderr, “error: cannot resolve symbol: %s”, myfunc_name);
> 
> This works on all POSIX operating systems.

You shouldn't rely on dlsym() working in production code. If the binary is 
stripped (as it is by default for release builds, I believe), it won't work.

Charles


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-17 Thread Maxthon Chan
You actually can, by using dlsym(3) to resolve the symbol, cast it to the 
appropriate function pointer and call it.

For example:

int (*myfunc)(int, int) = dlsym(RTLD_DEFAULT, myfunc_name);
if (myfunc)
printf(“%d”, myfunc(2, 3));
else
fprintf(stderr, “error: cannot resolve symbol: %s”, myfunc_name);

This works on all POSIX operating systems.

On Oct 18, 2013, at 11:34, Charles Srstka  wrote:

> On Oct 17, 2013, at 9:48 PM, Shane Stanley  wrote:
> 
>> is there any way to build a call to a C function on the fly? I mean 
>> something like pass a string to a method, and have it call the function of 
>> that name?
> 
> No. That's an Objective-C feature that's not present in standard C.
> 
> Charles
> 
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/xcvista%40me.com
> 
> This email sent to xcvi...@me.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions

2013-10-17 Thread Charles Srstka
On Oct 17, 2013, at 9:48 PM, Shane Stanley  wrote:

> is there any way to build a call to a C function on the fly? I mean something 
> like pass a string to a method, and have it call the function of that name?

No. That's an Objective-C feature that's not present in standard C.

Charles


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: C functions and instance variables

2008-03-28 Thread Hamish Allan
On Fri, Mar 28, 2008 at 9:14 AM, Trygve Inda <[EMAIL PROTECTED]> wrote:

> I have a Cocoa object with .m and .h files and need to include in this a
>  series of about 30 C functions which all all in their own .c file. Is it
>  possible to give functions in the .c file access to the .m files instance
>  variables?

No, but you can rename the file.c as file.m, pass a pointer to self
from your Obj-C method to your C function, and use the ivars' Obj-C
getters/setters within your C function.

>  How can I do this without rewriting the .c functions as Obj-C?

You don't need to rewrite anything, as you can mix C and Obj-C freely
in a .m file.

>  Also, is there a way to use two .m files for one object so that all my code
>  doesn't have to be in one file? Can I just #import one .m file into the
>  other?

You can use categories:

http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_4_section_1.html

Hamish
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]