1

I'm trying to use a separate C header and implementation file in Xcode iOS/Objective-C project.
I want to use the method I implemented in main.m but I get these errors:

enter image description here Full size here

I've included user.h in main.m

Note that Target Membership is selected in user.c for HelloWorld. When I deselect this the errors are gone. But when I try to run the app, I get these errors at compile time: enter image description here
Full size here

When I implement the struct and method in main.m it compiles and runs just fine. But I don't get it why I can't use this particular code in a separate file?

Source Code:
user.h

#ifndef HelloWorld_user_h
#define HelloWorld_user_h

typedef struct {
    char *name;
    int age;
    char sex;
} User; //sizeof(User) = 16 bytes

void CreateAndDisplay(User *usr, char *name, int age, char sex);

#endif

user.c

#include <stdio.h>
#include <stdlib.h>

void CreateAndDisplay(User *usr, char *name, int age, char sex) {
    usr->name = name;
    usr->age = age;
    usr->sex = sex;

    printf("User address -> value:\n");
    printf("Name:\t%u\t->\t%s\n", (uint)usr, *&usr->name);
    printf("Age:\t%u\t->\t%i\n", (uint)&usr->age, *&usr->age);
    printf("Sex:\t%u\t->\t%c\n\n", (uint)&usr->sex, *&usr->sex);

    printf("User has a size of %li bytes in memory", sizeof(*usr));
}

main.m

#import <UIKit/UIKit.h>

#import "HelloWorldAppDelegate.h"

#include <stdio.h>
#include <stdlib.h>

#include "user.h"

int main(int argc, char *argv[])
{
    User user1;
    CreateAndDisplay(&user1, "John Doe", 24, 'm');

    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([HelloWorldAppDelegate class]));
    }
}
3
  • 1
    Judging just by your screen snapshot you might want to try #include "user.h" in user.c. Looking at the #include file list of user.c. All that is there is #include <stdio.h> so no wonder the compiler has no clue what a User is. Commented Aug 17, 2013 at 21:39
  • @QuantumHive I moved your screen shots into your post directly. In the future, please actually put the code in your question inside code blocks as it makes your question stand on its own and is easier for others to compile, test, and edit the code on their own. Thanks! Commented Aug 17, 2013 at 21:50
  • Sorry, I will add the code now. Commented Aug 17, 2013 at 21:54

2 Answers 2

1

These errors are because there are two types referenced in user.c that haven't been declared in headers that it imports: User (defined in user.h) and uint (defined in <sys/types.h>). To resolve these errors, inside user.c you should add the following includes:

#include "user.h"
#include <sys/types.h>
Sign up to request clarification or add additional context in comments.

Comments

1

Try to include user.h in user.c, like you include stdio.h.

2 Comments

Hmm, thanks. I didn't know I had to include header files in the C implementation files. One error is gone: it now recognizes the struct. But the other three errors still remain: "Use of undeclared identifier 'uint': did you mean 'int'?" Why am I not able to use uint?
Okay solved the remaining errors. Apparently uint is a typedef and not a standard type for C, hence why it doesn't recognize it in the .c file, but does recognize it in the .m file. See link for the definition. I've changed uint to unsigned int in user.c which resolved the errors. unsigned int is a standard type for the ANSI C Language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.