1

我正在做一个 Ogre 项目,我遇到了 Xcode 的一些问题。每当我将场景管理器指针设为静态时,程序都无法编译,并且出现以下错误:

Undefined symbols for architecture x86_64:
  "OgreInit::sceneManager", referenced from:
      OgreInit::initOgre() in OgreInit.o
      OgreInit::initScene() in OgreInit.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是我的 OgreInit.cpp

#include <iostream>
#include <exception>
#include <string>

#include "OGRE/Ogre.h"
#include "OGRE/OgreException.h"
#include "OGRE/OgreRoot.h"
#include "OGRE/OgreResourceManager.h"
#include "OGRE/OgreMath.h"

#include "MainLoop.h"
#include "WorkingDirectory.h"
#include "OgreInit.h"

#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
#include "macUtils.h"
#endif

OgreInit::OgreInit()
{
    mainLoop = new MainLoop();
    mainLoop->startLoop();
    initOgre();
    initScene();
}

void OgreInit::initOgre()
{
    root = new Ogre::Root(WorkingDirectory::getResourcesDirectory() + "plugins.cfg", WorkingDirectory::getResourcesDirectory() + "window.cfg", "");
    root->showConfigDialog();

    window = root->initialise(true);
    sceneManager = root->createSceneManager(Ogre::ST_GENERIC);

    camera = sceneManager->createCamera("mainCamera");
    camera->setNearClipDistance(0.1);
    camera->setFarClipDistance(300);
    camera->setPosition(0, 0, 80);
    //camera->lookAt(Ogre::Vector3::ZERO);

    cameraNode = sceneManager->getRootSceneNode()->createChildSceneNode();
    cameraNode->attachObject(camera);

    viewport = window->addViewport(camera);
    viewport->setClearEveryFrame(true);
    viewport->setAutoUpdated(true);
    viewport->setBackgroundColour(Ogre::ColourValue(1, 0, 1));

    Ogre::ResourceGroupManager::getSingleton().addResourceLocation(WorkingDirectory::getModelDirectory(), "FileSystem");
    Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}

void OgreInit::initScene()
{
    sceneManager->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));

    Ogre::Entity *ogreHead = sceneManager->createEntity("Head", "ogrehead.mesh");

    Ogre::SceneNode *headNode = sceneManager->getRootSceneNode()->createChildSceneNode("HeadNode");
    headNode->attachObject(ogreHead);
    headNode->rotate(*new Ogre::Vector3(0, 1, 0), (Ogre::Radian)Ogre::Math::DegreesToRadians(90));

    Ogre::Light *light = sceneManager->createLight("MainLight");
    light->setPosition(20.0f, 80.0f, 50.0f);
}

//init resources before scene :! and translate before attaching

OgreInit::~OgreInit()
{

}

当然还有标题:

#ifndef __OgreTest__OgreInit__
#define __OgreTest__OgreInit__

#include <iostream>
#include <string>
#include <memory>

#include "OGRE/Ogre.h"
#include "MainLoop.h"

class OgreInit
{
public:
    OgreInit();
    ~OgreInit();
private:
    void initOgre();
    void initScene();
    MainLoop *mainLoop;
    Ogre::Root *root;
    Ogre::RenderWindow *window;
    static Ogre::SceneManager *sceneManager;
    Ogre::Viewport *viewport;
    Ogre::Camera *camera;
    Ogre::SceneNode *cameraNode;
};

#endif /* defined(__OgreTest__OgreInit__) */

我经常在 Stackoverflow 上看到这个问题,但通常是因为没有包含一些必需的库。我不认为这是这里的问题,因为如果我将:更改static Ogre::SceneManager *sceneManager;为:Ogre::SceneManager *sceneManager;

提前致谢。

4

1 回答 1

2

除了在头文件中声明它之外,您还需要在实现文件中实际定义sceneManager

...
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
#include "macUtils.h"
#endif

Ogre::SceneManager *OgreInit::sceneManager = 0;

OgreInit::OgreInit()
{
    ...
于 2012-08-24T08:07:13.340 回答