C++ 在 xcode 8 中包含另一个项目的 header
C++ Including header from another project in xcode 8
我在 mac 上使用 Xcode 8.2,我正在尝试包含来自另一个项目的 header。我做了一个简单的例子来解决这个问题,因为我正在处理的问题太大 post。
这是我要包括的项目:
#ifndef personType_hpp
#define personType_hpp
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
class personType {
public:
personType();
personType(string, string);
~personType();
void setName(string, string);
string getFName();
string getLname();
void print() const;
private:
string firstName;
string lastName;
};
#endif /* personType_hpp */
它的 .cpp 是:
#include "personType.hpp"
personType::personType() {
firstName = "";
lastName = "";
}
personType::personType(string fn, string ln): firstName(fn), lastName(ln) {
}
personType::~personType() {
}
void personType::setName(string fn, string ln) {
firstName = fn;
lastName = ln;
}
string personType::getFName() {
return firstName;
}
string personType::getLname() {
return lastName;
}
void personType::print() const {
cout << firstName << " " << lastName;
}
这是我制作的一个简单文件,只是为了显示我收到的错误。
#include <iostream>
#include <string>
#include "personType.hpp"
using namespace std;
int main() {
personType person1;
person1.setName("bob", "smith");
person1.print();
}
我得到的错误是:
warning: skipping file '/Users/idontwanttogivemyname/Desktop/C++ Projects/malikBook/malikBookEx11_3/malikBookEx11_3/personType.hpp' (unexpected file type 'sourcecode.cpp.h' in Frameworks & Libraries build phase)
Undefined symbols for architecture x86_64:
"personType::setName(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.o
"personType::personType()", referenced from:
_main in main.o
"personType::~personType()", referenced from:
_main in main.o
"personType::print() const", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
警告来自我在“常规”选项卡下的“链接框架和库”部分中选择上面的 header 文件 (personType.hpp)(这样做允许我将文件包含在主文件中而无需Xcode抱怨)。
作为免责声明:我一直在谷歌上广泛搜索这个问题。这看起来非常基本,因为我确定许多用户需要从他们计算机的其他部分访问 headers,但我无法找到 Xcode 的可靠答案(在过去一年内) ...)。让我知道是否需要提供更多信息。
谢谢!
编辑:
我已经成功将personType.cpp文件添加到编译列表中,但是现在使用相同的程序,它说在我正在处理的大项目中找不到该文件。这是它确实有效的示例项目的屏幕截图,然后是它不起作用的示例项目的屏幕截图。我看不出有什么理由不把它包含在这一个中...
Working example
Not working...
我得到的错误很简单:
/Users/idontwanttogivemyname/Desktop/C++ Projects/malikBook/malikBookGradeReportEx/malikBookGradeReportEx/studentType.hpp:15:10: 'personType.hpp' file not found
personType
的方法到处都找不到。是的,它们在 header 中声明,但找不到它的实际代码。这就像问某人 "there's this thing called plus" 然后 "what's 3 + 5?" 而没有实际告诉他们如何将东西加在一起。
要添加代码,您需要编译 personType.cpp
,然后一起编译 main.cpp
和 link。
尝试检查 Xcode 中的构建步骤或项目文件,确保 main.cpp
和 personType.cpp
一起编译。
将您想要 #include 的文件的副本添加到您当前正在处理的项目中,以确保所有内容都一起编译并一起链接 在 IDE.
目前,您只是引用在 header 中声明的资产,但在另一个项目的 .cpp 文件中有实现。必须正确编译和链接底层代码。
我在 mac 上使用 Xcode 8.2,我正在尝试包含来自另一个项目的 header。我做了一个简单的例子来解决这个问题,因为我正在处理的问题太大 post。
这是我要包括的项目:
#ifndef personType_hpp
#define personType_hpp
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
class personType {
public:
personType();
personType(string, string);
~personType();
void setName(string, string);
string getFName();
string getLname();
void print() const;
private:
string firstName;
string lastName;
};
#endif /* personType_hpp */
它的 .cpp 是:
#include "personType.hpp"
personType::personType() {
firstName = "";
lastName = "";
}
personType::personType(string fn, string ln): firstName(fn), lastName(ln) {
}
personType::~personType() {
}
void personType::setName(string fn, string ln) {
firstName = fn;
lastName = ln;
}
string personType::getFName() {
return firstName;
}
string personType::getLname() {
return lastName;
}
void personType::print() const {
cout << firstName << " " << lastName;
}
这是我制作的一个简单文件,只是为了显示我收到的错误。
#include <iostream>
#include <string>
#include "personType.hpp"
using namespace std;
int main() {
personType person1;
person1.setName("bob", "smith");
person1.print();
}
我得到的错误是:
warning: skipping file '/Users/idontwanttogivemyname/Desktop/C++ Projects/malikBook/malikBookEx11_3/malikBookEx11_3/personType.hpp' (unexpected file type 'sourcecode.cpp.h' in Frameworks & Libraries build phase)
Undefined symbols for architecture x86_64:
"personType::setName(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.o
"personType::personType()", referenced from:
_main in main.o
"personType::~personType()", referenced from:
_main in main.o
"personType::print() const", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
警告来自我在“常规”选项卡下的“链接框架和库”部分中选择上面的 header 文件 (personType.hpp)(这样做允许我将文件包含在主文件中而无需Xcode抱怨)。
作为免责声明:我一直在谷歌上广泛搜索这个问题。这看起来非常基本,因为我确定许多用户需要从他们计算机的其他部分访问 headers,但我无法找到 Xcode 的可靠答案(在过去一年内) ...)。让我知道是否需要提供更多信息。
谢谢!
编辑:
我已经成功将personType.cpp文件添加到编译列表中,但是现在使用相同的程序,它说在我正在处理的大项目中找不到该文件。这是它确实有效的示例项目的屏幕截图,然后是它不起作用的示例项目的屏幕截图。我看不出有什么理由不把它包含在这一个中...
Working example
Not working...
我得到的错误很简单:
/Users/idontwanttogivemyname/Desktop/C++ Projects/malikBook/malikBookGradeReportEx/malikBookGradeReportEx/studentType.hpp:15:10: 'personType.hpp' file not found
personType
的方法到处都找不到。是的,它们在 header 中声明,但找不到它的实际代码。这就像问某人 "there's this thing called plus" 然后 "what's 3 + 5?" 而没有实际告诉他们如何将东西加在一起。
要添加代码,您需要编译 personType.cpp
,然后一起编译 main.cpp
和 link。
尝试检查 Xcode 中的构建步骤或项目文件,确保 main.cpp
和 personType.cpp
一起编译。
将您想要 #include 的文件的副本添加到您当前正在处理的项目中,以确保所有内容都一起编译并一起链接 在 IDE.
目前,您只是引用在 header 中声明的资产,但在另一个项目的 .cpp 文件中有实现。必须正确编译和链接底层代码。