C++ static const multiple declaration error in Eclipse for Android NDK

风流意气都作罢 提交于 2019-12-13 17:54:20

问题


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.


回答1:


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.




回答2:


In your case, the names doesn't match. You are declaring as gravity and in cpp it's GRAVITY.

Edit: After your edit, I see no linking errors in your code unless you have defined GRAVITY in your Ball.cpp/h file also.



来源:https://stackoverflow.com/questions/6136536/c-static-const-multiple-declaration-error-in-eclipse-for-android-ndk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!