【问题标题】:Clion compiler error during the builduing process构建过程中的 Clion 编译器错误
【发布时间】:2017-03-18 07:28:28
【问题描述】:

我试图创建一个简单的类 program,但我收到了一个奇怪的错误,据我所知,这不是代码的问题,而是 Clion 的问题。

CMakeFiles\Assignment_4.dir/objects.a(Box.cpp.obj):Box.cpp:(.bss+0x0): multiple definition of `Box::objectCount'
CMakeFiles\Assignment_4.dir/objects.a(Q_1.cpp.obj):Q_1.cpp:(.bss+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Assignment_4.exe] Error 1
CMakeFiles\Assignment_4.dir\build.make:147: recipe for target 'Assignment_4.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Assignment_4.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/Assignment_4.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Assignment_4.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Assignment_4.dir/rule' failed
mingw32-make.exe: *** [Assignment_4] Error 2
Makefile:117: recipe for target 'Assignment_4' failed

即使直接使用 g++ 编译时,也会出现多对象定义错误。它以前在视觉工作室工作过。

C:\Users\fsa\CLionProjects\McMaster\Assignment_4>g++ Q_1.cpp clock.cpp box.cpp -o Q1.exe
C:\Users\fsa\AppData\Local\Temp\ccmD1R6t.o:box.cpp:(.bss+0x0): multiple definition of `Box::objectCount'
C:\Users\fsa\AppData\Local\Temp\cc0nh7LU.o:Q_1.cpp:(.bss+0x0): first defined here
collect2.exe: error: ld returned 1 exit status

【问题讨论】:

  • 我使用错误的命令进行编译。应该是g++ Q_1.cpp clock.cpp box.cpp -o Q1.exe
  • 那么,现在编译成功了吗?
  • @GigaRohan 不,但它确实改变了错误输出。在我的类文件根本没有编译之前。现在我有multiple definition of Box::objectCount'` 错误。我通过代码查看原因。

标签: c++ class g++ mingw clion


【解决方案1】:

问题是int Box::objectCount = 0; 在标题中。所以它出现在多个编译单元(Q1.o 和 Box.o)中。它应该被移动到 Box.cpp。

【讨论】:

  • 谢谢,解决了。但是,线的位置是否会影响计数器功能,因为即使在 main() 中创建了 2 个框对象,我的计数器仍保持为 0。
  • 我认为这并不重要,但通常它会放在 CPP 文件中包含的标题下方。如果您仍然有问题,我会说使用调试器逐步检查您的代码以查看发生了什么。附注:objectCount 在您的情况下应该是私有的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 2013-06-10
  • 2013-06-18
  • 1970-01-01
  • 2014-02-02
  • 2015-07-05
  • 1970-01-01
相关资源
最近更新 更多