cgo on macOS - unknown type name NSString

Viewed 17

In a go code i have following code:

/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -L./ ${SRCDIR}/test.dylib -framework Foundation
#include "./test.h"
#include <stdlib.h>
#include <stdio.h>
#import <Foundation/Foundation.h>
#import <Foundation/NSString.h>
*/
import "C"

test.h has a struct with NSString

On building the go code it shows error: unknown type name 'NSString' NSString nspath,

can someone guide what's wrong here since i have included appropriate objective C libraries and headers.

1 Answers

Not running OSX/iOS, but looking at github, it looks like at least some developers wrap objective-c code in the cgo preamble inside a typical C function.

This looks like a particularly good example:

/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation
#import <Foundation/NSBundle.h>
#import <Foundation/NSString.h>
#include <stdlib.h>
#include <string.h>
const char *assets_zip_path() {
    NSString *path = [[NSBundle mainBundle] pathForResource: @"aaaaxy" ofType: @"dat"];
    const char *data = [path UTF8String];
    if (data == NULL) {
        return NULL;
    }
    return strdup(data);
}
*/
import "C"

func openAssetsZip() (*os.File, error) {
    pathCStr := C.assets_zip_path()
...

You also may already find the bindings you need in the go/mobile package.

Related