Axel
Axel

Reputation: 1754

Declaring and using a C function in Objective C

This must be very simple, but I can't figure out how to do this: I have a C-function to monitor current memory usage:

natural_t report_memory(void) {
    struct task_basic_info info;
    mach_msg_type_number_t size = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(),
                               TASK_BASIC_INFO,
                               (task_info_t)&info,
                               &size);
    if( kerr == KERN_SUCCESS ) {
        return info.resident_size;
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
        return 0;
    }
}

Now, I would like to use it. How do I declare it in the .h? I tried the (for me) obvious within the objective c methods:

natural_t report_memory(void);

Calling this somewhere in the code:

NSLog(@"Memory used: %u", rvC.report_memory());

The Compiler complains error: called object is not a function. Thus, I assume, the declaration is somehow wrong. I tried several options, but the best I could get was a runtime error... How to fix this?

Upvotes: 5

Views: 2453

Answers (2)

Martin Babacaev
Martin Babacaev

Reputation: 6280

If you want to use this function in other modules, you should also put in your header (.h) file this line

extern natural_t report_memory(void);

Upvotes: 2

user557219
user557219

Reputation:

rvC.report_memory()

should be replaced with

report_memory()

since it is a C function.

Upvotes: 10

Related Questions