I am trying to create some string constants so I have the following:
Constants.h
extern NSString* const CONSTANT_ONE;
Constants.m
NSString *const CONSTANT_ONE = #"CONSTANT_ONE";
SomeOtherFile.m
#import "Constants.h"
... snip
NSString* something = CONSTANT_ONE;
Constants.m is listed in the "Compile Sources" bit of the target's build phases and is a member of the target when I look in its "Target Membership" section of the sidebar.
When I try to compile I get an error like:
Undefined symbols for architecture i386:
"_CONSTANT_ONE", referenced from:
-[SomeOtherFile someMethod:] in SomeOtherFile.o
I think I might be misunderstanding something basic here - can anyone help me with this error please?
The code in my initial question was correct. I don't know why but simply closing and re-opening Xcode solved the problem and it built fine. Just Xcode getting itself confused.
Related
I have a linting error on my objC but I don't know how to resolve it, if I'm using the reinterpret_cast syntax, the app does not build anymore... Someone has an idea please?
error: NSString+EXT.h:9: Using C-style cast. Use reinterpret_cast(...) instead [readability/casting]
NSString+EXT.h
#ifndef ATOM_BROWSER_UI_COCOA_NSSTRING_ANSI_H_
#define ATOM_BROWSER_UI_COCOA_NSSTRING_ANSI_H_
#import <Foundation/Foundation.h>
#interface NSString(ANSI)
- (BOOL)containsANSICodes;
- (NSMutableAttributedString*)attributedStringParsingANSICodes;
#end
#endif // ATOM_BROWSER_UI_COCOA_NSSTRING_ANSI_H_
After multiple research and test, I fix the issue by following this thread
A macro like
#define REINTERPRET(type, expr) (*(type *)&(expr))
help me to refactorize and resolve the issue
Summary:
Error I'm experiencing is as title states.
I'm encountering a Reference to '' is ambiguous error. I've been unable to determine what the problem, whether if my import structure is set up incorrectly and the headers are somehow included multiple times or whatnot.
Due to request and requirements, I'm currently trying to build a single view iOS application using Objective-C and C while importing a custom pure C SDK. This is my first time using C in an iOS application, let alone including a whole SDK, so I'm not 100% sure if the process I'm doing this is correct, but have been able to resolve errors as I've been compiling.
SDK files are being included via a prefix.h which is imported into the single viewcontroller's header. I also read somewhere to use .pch instead of .h, but I'm not able to figure out how to do so given the requirements of this project...
Errors:
Reference to 'E_EXTERNAL_DEVICE_TYPE__CARD_ENTRY_DEVICE' is ambiguous.
"In file included from .../ViewController.m"
-"In file included from .../ViewController.h"
"In file included from .../prefix.h"
"Candidate found by name lookup is'_E_EXTERNAL_DEVICE_TYPE__CARD_ENTRY_DEVICE'"
"Candidate found by name lookup is'_E_EXTERNAL_DEVICE_TYPE__CARD_ENTRY_DEVICE'"
"Candidate found by name lookup is'_E_EXTERNAL_DEVICE_TYPE__CARD_ENTRY_DEVICE'"
Reference to 'E_EXTERNAL_DEVICE_CONNECT_TYPE__BLUETOOTH' is ambiguous.
"In file included from .../ViewController.m"
"In file included from .../ViewController.h"
"In file included from .../prefix.h"
"Candidate found by name lookup is'_E_EXTERNAL_DEVICE_CONNECT_TYPE__BLUETOOTH'"
"Candidate found by name lookup is'_E_EXTERNAL_DEVICE_CONNECT_TYPE__BLUETOOTH'"
"Candidate found by name lookup is'_E_EXTERNAL_DEVICE_CONNECT_TYPE__BLUETOOTH'"
Reference to 'E_EXTERNAL_DEVICE_TYPE__PRINTER' is ambiguous.
"In file included from .../ViewController.m"
"In file included from .../ViewController.h"
"In file included from .../prefix.h"
"Candidate found by name lookup is'_E_EXTERNAL_DEVICE_TYPE__PRINTER'"
"Candidate found by name lookup is'_E_EXTERNAL_DEVICE_TYPE__PRINTER'"
"Candidate found by name lookup is'_E_EXTERNAL_DEVICE_TYPE__PRINTER'"
// Device.c
...
// this and other similiar lines of code that throws the error
if((deviceType == E_EXTERNAL_DEVICE_TYPE__CARD_ENTRY_DEVICE) && E_EXTERNAL_DEVICE_CONNECT_TYPE__BLUETOOTH) {
CODE x = E_EXTERNAL_DEVICE_TYPE__PRINTER;
}
Solutions attempted
The strange part is, at one point I was able to build and run parts of the project by interatively uncommenting lines and debugging lines of code as I went along starting with SDKInitialize. But for some unknown reason, I'm now getting this error even though I've commented the SDKInitialize() in ViewController.m such that it's just an empty ViewController doing nothing.
I've also tried reverting back an older git version where the project could build fine, but it's still encountering the same error, which possibly leads me to believe this may be related to the XCode IDE or some kind of configuration settings...
cleaned project
deleted everything inside '~/Library/Developer/Xcode/DerivedData/ModuleCache/'
clean once more
build project
Already checked
Reference to 'X' is ambiguous
Reference to 'CMTime' is ambiguous
Reference to enum is ambiguous objective-c
Xcode: "Reference to NSString is ambiguous"
Build Settings
I have tried setting setting always_search_user_paths = No;
Always Search User Paths = Yes;
Header Search Paths[$(PROJECT_DIR)/.../.../.../SDK/Common] = recursive;
Header Search Paths[$(PROJECT_DIR)/.../.../.../SDK/Core] = recursive;
Header Search Paths[$(PROJECT_DIR)/.../.../.../GenericAppRoot] = non-recursive;
... etc
FYI app.xcodeproj exists inside ___/GenericAppRoot/Devices/iOS
File Structure
Common.h
//located in $(PROJECT_DIR)/.../.../.../SDK/Common
//Common.h
#ifndef __DTISDK_COMMON_H__
#define __DTISDK_COMMON_H__
//--------
// SDK Includes
//--------
//...
//--------------
// External Device Types
//--------------
typedef enum _E_EXTERNAL_DEVICE_TYPE
{
E_EXTERNAL_DEVICE_TYPE__PRINTER = 1,
E_EXTERNAL_DEVICE_TYPE__CARD_ENTRY_DEVICE = 2,
E_EXTERNAL_DEVICE_TYPE__PIN_ENTRY_DEVICE = 3,
} E_EXTERNAL_DEVICE_TYPE;
//...
typedef enum _E_EXTERNAL_DEVICE_CONNECTION_TYPE
{
E_EXTERNAL_DEVICE_CONNECT_TYPE__AUDIO = 1,
E_EXTERNAL_DEVICE_CONNECT_TYPE__BLUETOOTH = 2,
} E_EXTERNAL_DEVICE_CONNECTION_TYPE;
//...
#endif //__DTISDK_COMMON_H__
prefix.h
#ifndef prefix_h
#define prefix_h
#include "File.h"
#include "File.c"
#include "System.h"
#include "System.c"
#include "Common.h"
... etc
ViewController.h
#import <UIKit/UIKit.h>
#import "prefix.h"
#interface ViewController: UIViewController
#end
ViewController.m
#import "ViewController.h"
#interface ViewController()
#end
#implementation ViewController
- (void) viewDidLoad {
[super viewDidLoad];
// initialization callback from one of the SDK classes
SDKInitialize();
}
#end
Any additional help or insights would be much appreciated
You are including *.c files from prefix.h which is wrong. Always include only header files (*.h).
When you are including the implementation (*.c) files, the contents of the file is inserted as is, therefore you get the same definition in multiple places, leading to name collisions.
This question already has answers here:
Accesing global variable giving linker error in objective C
(2 answers)
Closed 9 years ago.
I'm making a small, simple application, so I decided to use global variables over Singletons. I'm also only using one.
My app pulls an int from a small preference file, and that is set to the global variable as an NSInteger. The global variable may be changed while the app is running.
AppController.h
#import <Cocoa/Cocoa.h>
extern NSInteger preferenceNumber;
#interface ....
App Controller.m
-(void)someMethod {
...
//fileContents is a string containing the int that is inside the file
preferenceNumber = [fileContents intValue]
...
}
The Linker Errors (2):
Undefined symbols for architecture x86_64:
"_preferenceNumber", referenced from:
-[AppController someMethod1] in AppController.o
-[AppController someMethod2:] in AppController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The bolded parts are the two errors.
What is causing this? What is the simplest, best way to solve it?
Just add one line in your implementation class:-
AppContollr.m
#implementation AppContoller
NSInteger preferenceNumber;
I've got a class for storing constants.
So, there are two files that call Constant.h and Constant.m
This is what I have in .h file:
#import <Foundation/Foundation.h>
enum kParams {
kFirstName = 0,
kLastName = 1
};
extern NSString * const kNotificationUpdateMainMenu;
This is what I have in .m file:
#import "Constants.h"
NSString * const kNotificationUpdateMainMenu = #"kNotificationUpdateMainMenu";
For first time it works good, but when I try to add some other const (kNotificationFbLoginSuccsess for example) other classes don't see it.
This is a message that shows me which problem I have. But I don't understand how my other constants work without this issue (just new constant that I add get this error).
/Users/developer/Documents/Projects/Test/Test/Test/AppDelegate.m:121:64: Use of undeclared identifier 'kNotificationFbLoginSuccsess'
I found some way how to fix it:
Open organizer
Clear derived data
Delete project.xcworkspace file and xcuserdata
Close Project
Relaunch Xcode
but as I think is too much operations that I can add one constant. How come?
Your "global" constant is not actually external (separately compiled and later linked together). Take the easy way out and place NSString * const kNotificationUpdateMainMenu = #"kNotificationUpdateMainMenu"; into the header file. The method file needs nothing.
I would use #define kNotificationUpdateMainMenu #"kNotificationUpdateMainMenu" to perform the spell checking. The compiler will create one shared instance of the constant string for the entire compilation.
I required to share a const value between two files; so rather than keeping a magical number I decided to use a const variable.
So I created a global variable const int viewTag = 100; in my appDelegate.m
Then accessed it as extern const int viewTag; but I got following linker error:
Undefined symbols for architecture i386:
"viewTag", referenced from:
-[xxxViewController launchxxx] in libxxx_iPad.a(xxxViewController.o)
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
But the above thing is perfectly leagal in normal C,C++ env. can anybody throw some light?
Update: putting extern const int viewTag; in header and importing that header works, but I really don't want to do that for a single const int
Try putting:
extern const int viewTag;
in appDelegate.h, then importing this header where you need access to viewTag.
Another way is to use #define viewTag 100 in .pch file.
But I think, the best way is to create .h file with constants and include where you want