在 Xcode 中编译 C++ 类:编译期间出错:stl 向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/534861/
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
compiling a C++ class in Xcode: error during compilation: stl vector
提问by cbrulak
I have a C++ class that compiles fine on linux with gcc and on widows in visual studio.
我有一个 C++ 类,它可以在带有 gcc 的 linux 和 Visual Studio 中的寡妇上很好地编译。
boid.h:
boid.h:
#ifndef BOID_CLASS_HEADER_DEFINES_H
#define BOID_CLASS_HEADER_DEFINES_H
#include "defines.h"
class Boid {
public:
// Initialize the boid with random position, heading direction and color
Boid(float SceneRadius,float NormalVel);
.....
protected:
...
};
#endif
and in boid.cpp:
在 boid.cpp 中:
#include "Boid.h"
// Initialize the boid with random position, heading direction and color
Boid::Boid(float SceneRadius,float NormalVel)
{
....
}
However, I get the following error when I compile this code in Xcode:
但是,在 Xcode 中编译此代码时出现以下错误:
Compiling Boid.h: "error: vector: No such file or directory"
Any ideas? I thought you could take C/C++ code and compile it in Xcode without issues?
有任何想法吗?我以为您可以使用 C/C++ 代码并在 Xcode 中编译它而不会出现问题?
Thanks
谢谢
EDIT: Added defines.h (also added #endif to sample, but that was in the original code)
编辑:添加了defines.h(还添加了#endif 到示例,但这是在原始代码中)
EDIT 2: I am getting a different error after a commenting out a couple of includes there were empty: the vector error above.
编辑 2:在注释掉几个包含空的包含后,我收到一个不同的错误:上面的向量错误。
#ifndef BOID_NAV_DEFINES_H
#define BOID_NAV_DEFINES_H
#include <stdlib.h>
#include <vector>
#include "Vector3d.h"
#include "Point3d.h"
#include "win_layer.h"
#endif
回答by epatel
Are you including the C++ header in a .m file?
您是否在 .m 文件中包含 C++ 头文件?
.m files are treated as .c files with Objective-C extensions.
.m 文件被视为具有 Objective-C 扩展名的 .c 文件。
.mm files are treated as .cpp files with Objective-C extensions, then it's called Objective-C++
.mm 文件被视为具有 Objective-C 扩展名的 .cpp 文件,那么它被称为 Objective-C++
Just rename any .m file to .mm, right-click or ctrl-click and select rename on the file in Xcode.
只需将任何 .m 文件重命名为 .mm,右键单击或按住 ctrl 单击并在 Xcode 中的文件上选择重命名。
回答by John Riselvato
Without changing any .m to .mm or anything like that, if you click your project, click tagets->build settings go all the way down to "LLVM GCC 4.2 - Languages"(new xcode says "Apple LLVM compiler 4.2") you will see Compile Sources Aschange that value to Objective-C++;
在不将任何 .m 更改为 .mm 或类似内容的情况下,如果您单击您的项目,单击 tagets->build settings 一直到“LLVM GCC 4.2 - Languages”(新的 xcode 说“Apple LLVM compiler 4.2”)你将看到Compile Sources As将该值更改为Objective-C++;
回答by DrArt
I hope this will help. After updating xCode to version 10, I have had issues including < map > and < vector > libraries. Found an easy solution by changing the C++ library type in the project's build settings (target's Build Settings):
我希望这将有所帮助。将 xCode 更新到版本 10 后,我遇到了包括 <map> 和 <vector> 库在内的问题。通过在项目的构建设置(目标的构建设置)中更改 C++ 库类型,找到了一个简单的解决方案:
C++ Standard Library: libc++ (LLVM C++ standard library with C++ 11 support)
C++ 标准库:libc++(支持 C++ 11 的 LLVM C++ 标准库)
Compiled without any problem.
编译没有任何问题。
回答by codelogic
Make sure you're compiling it as C++. Right click the file in XCode and select Get Infoand make sure that File Typeis set to sourcecode.cpp.cppfor the implementation files.
确保将其编译为 C++。右键单击 XCode 中的文件并选择“获取信息”,并确保将实现文件的文件类型设置为sourcecode.cpp.cpp。
回答by Sol
Assuming you're talking about the OS X XCode, that uses gcc to do the actual compiling. So there should be no difference between that and Linux, other than maybe different versions of gcc.
假设您在谈论 OS X XCode,它使用 gcc 进行实际编译。所以除了 gcc 的不同版本之外,它和 Linux 之间应该没有区别。
First thing that jumps out at me here is that you've typed "boid.h" as the name of the file, but you're including "Boid.h". Assuming that's not a typo, I would expect that to cause trouble on both Linux and OS X....
在这里,我首先想到的是您输入了“boid.h”作为文件名,但您包含了“Boid.h”。假设这不是一个错字,我希望这会在 Linux 和 OS X 上引起麻烦......
Edited to answer the new question: Hmmm... vector is definitely part of Xcode: /Developer/SDKs/MacOSX10.5.sdk/usr/include/c++/4.0.0/vector
on my machine.
编辑回答新问题:嗯...矢量绝对是 Xcode 的一部分:/Developer/SDKs/MacOSX10.5.sdk/usr/include/c++/4.0.0/vector
在我的机器上。
Further thought: If you port the source and makefiles from the Linux build over to the Mac, you can probably just compile it from the command line exactly like you do on Linux....
进一步思考:如果您将 Linux 构建中的源代码和 makefile 移植到 Mac,您可能可以像在 Linux 上一样从命令行编译它......
回答by cbrulak
This issue had two errors:
这个问题有两个错误:
- one of my includes had a typo which caused a compile error
- the vector not found error was fixed by the .m files to .mm
- 我的一个包含有一个错字,导致编译错误
- 未找到向量错误已由 .m 文件修复为 .mm
回答by vdsf
Not sure if you forgot to paste, but you have an unterminated #ifndef
不确定您是否忘记粘贴,但您有一个未终止的 #ifndef
What's inside defines.h?
define.h里面有什么?
Edit:You seem to have found the solution. One more remark:
编辑:您似乎找到了解决方案。再说一句:
#include <stdlib.h>
For C++, please:
对于 C++,请:
#include <cstdlib>
:D
:D
回答by Diego Sevilla
Definitely, something in defines.h is affecting the class definition.
毫无疑问,defines.h 中的某些内容正在影响类定义。