Static C++ Variable Apple Mach-o Linker issue

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 16:33:03

问题


I am working on an Ogre project and I am getting some issues with Xcode. Whenever I make the scene manager pointer static, the program does not compile and I get the following error:

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)

Here is my 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()
{

}

And of course the header:

#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__) */

I have seen this issue on Stackoverflow quite often, but usually it happened because some required libraries were not included. I don't think that is the issue here because I am not getting any errors if I change: static Ogre::SceneManager *sceneManager; to: Ogre::SceneManager *sceneManager;

Thanks in advance.


回答1:


As well as declaring it in your header file, you need to actually define the sceneManager in your implementation file:

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

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

OgreInit::OgreInit()
{
    ...


来源:https://stackoverflow.com/questions/12105377/static-c-variable-apple-mach-o-linker-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!