LNK2019在功能中未解决的外部符号引用
LNK2019 unresolved external symbol reference in function
在visual studio中运行的基本cocos2dx应用程序中出现链接器错误。当AppDelegate的"applicationDidFinishLaunching"方法调用"MyGameScene"的"singleton"方法时发生。我已经检查了MyGameScene.h中定义的方法在MyGameScene.cpp类
中实现错误味精
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:Devcocos2d-2.0-x-
2.0.4MyGameproj.win32AppDelegate.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;
}
我在Visual Studio本身的项目中创建了一个新类,并将MyGame类的所有变量和成员函数复制到其中。然后,我就可以引用新类并正确编译了。
[复制我的评论供参考]我能够使用cygwin在windows上编译现有的cocos2dx游戏,我的同事能够使用XCode在Mac上编译相同的东西。问题只在用Visual Studio
编译时出现我认为Visual Studio没有编译MyGame文件。我如何确保类将被编译?
您是否尝试在.cpp文件的顶部设置静态MyGame变量?
static MyGame *singleton();
如果没有设置,则产生此错误。
我猜是打错了。错误消息包含了足够的信息。"public: static ----- class
----MyGame * __cdecl…"
MyGame *MyGame
应该MyGame *myGame
在AppdDeleate.cpp 我发现在从模板创建项目后,我没有正确地链接cocos2dx库。这就是链接器错误的原因(尽管错误指向了不同的方向)。
谢谢大家的帮助!
我计划列出正确的步骤,我后来使用,在现有的cocos2dx项目中创建Visual Studio 2012中的win32项目,对于任何有类似问题访问此问题的人。
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 未解析的外部符号_MsiLocateComponentW@12.
- 在C++中使用 gRPC 时未解析的外部符号
- LibPrivoxy: 未解析的外部符号 __declspec(dllimport) int __stdcall Sta
- 尝试从 XamlApplication 编译 C++/WinRT 空白应用程序时,我收到未解析的外部符号 winrt_make_*
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 仅在少数方法(静态或共享库)中解析的外部符号
- 未解析的外部符号"__declspec(dllimport)与 Spdlog c++ 库
- 函数 _main (OPENGL C++) 中引用的未解析的外部符号 ________
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 使用静态 constexpr 成员的未解析外部符号
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- Dlib LNK2001未解析的外部符号USER_ERROR__consistent_build_configurati
- 矛盾的未解决的外部符号+未使用的库与VS2017和FFMPEG 4
- 链接器错误:切换到unicode生成给出:未解析的外部符号WinMain
- 类模板上一个特定函数的未解析外部符号
- LNK2001:未解析的外部符号public:static类std::vector