Cocos2D-X编译Linux错误:未定义的参考

时间:2015-05-24 15:31:36

标签: c++ linux cocos2d-x-3.0

我是cocos2D-X(v 3.6)的新手,我正在使用Linux来编译和运行我的项目。当我尝试以下命令行时:

cocos compile -p linux

它给了我错误:

Linking CXX executable bin/MyGame
CMakeFiles/MyGame.dir/Classes/AppDelegate.cpp.o: In function `AppDelegate::applicationDidFinishLaunching()':
/home/caisar/MyCompany/MyGame/Classes/AppDelegate.cpp:53: undefined reference to `GraphicsScene::createScene()'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/MyGame] Error 1
make[1]: *** [CMakeFiles/MyGame.dir/all] Error 2
make: *** [all] Error 2
Error running command, return code: 2

这是我的GraphicsScene.h

#ifndef __GRAPHICS_SCENE_H__
#define __GRAPHICS_SCENE_H__
#include "cocos2d.h"

class GraphicsScene : public cocos2d::Layer{
public:
    static cocos2d::Scene* createScene();
    virtual bool init();
    void menuCloseCallback(cocos2d::Ref* pSender);
    CREATE_FUNC(GraphicsScene);
};

#endif

这是我的GraphicsScene.cpp

#include "GraphicsScene.h"

USING_NS_CC;

Scene* GraphicsScene::createScene(){
    auto scene = Scene::create();
    auto layer = GraphicsScene::create();
    scene->addChild(layer);

    return scene;
}

bool GraphicsScene::init(){
    if(!Layer::init()){
        return false;
    }

    auto sprite = Sprite::create("autobot.png");
    sprite->setPosition(0,0);
    this->addChild(sprite, 0);

    return true;
}

void GraphicsScene::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

我还需要添加其他内容吗?

2 个答案:

答案 0 :(得分:3)

我今天也遇到了这个问题,并通过编辑PROJECT_PATH/CMakeLists.txt文件解决了这个问题。您必须在GAME_SRCGAME_HEADERS部分中包含* .cpp和* .h文件路径。例如,在您的情况下,它必须是这样的:

set(GAME_SRC
  Classes/AppDelegate.cpp
  Classes/HelloWorld.cpp
  Classes/GraphicsScene.cpp
  ${PLATFORM_SPECIFIC_SRC}
)

set(GAME_HEADERS
  Classes/AppDelegate.h
  Classes/HelloWorld.h
  Classes/GraphicsScene.h
  ${PLATFORM_SPECIFIC_HEADERS}
)

希望它有效!

答案 1 :(得分:2)

我猜连接器没有找到GraphicsScene :: createScene()的.cpp源文件。你应该将GraphicsScene.cpp添加到Android.mk文件中。比如我为我的Windows项目做了那个。

LOCAL_SRC_FILES:= hellocpp / main.cpp \

              ../../Classes/Graphic_Image.cpp \

希望有所帮助。 感谢。