3

I've written a custom struct in a separate header file. It looks something like this

typedef struct RequestSpecifics {
    BOOL includeMetaData;
    BOOL includeVerboseData;
} RequestSpecifics;

Now I want to make a custom 'make' method, similar to the CoreLocation struct CLLocationCoordinate2 CLLocationCoordinate2DMake method.

I've tried two different ways. While both ways give no errors in the .h file, I do get errors when I want to use the make method.

Method 1:

extern RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData);

Throws:

Apple Mach-O Linker

"_RequestSpecificsMake", referenced from:

Error Linker command failed with exit code 1 (use -v to see invocation)

Method 2:

extern RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData) {
    RequestSpecifics specifics;
    specifics.includeMetaData = includeMetaData;
    specifics.includeVerboseData = includeVerboseData;
    return specifics;
}

Throws:

Apple Mach-O Linker

Error Linker command failed with exit code 1 (use -v to see invocation)

Usage example:

RequestSpecificsMake(NO, NO)

I've checked all common solutions for the Apple Macho-Linker error but nothing seems to work or the solutions are not relevant.

So how do I correctly implement the 'make' method for a struct?

Community
  • 1
  • 1
SolveSoul
  • 2,448
  • 4
  • 25
  • 34

2 Answers2

2

So apparently method 2 should be the implementation and it should not be in the .h file. Naturally, I need a .m file as well. This should be the correct way to do it:

.h file

RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData);

.m file

RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData) {
        RequestSpecifics specifics;
        specifics.includeMetaData = includeMetaData;
        specifics.includeVerboseData = includeVerboseData;
        return specifics;
    }

In the end I had to combine both methods! Also, by the looks of it, the extern keyword is not required.

SolveSoul
  • 2,448
  • 4
  • 25
  • 34
1

Why dont you try

static inline instead of extern

static inline RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData) {
    RequestSpecifics specifics;
    specifics.includeMetaData = includeMetaData;
    specifics.includeVerboseData = includeVerboseData;
    return specifics;
}

or if you want to use extern then you need to write it in .m file.

Krishna Kumar
  • 1,652
  • 9
  • 17