C++在xcode 8中包含来自另一个项目的头

C++ Including header from another project in xcode 8

本文关键字:另一个 项目 包含 xcode C++      更新时间:2023-10-16

我在mac上使用Xcode 8.2,并试图包含另一个项目的头。我做了一个快速的例子来回答这个问题,因为我正在处理的这个问题太大了,无法发布。

这是我想包括的项目:

#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)

警告来自于我在"常规"选项卡下的"链接的框架和库"部分中选择上面的头文件(personType.hpp)(这样做允许我在没有Xcode抱怨的情况下将该文件包含在主文件中)。

免责声明:我已经在谷歌上广泛搜索了这个问题。这似乎很基本,因为我相信许多用户需要从他们的计算机的其他部分访问标题,但我一直无法找到Xcode的可靠答案(在过去的一年里…)。如果我需要提供更多信息,请告诉我。

谢谢!

编辑:

我已经成功地将personType.cpp文件添加到编译列表中,但现在使用相同的过程,它说在我正在进行的大项目中找不到该文件。我看不出有什么理由不把它包括在这个。。。

工作示例

不工作。。。

我得到的错误很简单:

/Users/idontwanttogivemyname/Desktop/C++ Projects/malikBook/malikBookGradeReportEx/malikBookGradeReportEx/studentType.hpp:15:10: 'personType.hpp' file not found

将要#包含的文件的副本添加到当前正在处理的项目中,以确保所有内容都在IDE中一起编译并链接。

目前,您只是引用在标头中声明的资产,但在另一个项目的.cpp文件中有实现。必须正确编译和链接基础代码。

personType的方法在任何地方都找不到。是的,它们是在头中声明的,但找不到它的实际代码。这就像问某人"有一种叫做加号的东西",然后问"3+5是什么?"而没有告诉他们如何把东西加在一起。

要添加代码,您需要编译personType.cpp,然后编译main.cpp并将它们链接在一起。

尝试在Xcode中检查构建步骤或项目文件,以确保main.cpppersonType.cpp都编译在一起。

相关文章: