架构 x86_64 的未定义符号:Xcode 5 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21966887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined symbols for architecture x86_64: Xcode 5 Issues
提问by heri-salmas
I am learning to code C++ in Xcode, i am doing this example from the book by Joe Pitt-Francis and Jonathan Whiteley. "Guide to Scientific Computing in C++"
我正在学习在 Xcode 中编写 C++ 代码,我正在从 Joe Pitt-Francis 和 Jonathan Whiteley 的书中做这个例子。“C++ 科学计算指南”
The code is as follow,
代码如下,
Book.ccp
书.cc
#include "Book.hpp"
#include <iostream>
int main (int argc, char* argv[])
{
Book good_read;
good_read.author = "Lewis Carroll";
good_read.title = "Alice ";
good_read.publisher = "MA";
good_read.price = 199;
good_read.format = "hardback";
good_read.SetYearOfPublication(1787);
std::cout << "Year of publication of " << good_read.title << " is " << good_read.GetYearOfPublication() << "\n";
Book another_book(good_read);
Book an_extra_book ("the magic");
return 0;
}
Book.hpp
书.hpp
#ifndef book_Header_h
#define book_Header_h
#include <string>
class Book
{
public:
Book ();
Book (const Book& otherBook);
Book (std::string bookTitle);
std::string author, title, publisher, format;
int price;
void SetYearOfPublication (int year);
int GetYearOfPublication() const;
private:
int mYearOfPublication;
};
#endif
When I complie, I got the follow error.
当我遵守时,出现以下错误。
Undefined symbols for architecture x86_64:
"Book::SetYearOfPublication(int)", referenced from:
_main in Book.o
"Book::Book(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in Book.o
"Book::Book(Book const&)", referenced from:
_main in Book.o
"Book::Book()", referenced from:
_main in Book.o
"Book::GetYearOfPublication() const", referenced from:
_main in Book.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
In several forums it says that the issue is that Xcode is not compiling all the files, but I have no idea on how to solve it, any suggestion is more than welcome.
在几个论坛中,它说问题是 Xcode 没有编译所有文件,但我不知道如何解决它,任何建议都非常受欢迎。
回答by Oswald
You declare function Book::SetYearOfPublication()
in Book.hpp, but you never define it. You have to do something like this in an implementation file:
您Book::SetYearOfPublication()
在Book.hpp 中声明函数,但从未定义它。你必须在一个实现文件中做这样的事情:
void Book::SetYearOfPublication (int year) {
// TODO: add code for setting the publication year
}