静态C ++变量Apple Mach-o链接器问题 [英] Static C++ Variable Apple Mach-o Linker issue

查看:160
本文介绍了静态C ++变量Apple Mach-o链接器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Ogre项目,但Xcode出现了一些问题. 每当我将场景管理器指针设为静态时,程序都不会编译,并且会出现以下错误:

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)

这是我的OgreInit.cpp

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()
{

}

当然还有标题:

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

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;

谢谢.

推荐答案

以及在头文件中声明,您实际上需要定义 sceneManager在您的实施文件中:

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()
{
    ...

这篇关于静态C ++变量Apple Mach-o链接器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