C++ LNK2019函数中未解析的外部符号引用
在visual studio中运行的基本cocos2dx应用程序中获取链接器错误。当AppDelegate的“ApplicationIDFinishLaunching”方法调用“MyGameSecene”的“singleton”方法时发生。 我已经检查了MyGameSecene.h中定义的方法是否在MyGameSecene.cpp类中实现 错误消息C++ LNK2019函数中未解析的外部符号引用,c++,visual-studio,linker-errors,cocos2d-x,lnk2019,C++,Visual Studio,Linker Errors,Cocos2d X,Lnk2019,在visual studio中运行的基本cocos2dx应用程序中获取链接器错误。当AppDelegate的“ApplicationIDFinishLaunching”方法调用“MyGameSecene”的“singleton”方法时发生。 我已经检查了MyGameSecene.h中定义的方法是否在MyGameSecene.cpp类中实现 错误消息 Error 1 error LNK2019: unresolved external symbol "public: static clas
Error 1 error LNK2019: unresolved external symbol "public: static class
MyGame * __cdecl MyGame::singleton(void)" (?singleton@MyGame@@SAPAV1@XZ)
referenced in function "public: virtual bool __thiscall
AppDelegate::applicationDidFinishLaunching(void)"
(?applicationDidFinishLaunching@AppDelegate@@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_
#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__
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();
}
#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;
}
MyGameScene.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_
#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 "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();
}
#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;
}
#包括“MyGameSecene.h”
//#包括“SimpleAudioEngine.h”
#包括“Shaders.h”
使用名称空间cocos2d;
//使用名称空间coosdenshion;
#定义COCOS2D_调试1
外部“C”{
#包括
#包括
}
静态MyGame*_MyGameSingleton=NULL;
静态布尔错误接收;
MyGame*MyGame::singleton(){
//“层”是一个自动释放对象
if(_mygamessingleton==NULL){
_MyGameSingleton=MyGame::create();
}
return_MyGameSingleton;
}
CCScene*MyGame::scene(){
如果(!\u场景){
//“场景”是一个自动释放对象
_scene=CCScene::create();
//将层作为子层添加到场景中
_场景->添加子对象(此);
}
//返回现场
返回现场;
}
//在“init”中,您需要初始化实例
bool MyGame::init(){
_场景=空;
如果(!CCLayer::init()){
返回false;
}
//CCLOG(“初始”);
//CCSize winSize=CCDirector::sharedDirector()->getWinSize();
//mWidth=winSize.width;
//mHeight=winSize.height;
这个->时间表(时间表选择器(MyGame::Tick));
返回true;
}
void MyGame::menuCloseCallback(cObject*pSender){
CCDirector::sharedDirector()->end();
#如果(CC_目标_平台==CC_平台_IOS)
出口(0);
#恩迪夫
}
MyGame::~MyGame(){
}
MyGame::MyGame(){
}
void MyGame::勾选(float dt){
CCDirector*director=CCDirector::sharedDirector();
CCSize windowSize=director->getVisibleSize();
mWidth=windowSize.width;
mHeight=windowSize.height;
}
更新
我在VisualStudio的项目中创建了一个新类,并将MyGame类的所有变量和成员函数复制到该类中。然后,我能够引用新类并正确编译
[在此复制我的评论供参考]
我可以使用cygwin在windows上编译现有的cocos2dx游戏,我的同事也可以使用XCode在Mac上编译同样的东西。问题只出现在使用Visual Studio编译时
我认为VisualStudio没有编译MyGame文件。如何确保将编译该类?是否尝试在.cpp文件顶部设置静态MyGame变量
static MyGame *singleton();
如果未在任何位置设置,则生成此错误 我想这是个打字错误。错误消息确实包含足够的信息。“公共:静态------class---- 我的游戏* 应该是
MyGame *myGame
在AppdDeleate.cpp中,我发现在从模板创建项目后,我没有正确链接cocos2dx库。这就是链接器错误的原因[尽管错误为我指明了不同的方向]
谢谢大家的帮助
我计划列出我稍后使用的正确步骤,以便在现有的cocos2dx项目中在Visual Studio 2012中创建win32项目,以供访问此问题时遇到类似问题的任何人使用。您的
AppDelegate
和MyGame
类是否包含在同一程序集中?如果没有,那可能是链接器的问题..为什么在AppDelegate.cpp中包含“cocos2d.h”,而它已经包含在MyGameSecene.h中。。这可能会导致重新定义问题。它们位于文件系统上的同一文件夹中。在VS项目中,“AppDelegate”在“包含”和“源”分支中,而“MyGame”在“外部依赖”分支中。通过外部依赖,您的意思是MyGame是一个外部库(Lib/DLL)?否。在“其他包含目录”中,我确实遇到了与您相同的问题。试着阅读