Cocos2D-X compile Linux Error: Undefined Reference
Cocos2D-X compile Linux Error: Undefined Reference
我是 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
}
还有什么要补充的吗?
我猜链接器没有找到 GraphicsScene::createScene() 的 .cpp 源文件。您应该将 GraphicsScene.cpp 添加到 Android.mk 文件。比如我在 windows.
上为我的项目所做的
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/Graphic_Image.cpp \
希望对您有所帮助。
谢谢
我今天也遇到了这个问题,通过编辑PROJECT_PATH/CMakeLists.txt
文件解决了。您必须在 GAME_SRC
和 GAME_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}
)
希望有用!
我是 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
}
还有什么要补充的吗?
我猜链接器没有找到 GraphicsScene::createScene() 的 .cpp 源文件。您应该将 GraphicsScene.cpp 添加到 Android.mk 文件。比如我在 windows.
上为我的项目所做的LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/Graphic_Image.cpp \
希望对您有所帮助。 谢谢
我今天也遇到了这个问题,通过编辑PROJECT_PATH/CMakeLists.txt
文件解决了。您必须在 GAME_SRC
和 GAME_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}
)
希望有用!