我正在浏览斯坦福CS106B在线课程,并创建了一个文件夹/ usr / local,我在其中放置了课程的头文件。但是,每当我调用头文件时,我都会遇到问题。举个例子吧。当我尝试编译它时,我得到错误,但RandomChance(double n)是头文件random.h中的一个函数。救命啊!
架构x86_64的未定义符号: “RandomChance(double)”,引自: main.o中的flipCoin() ld:找不到架构x86_64的符号 clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)
该计划如下:
#include <iostream>
#include <string>
#include "/usr/local/random.h"
using namespace std;
string flipCoin();
string flipCoin() {
if (RandomChance(0.50)) {
return "heads";
} else {
return "tails";
}
}
int main (int argc, const char * argv[])
{
cout << "Enter number of votes" << endl;
cout << "Enter percentage spread between candidates" << endl;
cout << "Enter voting error percentage" << endl;
return 0;
}
这是random.h
/*
* Function: Randomize
* Usage: Randomize();
* -------------------
* This function sets the random seed so that the random sequence
* is unpredictable. If this function is not called, the other
* functions will return the same values on each run. During the
* debugging phase, it is best not to call this function, so that
* program behavior is repeatable.
*/
void Randomize();
/*
* Function: RandomInteger
* Usage: n = RandomInteger(low, high);
* ------------------------------------
* This function returns a random integer in the range low to high,
* inclusive.
*/
int RandomInteger(int low, int high);
/*
* Function: RandomReal
* Usage: d = RandomReal(low, high);
* ---------------------------------
* This function returns a random real number in the half-open
* interval [low .. high), meaning that the result is always
* greater than or equal to low but strictly less than high.
*/
double RandomReal(double low, double high);
/*
* Function: RandomChance
* Usage: if (RandomChance(p)) . . .
* ---------------------------------
* The RandomChance function returns true with the probability
* indicated by p, which should be a floating-point number between
* 0 (meaning never) and 1 (meaning always). For example, calling
* RandomChance(.30) returns true 30 percent of the time.
*/
bool RandomChance(double p);
#endif /*RANDOM_H_*/
更新:所以它有效,然后我通过#include simpio.h
添加了另一个库,整个过程变得混乱。它显示了38个错误,包括以下内容。
Ld /Users/alexandermoreno/Library/Developer/Xcode/DerivedData/voting-ccwjagcijcytcigfgxvxyiaplsxm/Build/Products/Debug/voting normal i386
cd "/Users/alexandermoreno/Documents/Paradigms/assignment 1/voting"
setenv MACOSX_DEPLOYMENT_TARGET 10.7
/Developer/usr/bin/clang++ -arch i386 -isysroot /Developer/SDKs/MacOSX10.7.sdk -L/Users/alexandermoreno/Library/Developer/Xcode/DerivedData/voting-ccwjagcijcytcigfgxvxyiaplsxm/Build/Products/Debug -F/Users/alexandermoreno/Library/Developer/Xcode/DerivedData/voting-ccwjagcijcytcigfgxvxyiaplsxm/Build/Products/Debug -filelist /Users/alexandermoreno/Library/Developer/Xcode/DerivedData/voting-ccwjagcijcytcigfgxvxyiaplsxm/Build/Intermediates/voting.build/Debug/voting.build/Objects-normal/i386/voting.LinkFileList -mmacosx-version-min=10.7 -lcs106 -o /Users/alexandermoreno/Library/Developer/Xcode/DerivedData/voting-ccwjagcijcytcigfgxvxyiaplsxm/Build/Products/Debug/voting
Undefined symbols for architecture i386:
"___CFConstantStringClassReference", referenced from:
CFString in libcs106.a(mainwrapper.o)
CFString in libcs106.a(mainwrapper.o)
CFString in libcs106.a(mainwrapper.o)
CFString in libcs106.a(mainwrapper.o)
CFString in libcs106.a(mainwrapper.o)
CFString in libcs106.a(mainwrapper.o)
CFString in libcs106.a(mainwrapper.o)
...
"_NSApp", referenced from:
+[Glue showAbout:] in libcs106.a(mainwrapper.o)
TerminateApp() in libcs106.a(mainwrapper.o)
+[NSBundleWithoutSpuriousErrorLog aloadNibNamed:owner:] in libcs106.a(mainwrapper.o)
_main in libcs106.a(mainwrapper.o)
-[ConsoleTextView keyDown:] in libcs106.a(ConsoleTextView.o)
+[Glue showAbout:] in libcs106.a(mainwrapper.o)
TerminateApp() in libcs106.a(mainwrapper.o)
+[NSBundleWithoutSpuriousErrorLog aloadNibNamed:owner:] in libcs106.a(mainwrapper.o)
_main in libcs106.a(mainwrapper.o)
-[ConsoleTextView keyDown:] in libcs106.a(ConsoleTextView.o)
"_objc_msgSendSuper", referenced from:
+[NSBundleWithoutSpuriousErrorLog aloadNibNamed:owner:] in libcs106.a(mainwrapper.o)
-[ConsoleTextView initWithFrame:] in libcs106.a(ConsoleTextView.o)
-[ConsoleTextView setFont:] in libcs106.a(ConsoleTextView.o)
-[ConsoleTextView dealloc] in libcs106.a(ConsoleTextView.o)
-[ConsoleTextView keyDown:] in libcs106.a(ConsoleTextView.o)
-[ConsoleTextView rangeForUserTextChange] in libcs106.a(ConsoleTextView.o)
"_CFBundleGetInfoDictionary", referenced from:
_main in libcs106.a(mainwrapper.o)
"_CFBundleCopyBundleURL", referenced from:
_main in libcs106.a(mainwrapper.o)
"_objc_msgSend", referenced from:
+[Glue applicationDidFinishLaunching:] in libcs106.a(mainwrapper.o)
+[Glue showAbout:] in libcs106.a(mainwrapper.o)
CocoaConsoleResize(int, int, int, int) in libcs106.a(mainwrapper.o)
TerminateApp() in libcs106.a(mainwrapper.o)
SetConsoleSize(int, int, int, bool) in libcs106.a(mainwrapper.o)
patched_read(void*, char*, int) in libcs106.a(mainwrapper.o)
CreateMenuWithItems(NSString*, menuitem*, int) in libcs106.a(mainwrapper.o)
...
"_CFURLCreateCopyDeletingLastPathComponent", referenced from:
_main in libcs106.a(mainwrapper.o)
"_CFURLGetFileSystemRepresentation", referenced from:
_main in libcs106.a(mainwrapper.o)
"_CFBundleGetMainBundle", referenced from:
_main in libcs106.a(mainwrapper.o)
"Main()", referenced from:
+[Glue studentMain] in libcs106.a(mainwrapper.o)
"_CFRelease", referenced from:
_main in libcs106.a(mainwrapper.o)
"_NSFileHandleDataAvailableNotification", referenced from:
-[ConsoleTextView registerForNotifications] in libcs106.a(ConsoleTextView.o)
"_NSForegroundColorAttributeName", referenced from:
-[ConsoleTextView setFont:] in libcs106.a(ConsoleTextView.o)
"_NSFontAttributeName", referenced from:
-[ConsoleTextView setFont:] in libcs106.a(ConsoleTextView.o)
+[ConsoleTextView contentSizeForPointSize:numRows:numCols:] in libcs106.a(ConsoleTextView.o)
"_NSBeep", referenced from:
-[ConsoleTextView keyDown:] in libcs106.a(ConsoleTextView.o)
"_objc_msgSend_fpret", referenced from:
+[ConsoleTextView contentSizeForPointSize:numRows:numCols:] in libcs106.a(ConsoleTextView.o)
"_objc_msgSend_stret", referenced from:
-[ConsoleTextView setPointSize:numRows:numCols:center:] in libcs106.a(ConsoleTextView.o)
+[ConsoleTextView contentSizeForPointSize:numRows:numCols:] in libcs106.a(ConsoleTextView.o)
+[ConsoleTextView createConsoleInWindow] in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSThread", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(mainwrapper.o)
".objc_class_name_NSAutoreleasePool", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(mainwrapper.o)
".objc_class_name_NSDictionary", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(mainwrapper.o)
".objc_class_name_NSString", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(mainwrapper.o)
pointer-to-literal-objc-class-name in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSMenu", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(mainwrapper.o)
".objc_class_name_NSMenuItem", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(mainwrapper.o)
".objc_class_name_NSBundle", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(mainwrapper.o)
.objc_class_name_NSBundleWithoutSpuriousErrorLog in libcs106.a(mainwrapper.o)
(maybe you meant: .objc_class_name_NSBundleWithoutSpuriousErrorLog)
".objc_class_name_NSApplication", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(mainwrapper.o)
".objc_class_name_NSObject", referenced from:
.objc_class_name_Glue in libcs106.a(mainwrapper.o)
".objc_class_name_NSAttributedString", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSMutableDictionary", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSFont", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSScroller", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSScreen", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSWindow", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSScrollView", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSColor", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSNotificationCenter", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSFileHandle", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSData", referenced from:
pointer-to-literal-objc-class-name in libcs106.a(ConsoleTextView.o)
".objc_class_name_NSTextView", referenced from:
.objc_class_name_ConsoleTextView in libcs106.a(ConsoleTextView.o)
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是#simpio.h文件
#ifndef SIMPIO_H_
#define SIMPIO_H_
/*
* File: simpio.h
* Version: 1.0CPP
* Last modified on Wed Sep 18 13:34:29 PDT 2002 by jzelenski
* ----------------------------------------------------------
* This interface provides access to a simple package of
* functions that simplify the reading of console input.
*/
#include "genlib.h"
/*
* Function: GetInteger
* Usage: n = GetInteger();
* ------------------------
* GetInteger reads a line of text from standard input and scans
* it as an integer. The integer value is returned. If an
* integer cannot be scanned or if more characters follow the
* number, the user is given a chance to retry.
*/
int GetInteger();
/*
* Function: GetLong
* Usage: n = GetLong();
* ---------------------
* GetLong reads a line of text from standard input and scans
* it into a long integer. The long is returned. If the
* number cannot be scanned or if extra characters follow it,
* the user is given a chance to retry.
*/
long GetLong();
/*
* Function: GetReal
* Usage: x = GetReal();
* ---------------------
* GetReal reads a line of text from standard input and scans
* it as a double. If the number cannot be scanned or if extra
* characters follow after the number ends, the user is given
* a chance to reenter the value.
*/
double GetReal();
/*
* Function: GetLine
* Usage: s = GetLine();
* ---------------------
* GetLine reads a line of text from standard input and returns
* the line as a string. The newline character that terminates
* the input is not stored as part of the string that is returned.
*/
string GetLine();
#endif /*SIMPIO_H_*/
答案 0 :(得分:1)
好的,所以我看到这被标记为一个受欢迎的问题。对于任何这样做的人,我只是建议查找更高版本的cs106b,而不是SEE或iTunes中的pset,并下载这些,只是等待解决方案出来,如果你需要它们。很多时候,它已经被设置为使用最新版本的XCode。如果没有,可能会有点混乱。他们还有一些文档描述了在大多数CS106b站点上链接器错误的情况下该怎么做。
答案 1 :(得分:0)
标题中有什么?是否有函数的实际定义或只是声明?
此外,我怀疑您要将文件放在/usr/local/include
中,我怀疑/usr/local
是否在您的标题搜索路径中。
编辑:
库有定义,标题有声明。将库放在一个搜索到的库路径中,/usr/local/lib
可能会让你自豪。
编辑#2:
回应:
嘿,我这样做了,并且还使用链接二进制文件来链接 图书馆。然后我得到了这个&#34;忽略文件 /Developer/SDKs/MacOSX10.7.sdk/usr/local/lib/libcs106.a,失踪 在文件中需要架构x86_64。&#34;难道有东西吗? 用于xcode 4的库有问题吗?这是另一个论坛 暗示。
该库似乎可能是针对32位架构编译的,而项目的架构设置为64位。如果库的64位版本不可用,则将项目设置更改为使用32位应该可以解决问题。
编辑#3:编辑#3: 看起来新库是针对objective-c的,或者您正在使用.m文件扩展名并编译为objective-c。