【问题标题】:Cocos2D-X compile Linux Error: Undefined ReferenceCocos2D-X 编译 Linux 报错:未定义参考
【发布时间】:2015-05-24 15:31:36
【问题描述】:

我是 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
}

还有什么需要补充的吗?

【问题讨论】:

  • 我也遇到了同样的问题。找到解决方案了吗?

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


【解决方案1】:

我今天也遇到了这个问题,通过编辑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}
)

希望它有效!

【讨论】:

  • 是否会更新所有子项目,即 win32、android、android-studio 等
  • 在我的情况下,我只测试了 linux 和 android 子项目,并且都有效。
  • 所以在用我的新源文件更新这个 CMAKE 文件后,我需要运行 cocos compile -p linux 还是运行 CMAKE 命令
  • 据我记得,你只需要编译它(但我在过去一年半的时间里没有使用 Cocos2d-x,所以我不是 100% 确定。抱歉关于那个)
【解决方案2】:

我猜链接器没有找到 GraphicsScene::createScene() 的 .cpp 源文件。您应该将 GraphicsScene.cpp 添加到 Android.mk 文件中。比如我在 windows 上为我的项目做的。

LOCAL_SRC_FILES := hellocpp/main.cpp \

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

希望对您有所帮助。 谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 2023-03-13
    • 2022-10-15
    • 2015-06-25
    • 1970-01-01
    • 2012-08-06
    相关资源
    最近更新 更多