multiple definition of `xxx'

代码菜鸡一只,在写作业的时候被这个错误折磨到头秃。记录下来,留着以后备用。

在将电梯长代码拆分成项目的时候,全局变量的位置十分尴尬。

问题:1、在main.c中定义,elevaor.cpp中[Error] 'XXX' was not declared in this scope

          2、在elevator.h中定义,multiple definition of `XXX'

          3、在elevator.c中定义,multiple definition of `XXX'

=-=果然太麻烦了,简单粗暴上解决方式好了。(此处参考一众文章)

解决方法:1、对于elevator.h别忘了条件编译:

                       #ifndef ELEVATOR_H
                       #define ELEVATOR_H
                       ……
                       #endif

                  2、在main.c中定义变量,在elevator.c中定义extern int XXX,表示该变量在其他文件中定义。

猜你喜欢

转载自blog.csdn.net/qq_41516296/article/details/80279383
今日推荐