objective-cundefined-symbol

How to remove "undefined symbol" error in Objective-C?


I declare the variable ue in my global.h file as Extern int ue;. I have #include "global.h" on all view controllers where I want to use the variable. If I don't assign a value to ue my project builds and runs. If I assign a value to ue I get the following errors and the build fails;

undefined symbol: _ue
Linker command failed with exit code 1 (use -v to see invocation)

I have searched for the resolution and tried some suggestions, but I keep getting the same errors. I have declared multiple variables in "global.h" previously and had no issues.


Solution

  • extern modifier means the value is assigned to variable in some other file, so you should not perform it in-place. There are two options to solve your issue.

    Option 1: Declare your variable in global.h as static instead of extern.

    static int ue = 1;
    

    Option 2: Create an Obj-C class with global.h and global.m files. Remove all contents (interface/implementation) from both files (you should only keep the #import directive in .m file). In your .h file declare an extern variable.

    extern int ue;
    

    In your .m file assign the value:

    int ue = 1;