【问题标题】:Including header files in Xcode 4在 Xcode 4 中包含头文件
【发布时间】:2012-01-19 03:35:20
【问题描述】:

我正在浏览斯坦福 CS106B 在线课程,并创建了一个文件夹 /usr/local,我将在其中放置课程的头文件。但是,每当我调用头文件时,我都会遇到问题。以这个为例。当我尝试编译它时,我得到了错误,但 RandomChance(double n) 是头文件 random.h 中的一个函数。救命!

架构 x86_64 的未定义符号: “RandomChance(double)”,引用自: main.o 中的翻转币() 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;
}

这是随机的.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_*/

【问题讨论】:

  • 您只是在复制标题。它的实现文件呢?您得到的是链接器错误。检查 random.cpp 的目标文件是否在链接器正在查找的默认路径中。
  • 对不起,我有点菜鸟。没有random.cpp文件,也不知道什么是链接器。
  • 有一个文件 cs106lib.a 以及所有的头文件,不知道我是否应该用它做点什么。
  • 你去。这就是链接器正在寻找的目标文件。如果我没记错的话,它有 random.h 函数的定义。将该 cs106lib.a 文件复制到 /usr/local/lib/ 并重建应用程序。
  • 它已经与 random.h 文件在同一个文件夹中。另外,看看random.h,它看起来并没有真正定义函数,这可能是我的问题吗?只是看起来它初始化了它。

标签: c++ xcode4


【解决方案1】:

好的,我看到这被标记为热门问题。对于任何这样做的人,我只建议查找更高版本的 cs106b 而不是 SEE 或 iTunes 中的 pset,然后下载它们,然后等待解决方案出现(如果需要)。很多时候,它已经设置为可以与最新版本的 XCode 一起使用。如果没有,那么设置可能会有点混乱。他们还有一些文档描述了在大多数 CS106b 网站上出现链接器错误时该怎么做。

【讨论】:

    【解决方案2】:

    标题中有什么?该函数是否有实际定义或只是声明?

    另外,我怀疑你想把文件放在/usr/local/include,我怀疑/usr/local 在你的标题搜索路径中。

    编辑:

    库有定义,头有声明。将库放在搜索过的库路径之一中,/usr/local/lib 可能会让您感到自豪。

    编辑#2:

    回应:

    嘿,我这样做了,还使用了带有库的链接二进制文件来链接 图书馆。然后我得到了这个“忽略文件 /Developer/SDKs/MacOSX10.7.sdk/usr/local/lib/libcs​​106.a,缺失 文件中所需的架构 x86_64。”会不会有什么东西 在 xcode 4 中使用的库有问题吗?这就是另一个论坛 暗示。

    似乎该库是为 32 位架构编译的,而项目的架构设置为 64 位。如果 64 位版本的库不可用,将项目设置更改为使用 32 位应该可以解决问题。

    编辑#3: 看起来这个新库是为 Objective-c 设计的,或者您正在使用 .m 文件扩展名并编译为 Objective-c。

    【讨论】:

    • 标头具有函数 RandomChance 的实际定义。我会试试的,谢谢。
    • 我这样做并将其更改为#include "/usr/local/include/random.h",但我仍然遇到同样的错误。
    • @user1157605 在您的问题中发布“random.h”有什么。
    • 嘿,我这样做了,并且还使用了带有库的链接二进制文件来链接库。然后我得到这个“忽略文件/Developer/SDKs/MacOSX10.7.sdk/usr/local/lib/libcs​​106.a,文件中缺少所需的架构x86_64。”在 xcode 4 中使用的库可能有问题吗?这就是另一个论坛所暗示的。
    • 可能是该库已针对 32 位架构编译,在您的项目设置中您可以更改架构类型 - 将其设置为 32 位,您可能会很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    相关资源
    最近更新 更多