What to use for iOS compiler define - ios

I have the following code that I want to run on Iphone OS, what compiler flag should I use for IOS and where in the below code should it be inserted? Thanks
#if defined(__BORLANDC__) || defined (__WATCOMC__) || defined(_MSC_VER) || defined(__ZTC__) || defined(__HIGHC__) || defined(_TURBOC_)
typedef long int Word32 ;
typedef short int Word16 ;
typedef short int Flag ;
#elif defined( __sun)
typedef short Word16;
typedef long Word32;
typedef int Flag;
#elif defined(__unix__) || defined(__unix)
typedef short Word16;
typedef int Word32;
typedef int Flag;
#elif defined(VMS) || defined(__VMS)
typedef short Word16;
typedef long Word32;
typedef int Flag;
#else
#error COMPILER NOT TESTED typedef.h needs to be updated, see readme
#endif

For iOS/tvOS/watchOS, you can use:
#if (TARGET_OS_IPHONE)
For iOS, you can use:
#if (TARGET_OS_IOS)
and for MAC OS X:
#if (TARGET_OS_OSX)
Hope this helps.

Related

Error : Expected identifier or '(' after usage of old Objective C library in Swift project through bridging header

I have quite low experience with Objective C as I started learning iOS development using Swift and I need to use a older ObjC library.
My netsdk.h library (file has over 50k lines)
#ifndef DHNETSDK_H
#define DHNETSDK_H
#if (defined(WIN32) || defined(_WIN32) || defined(_WIN64))
...
#else //non-windows
#define CLIENT_NET_API extern "C"
#define CALL_METHOD
#define CALLBACK
...
Basically after reading some problems related to this one I think there is problem with extern "C" on the next line
...
#define CLIENT_NET_API extern "C"
...
Due to this I am getting errors on many lines that use CLIENT_NET_API like
expected identifier or '('
Expanded from macro 'CLIENT_NET_API'
My first try was to wrap #define CLIENT_NET_API extern "C"
into
#ifdef __cplusplus
extern "C" {
#endif
#define CLIENT_NET_API
#ifdef __cplusplus
}
#endif
but then I am getting error "C does not support default arguments" on all lines using CLIENT_NET_API
for example these
CLIENT_NET_API BOOL CALL_METHOD CLIENT_StartBackUpCase(LLONG lLoginID, const NET_IN_START_CASE_BACK_UP* pstInParam, NET_OUT_START_CASE_BACK_UP *pstOutParam, int nWaitTime = NET_INTERFACE_DEFAULT_TIMEOUT);
CLIENT_NET_API BOOL CALL_METHOD CLIENT_StopBackUpCase(LLONG lLoginID, const NET_IN_STOP_CASE_BACK_UP* pstInParam, NET_OUT_STOP_CASE_BACK_UP *pstOutParam, int nWaitTime = NET_INTERFACE_DEFAULT_TIMEOUT);
I think it should be wrapped in some other way but I couldn't find a example with #define and extern keywords on the same line
In this case:
define CLIENT_NET_API extern "C"
Says, let CLIENT_NET_API be extern "C"
So, in your case you just define as empty existed preprocessor variable
#ifdef __cplusplus
extern "C" {
#endif
#define CLIENT_NET_API
#ifdef __cplusplus
}
#endif

JNA to Go DLL - How do I get String returned from Go Func?

