C++ Android NDK Eclipse 中的 static const 多重声明错误
我读过类似的问题,但回答我的问题仅适用于 VisualStudio。我正在使用 Eclipse 并使用 Cocos2d-X 开发 Android 应用程序,Cocos2d-X 是一个使用 Android NDK 的框架。我创建了一个名为 Config 的类,其中包含应用程序的所有常量,例如球大小和 fps。下面是我如何安排代码。
Config.h
#ifndef __CONFIG_H_ // this was auto-generated by eclipse
#define __CONFIG_H_
class Config {
public:
static const double GRAVITY;
static const int BALL_WIDTH;
static const int BALL_HEIGHT;
}
#endif /* config.h */
Config.cpp
#include "Config.h"
const double Config::GRAVITY = 9.8;
const int Config::BALL_WIDTH = 100;
const int Config::BALL_HEIGHT = 100;
它编译时没有错误,但是当它开始链接时,我收到以下错误:
multiple definition of `Config::GRAVITY'
C:/workspace/cocos2d-x/SampleGame/android/obj/local/armeabi/objs-debug/game/../../../Classes/Config.o:(.rodata+0xc8): first defined here
C:/workspace/cocos2d-x/SampleGame/android/obj/local/armeabi/objs-debug/game/../../../Classes/Ball.o:(.rodata+0xcc):`
所有声明的常量都会发生先前的错误。我没有将 Config.cpp 包含在任何报告的源文件的源代码中。
我不知道如何纠正这个问题。我发现了一个极其相似的问题,但答案是针对微软的VisualStudio指定的。另外,我很抱歉使用“cocos2d”标签,即使这适用于 cocos2d-X,但我希望有人知道如何解决这个问题。
I've read the similar questions, but the one that answers mine applies only to VisualStudio. I am using Eclipse and developing an Android application using Cocos2d-X, which is a framework that uses Android's NDK. I created a class named Config, which contains all of the application's constants such as ball sizes and fps. Below is how I arranged the code.
Config.h
#ifndef __CONFIG_H_ // this was auto-generated by eclipse
#define __CONFIG_H_
class Config {
public:
static const double GRAVITY;
static const int BALL_WIDTH;
static const int BALL_HEIGHT;
}
#endif /* config.h */
Config.cpp
#include "Config.h"
const double Config::GRAVITY = 9.8;
const int Config::BALL_WIDTH = 100;
const int Config::BALL_HEIGHT = 100;
It compiles without errors, but when it begins linking, I get the following error:
multiple definition of `Config::GRAVITY'
C:/workspace/cocos2d-x/SampleGame/android/obj/local/armeabi/objs-debug/game/../../../Classes/Config.o:(.rodata+0xc8): first defined here
C:/workspace/cocos2d-x/SampleGame/android/obj/local/armeabi/objs-debug/game/../../../Classes/Ball.o:(.rodata+0xcc):`
The previous error occurs for all the constants declared. I have not included Config.cpp in the source code of any of the reported source files.
I have no idea how to correct this. I found an extremely similar question, but the answer was specified towards Microsoft's VisualStudio. Also, I'm sorry for using the 'cocos2d' tag, even if this applies to cocos2d-X, but I'm hoping someone knows how to fix this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生该错误的唯一原因是您包含了 .cpp 文件。否则,您的代码完全符合标准。毕竟,该错误意味着该常量是在
Ball.o
中定义的,除非您包含 cpp,否则我发现这种情况不太可能发生。The only way that error could occur is if you're including the .cpp file around. Else, your code is perfectly Standards-compliant. After all, the error implies that the constant was defined in
Ball.o
, which I find very unlikely unless you included the cpp.就您而言,名称不匹配。您声明为
gravity
,在 cpp 中它是GRAVITY
。编辑:编辑后,我发现代码中没有链接错误,除非您也在
Ball.cpp/h
文件中定义了GRAVITY
。In your case, the names doesn't match. You are declaring as
gravity
and in cpp it'sGRAVITY
.Edit: After your edit, I see no linking errors in your code unless you have defined
GRAVITY
in yourBall.cpp/h
file also.