在Visual Studio中運行的基本cocos2dx應用程序中獲取鏈接器錯誤。當AppDelegate的「applicationDidFinishLaunching」方法調用「MyGameScene」的「單例」方法時發生。 我已經檢查了在MyGameScene.h定義的方法MyGameScene.cpp類實現LNK2019函數中未解析的外部符號引用
錯誤味精
Error 1 error LNK2019: unresolved external symbol "public: static class
MyGame * __cdecl MyGame::singleton(void)" ([email protected]@@[email protected])
referenced in function "public: virtual bool __thiscall
AppDelegate::applicationDidFinishLaunching(void)"
([email protected]@@UAE_NXZ) D:\Dev\cocos2d-2.0-x-
2.0.4\MyGame\proj.win32\AppDelegate.obj MyGame
AppDelegate.h
#ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_
#include "CCApplication.h"
/**
@brief The cocos2d Application.
The reason for implement as private inheritance is to hide some interface call by CCDirector.
*/
class AppDelegate : private cocos2d::CCApplication
{
public:
AppDelegate();
virtual ~AppDelegate();
/**
@brief Implement CCDirector and CCScene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
virtual bool applicationDidFinishLaunching();
/**
@brief The function be called when the application enter background
@param the pointer of the application
*/
virtual void applicationDidEnterBackground();
/**
@brief The function be called when the application enter foreground
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
};
#endif // _APP_DELEGATE_H_
AppDelegate中。 cpp
#include "AppDelegate.h"
#include "cocos2d.h"
#include "MyGameScene.h"
USING_NS_CC;
AppDelegate::AppDelegate()
{
}
AppDelegate::~AppDelegate()
{
}
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0/60);
// create a scene. it's an autorelease object
MyGame *MyGame = MyGame::singleton();
CCScene *pScene = MyGame->scene();
// run
pDirector->runWithScene(pScene);
return true;
}
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
CCDirector::sharedDirector()->pause();
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
CCDirector::sharedDirector()->resume();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
MyGameScene.h
#ifndef __MYGAME_SCENE_H__
#define __MYGAME_SCENE_H__
#include "cocos2d.h"
#include "Box2D/Box2d.h"
#define PTM_RATIO 32
USING_NS_CC;
class MyGame: public cocos2d::CCLayer {
public:
cocos2d::CCSprite *_ball;
// implement the "static node()" method manually
CREATE_FUNC(MyGame)
;
static MyGame *singleton();
MyGame();
~MyGame();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommand to return the exactly class pointer
cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
void Tick(float dt);
private:
CCScene *_scene;
int mWidth;
int mHeight;
};
#endif // __MYGAME_SCENE_H__
MyGameScene.cpp
#include "MyGameScene.h"
//#include "SimpleAudioEngine.h"
#include "Shaders.h"
using namespace cocos2d;
//using namespace CocosDenshion;
#define COCOS2D_DEBUG 1
extern "C" {
#include <pthread.h>
#include <unistd.h>
}
static MyGame *_MyGameSingleton = NULL;
static bool mIsNewFrameReceived;
MyGame* MyGame::singleton() {
// 'layer' is an autorelease object
if (_MyGameSingleton == NULL) {
_MyGameSingleton = MyGame::create();
}
return _MyGameSingleton;
}
CCScene* MyGame::scene() {
if (!_scene) {
// 'scene' is an autorelease object
_scene = CCScene::create();
// add layer as a child to scene
_scene->addChild(this);
}
// return the scene
return _scene;
}
// on "init" you need to initialize your instance
bool MyGame::init() {
_scene = NULL;
if (!CCLayer::init()) {
return false;
}
// CCLOG("init");
// CCSize winSize = CCDirector::sharedDirector()->getWinSize();
// mWidth = winSize.width;
// mHeight = winSize.height;
this->schedule(schedule_selector(MyGame::Tick));
return true;
}
void MyGame::menuCloseCallback(CCObject* pSender) {
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
MyGame::~MyGame() {
}
MyGame::MyGame() {
}
void MyGame::Tick(float dt) {
CCDirector *director = CCDirector::sharedDirector();
CCSize windowSize = director->getVisibleSize();
mWidth = windowSize.width;
mHeight = windowSize.height;
}
UPDATE 我在Visual Studio本身的項目中創建一個新的類,並複製所有的變量以及MyGame類的成員函數。然後,我能夠引用新的類並正確編譯。
[複製我的評論在這裏FYI] 我能夠使用cygwin在Windows上編譯現有的cocos2dx遊戲,我的同事能夠使用XCode在Mac上編譯相同的東西。這個問題只在使用Visual Studio進行編譯時纔有意義
我認爲Visual Studio沒有編譯MyGame文件。我怎樣才能確保課程將被編譯?
你的'AppDelegate'和'MyGame'類包含在同一個程序集中嗎?如果沒有,那麼它可能是一個鏈接器問題.. – 2013-03-20 09:56:53
爲什麼你在AppDelegate.cpp中包含「cocos2d.h」,當它已經包含在MyGameScene.h中時。這可能會導致重定義問題。 – 2013-03-20 10:00:32
它們位於文件系統的相同文件夾中。在VS項目中,「AppDelegate」位於「include」和「source」分支中,而「MyGame」位於「外部依賴項」分支 – asloob 2013-03-20 10:01:56