I have a Java program that is using JNA to call a Go Func. Here's the Interface to the Go func in Java:
public interface GPG extends Library {
// GoString class maps to: C type struct { const char *p; GoInt n; }
public class GoString extends Structure {
public static class ByValue extends GoString implements Structure.ByValue {}
public String p;
public long n;
protected List getFieldOrder(){
return Arrays.asList(new String[]{"p","n"});
}
}
// Foreign functions
public GoString.ByValue decrypt(GoString.ByValue encString, GoString.ByValue secretKeyring, GoString.ByValue passphrase);
}
The func signature in Go is:
func decrypt(encString string, secretKeyring string, passphrase string) string
The Go generated C header has:
/* Created by "go tool cgo" - DO NOT EDIT. */
/* package command-line-arguments */
#line 1 "cgo-builtin-prolog"
#include <stddef.h> /* for ptrdiff_t below */
#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif
/* Start of preamble from import "C" comments. */
/* End of preamble from import "C" comments. */
/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"
#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
typedef _GoString_ GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
#endif
/* End of boilerplate cgo prologue. */
#ifdef __cplusplus
extern "C" {
#endif
extern GoString decrypt(GoString p0, GoString p1, GoString p2);
#ifdef __cplusplus
}
#endif
I call the Go Func from Java using this code:
GPG gpg = (GPG) Native.loadLibrary("C:/lib/gpg.dll", GPG.class);
GPG.GoString.ByValue encString = new GPG.GoString.ByValue();
encString.p = value;
encString.n = encString.p.length();
GPG.GoString.ByValue secretKeyring = new GPG.GoString.ByValue();
secretKeyring.p = "c:/gnupg/secring.gpg";
secretKeyring.n = secretKeyring.p.length();
GPG.GoString.ByValue passphrase = new GPG.GoString.ByValue();
passphrase.p = "SecretPassPhrase";
passphrase.n = passphrase.p.length();
GPG.GoString.ByValue decValue = gpg.decrypt(encString, secretKeyring, passphrase);
Clearly the func is being called and processes up to the return of the result string. But it then produces: "panic: runtime error: cgo result has Go pointer"
How do I get a String result back from Go?
Using go version go1.10 windows/amd64, JNA 4.5.1, Java 1.8.0_152
Your GO function should looks like this:
//export decrypt
func decrypt(encString string, secretKeyring string, passphrase string) *C.char {
//... your code here
var str string = "returning string"
return C.CString(str)
}
Java Interface:
public String decrypt(GoString.ByValue encString, GoString.ByValue secretKeyring, GoString.ByValue passphrase);
Your const char * in _GoString_ should use a Pointer instead, then use Pointer.getString() with the provided offset to obtain the actual string.
If Go itself is rejecting a string return value, you'll likely have to instead populate a buffer provided by the caller.

Conflicting types for 'SecRandomCopyBytes'

I get an error on xCode when I'm trying to archive a new version of my app and I can't figurate out where it come from and how can I fix it.
Look at the printscreen from xcode: Screenshot
iOS 11 changed the signature of that method.
You can try this
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
extern int SecRandomCopyBytes(SecRandomRef rnd, size_t count, void *bytes) __attribute__((weak_import));
#else
extern int SecRandomCopyBytes(SecRandomRef rnd, size_t count, uint8_t *bytes) __attribute__((weak_import));
#endif
Source : https://github.com/RNCryptor/RNCryptor/issues/248
This worked for me on macOS
replacing with
extern int SecRandomCopyBytes(SecRandomRef rnd, size_t count, void * bytes) __attribute__((weak_import));

Xcode extern error

I have problem with Xcode, I am getting following error when running the code:
Error : Expected identifier or ')'
Here is th code:
#import "IOKitLib.h"
#import <dlfcn.h>
#import <pthread.h>
#import <mach/mach.h>
#import <sys/ptrace.h>
#import <libkern/OSAtomic.h>
static mach_port_t masterPort = 0;
extern "C" kern_return_t io_service_open_extended
(
mach_port_t service,
task_t owningTask,
uint32_t connect_type,
NDR_record_t ndr,
io_buf_ptr_t properties,
mach_msg_type_number_t propertiesCnt,
kern_return_t *result,
mach_port_t *connection
);
Please help me to solve this problem and let me know where am I doing wrong. Thanks !
extern "C" is valid in C++ only. You can make the declaration usable in any language (C++, C or Objective-C), from a header file for example, using the __cplusplus guard macro:
#ifdef __cplusplus
extern "C" {
#endif
kern_return_t io_service_open_extended
(
mach_port_t service,
task_t owningTask,
uint32_t connect_type,
NDR_record_t ndr,
io_buf_ptr_t properties,
mach_msg_type_number_t propertiesCnt,
kern_return_t *result,
mach_port_t *connection
);
#ifdef __cplusplus
}
#endif

Define IOS Logs From C

How do I define Apple's NSLOG prints from C code ?
For Android it would be
#if _ANDROID__
# include <android/log.h>
# define LOGFUNC(level, fmt, args) __android_log_vprint(level, "andorid", fmt, args)
but How to to do it with Apple and NSLOG ?
I know I can do something like
#elif __APPLE__
# define LOGFUNC(level, fmt, args) vprintf(fmt, args)
but I dont see the logs in the device Logs.
You can provide a C-stub implemented as Objective-C and compile that for OSX/iOS only, providing equivalents for Android, Windows, etc. That's what I normally do when writing cross-platform code.
Log.h:
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
extern void logMsg(const char *fmt, ...);
#ifdef __cplusplus
}
#endif
AppleLog.m:
#import <Foundation/Foundation.h>
#import "Log.h"
void logMsg(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
NSString *message = [[NSString alloc] initWithFormat:#(fmt) arguments:va];
va_end(va);
NSLog(#"%#", message);
}
You can also provide platform-specific code for getHomeDirectory(), getTempDirectory(), etc. in much the same way.

Resources