-1

I am trying to get the NSHomeDirectory() in pure C

#include <CoreFoundation/CFBundle.h>
#include <CoreFoundation/CoreFoundation.h>

void testFunc() {
    printf(NSHomeDirectory()); 
}
user16217248
  • 3,119
  • 19
  • 19
  • 37
Mohab
  • 15
  • 6
  • 1
    And what's your problem? It's not a good way of using `printf()` — you would be safer using `printf("%s\n", NSHomeDirectory())`, but it's unlikely to be your problem. Is the problem occurring when you compile the code, when you link the code, or when you run the code? You should probably mention that you're trying to work on macOS X, and presumably, you're using Xcode — but are you using the IDE or just the command-line tools? – Jonathan Leffler Apr 20 '23 at 02:07
  • `CFCopyHomeDirectoryURL()`? – Cy-4AH Apr 20 '23 at 14:44
  • Does this answer your question? [Create directory/file in iOS using C++](https://stackoverflow.com/questions/38945963/create-directory-file-in-ios-using-c) – Cy-4AH Apr 21 '23 at 07:35

1 Answers1

3

You need to send the UTF8String message to the NSString object returned by NSHomeDirectory() to obtain a C string. To do this in pure C (Tested):

#include <objc/runtime.h>
#include <objc/message.h>
void *NSHomeDirectory();
void testFunc() {
    puts(((const char *(*)(void *, SEL))objc_msgSend)(NSHomeDirectory(), sel_getUid("UTF8String")));
    // Also use puts() instead of printf() here
}

Note: Compile using -framework Cocoa

user16217248
  • 3,119
  • 19
  • 19
  • 37