compiling a C++ class in Xcode: error during compi

2020-02-12 03:30发布

问题:

I have a C++ class that compiles fine on linux with gcc and on widows in visual studio.

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:

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

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?

Thanks

EDIT: Added defines.h (also added #endif to sample, but that was in the original code)

EDIT 2: I am getting a different error after a commenting out a couple of includes there were empty: the vector error above.

#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

回答1:

Are you including the C++ header in a .m file?

.m files are treated as .c files with Objective-C extensions.

.mm files are treated as .cpp files with Objective-C extensions, then it's called Objective-C++

Just rename any .m file to .mm, right-click or ctrl-click and select rename on the file in Xcode.



回答2:

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 As change that value to Objective-C++;



回答3:

Make sure you're compiling it as C++. Right click the file in XCode and select Get Info and make sure that File Type is set to sourcecode.cpp.cpp for the implementation files.



回答4:

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

C++ Standard Library: libc++ (LLVM C++ standard library with C++ 11 support)

Compiled without any problem.



回答5:

Definitely, something in defines.h is affecting the class definition.



回答6:

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


回答7:

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.

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....

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.

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....



回答8:

Not sure if you forgot to paste, but you have an unterminated #ifndef

What's inside defines.h ?

Edit: You seem to have found the solution. One more remark:

#include <stdlib.h>

For C++, please:

#include <cstdlib>

:D