链接器命令失败,退出代码1 - Xcode
linker command failed with exit code 1 - Xcode
我一直得到这个错误,我不知道为什么。我已经在其他应用程序中实现了这个方法,但由于某种原因,它不适合这个…
我有以下内容:
ViewController.h:
NSInteger HighScore;
ViewController.m:
- (void)viewDidLoad {
...
//load highscores
HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
HighscoreLabel.text = [NSString stringWithFormat:@"%li", (long)HighScore];
}
Game.m:
#import "ViewController.h"
...
//set/save new highscore
if(Score > HighScore){
[[NSUserDefaults standardUserDefaults] setInteger:Score forKey:@"HighScoreSaved"];
}
它一直返回一个失败的构建,链接器错误说"重复符号"。
我很困惑。我甚至尝试添加一个全局标题,并将其导入ViewController和Game,但我仍然得到链接器错误?:
Global.h:
#ifndef _Global_h
#define _Global_h
NSInteger HighScore;
#endif
ViewController.m:
#import "Global.h"
- (void)viewDidLoad {
...
//load highscores
HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
HighscoreLabel.text = [NSString stringWithFormat:@"%li", (long)HighScore];
}
Game.m:
#import "Global.h"
...
//set/save new highscore
if(Score > HighScore){
[[NSUserDefaults standardUserDefaults] setInteger:Score forKey:@"HighScoreSaved"];
}
Xcode会有问题吗?我尝试过典型的"干净构建"等等……还是我做了什么蠢事?谢谢。
基于molbdnilo的答案的UPDATE
虽然这不是我以前实现它的方式,但它现在与这个实现一起工作:
ViewController.h:
extern NSInteger HighScore;
ViewController.m:
//load highscore
HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
HighscoreLabel.text = [NSString stringWithFormat:@"%li", (long) HighScore];
Game.h:
NSInteger HighScore; //exactly as declared in ViewController.h
Game.m:
//if higher score, overwrite
if (Score > HighScore){
[[NSUserDefaults standardUserDefaults] setInteger:Score forKey:@"HighScoreSaved"];
}
每次在某处包含/导入文件时,您的HighScore
变量获得一个定义。
(有关血腥的细节,请查阅"翻译单位"概念。)
如果你真的,真的想使用一个全局变量,你需要声明它"extern"在头文件中:
extern NSInteger HighScore;
和在一个源文件中定义:
NSInteger HighScore;
相关文章:
- Xcode 语义问题引用或以前定义的代码
- Xcode:链接器命令失败,退出代码为 1(使用 -v 查看调用)[C++]
- 如何在C++代码之上使用标准的Xcode GUI
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- 线程 1:xcode 中出现EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)错误
- Xcode 10.0:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- Xcode:线程1:exc_bad_access(代码= 1,地址= 0x0)在缩短列表时
- 如何使用可执行文件将我的C++代码合并到我的 Xcode 项目中
- 将C 代码桥接到我的Swift代码中.哪些文件扩展名为XCode中的基于C的语言
- C 11模板代码 - 无法在XCode中编译
- 当我尝试构建和运行C 程序时,获取Xcode错误代码
- 我在 Xcode 中的 C++ 代码无法正常工作
- 链接器命令失败,退出代码 1 Xcode 9
- Xcode C :链接命令失败,出口代码1未找到-lboost_regex
- 当我在Xcode上访问矩阵(openCV垫子)的位置时,为什么我会得到未定义的行为(exc_bad_access(代码=
- 在独立的 Xcode 操场中运行 c 或 c++ 代码
- Xcode链接器错误:链接器命令失败,退出代码为1(使用-v查看调用)
- 编译 c++ 代码时 xcode 上的线程
- 代码示例 "switch" (b.eckel) 不在 Xcode 上构建/运行,而是在 QT 上构建/运行
- 为iOS和Android编译c++代码(Xcode).这是真的吗?