c++Mac Xcode:体系结构x86_64:的未定义符号



我在Mac 上的Xcode中用c++运行以下代码

int fibo(int x)
{
      if (x==1||x==2)
          return 1;
          else
              return fibo(x-1)+fibo(x-2);
}

并且接收到该错误无法知道原因。

undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

有人能帮我吗?

您需要定义一个main函数。这是第一个被调用来"启动"程序的函数。

将此添加到您的文件中:

int main()
{
    fibo(10);  // calls your function with
}

您应该实现main()函数。

在初始化后程序启动时调用主函数具有静态存储持续时间的非本地对象。它是在宿主中执行的程序的指定入口点环境(即具有操作系统)。入口指向独立程序(引导加载程序、操作系统内核等)实现定义。http://en.cppreference.com/w/cpp/language/main_function

#include <iostream> // for std::cout
int fibo(int x)
{
      if (x==1||x==2)
          return 1;
          else
              return fibo(x-1)+fibo(x-2);
}
int main() 
{
      int x = 1;
      int result = fibo(x);
      std::cout << "Result: " << x; // Printing result
      return 0;
}

最新更新