C++在xcode 8中包含来自另一个项目的头
C++ Including header from another project in xcode 8
我在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.cpp
和personType.cpp
都编译在一起。
- 运行同一解决方案的另一个项目的项目
- 是否可以将 EXE 文件作为 lib 文件链接到另一个项目?
- 如何使用另一个项目命名空间?
- 单元测试类无法在我要测试的同一解决方案中的另一个项目中找到类
- 如果我尝试将对话框从一个项目移动到另一个项目,我是否需要从 rc 文件中复制 DESIGNINFO、对话框信息和AFX_
- OpenCV:文件存储操作员">>"在一个项目中工作,但不在另一个项目中工作(在同一类上)
- 如何将另一个项目中用 C 编写的源代码包含在我自己的项目中,C++在 Visual Studio 中
- 将过滤器中的所有.cpp和.h移动到另一个项目并更正其文件夹位置
- 在另一个项目中包括继承的类时,抽象类的链接器错误
- 创建在另一个项目中定义的类对象
- 如何在不复制的情况下将C++类从一个项目重用到另一个项目
- Cmakelist仅用于标题项目,并将其用于另一个项目
- 在另一个项目中将模板函数用作dll
- 在同一解决方案但不同配置的另一个项目中使用一个类
- 未解析的外部符号来自一个项目中的静态库,而不是另一个项目.(C++)
- std :: getline在一个项目中汇总了罚款,但不是另一个项目
- 导出visual studio c++项目以在另一个项目中使用,而不显示.cpp和.h源文件
- 有没有办法设置 Eclipse 以使用另一个项目的输出作为库?
- C++/VS 2012-多项目解决方案:一个项目中存在链接器错误,但另一个项目没有
- Boost:阻塞,直到队列有另一个项目