:-1: error: main.o:(.bss+0x10): multiple definition of `MySpdlog::myLogger2'; csocketfactory.o:(.bss+0x10): first defined here
时间: 2024-02-03 13:02:29 浏览: 155
这个错误和之前的错误类似,也是由于在两个文件中都定义了名为 `MySpdlog::myLogger2` 的变量,导致了重复定义。解决方法也是将其中一个文件中的变量声明为 `extern`。
例如,在 `main.cpp` 中定义变量时,可以这样写:
```cpp
#include "myspdlog.h"
// 定义变量
MySpdlog::Logger MySpdlog::myLogger2("logger2");
// 声明变量
extern MySpdlog::Logger myLogger2;
```
然后在其他文件中使用 `myLogger2` 变量时,也需要使用 `extern` 声明:
```cpp
// 声明变量
extern MySpdlog::Logger myLogger2;
// 使用变量
myLogger2.info("log message"); // 注意,这里不需要再次定义变量
```
这样就可以解决重复定义的问题了。
相关问题
但是像你这样写会报错:-1: error: socketimpl.o:(.bss+0x0): multiple definition of `MySpdlog::myLogger1'; csocketfactory.o:(.bss+0x0): first defined here
这个错误是由于在两个文件中都定义了名为 `MySpdlog::myLogger1` 的变量,导致了重复定义。解决方法是将其中一个文件中的变量声明为 `extern`。
例如,在 `socketimpl.cpp` 中定义变量时,可以这样写:
```cpp
#include "myspdlog.h"
// 定义变量
MySpdlog::Logger MySpdlog::myLogger1("logger1");
// 声明变量
extern MySpdlog::Logger myLogger1;
```
然后在其他文件中使用 `myLogger1` 变量时,也需要使用 `extern` 声明:
```cpp
// 声明变量
extern MySpdlog::Logger myLogger1;
// 使用变量
myLogger1.info("log message"); // 注意,这里不需要再次定义变量
```
这样就可以解决重复定义的问题了。
/usr/bin/ld: CMakeFiles/AVM_HIKVISION.dir/src/captures.cpp.o:(.bss+0x0): multiple definition of `last'; CMakeFiles/AVM_HIKVISION.dir/src/main.cpp.o:(.bss+0x0): first defined here /usr/bin/ld: CMakeFiles/AVM_HIKVISION.dir/include/AVM/birdview.cpp.o:(.bss+0x0): multiple definition of `last'; CMakeFiles/AVM_HIKVISION.dir/src/main.cpp.o:(.bss+0x0): first defined here /usr/bin/ld: CMakeFiles/AVM_HIKVISION.dir/src/mediawall.cpp.o:(.bss+0x0): multiple definition of `last'; CMakeFiles/AVM_HIKVISION.dir/src/main.cpp.o:(.bss+0x0): first defined here
这个错误提示说明了一个多重定义的问题。在你的代码中,变量 `last` 被多次定义了。
根据错误提示,`last` 在多个文件中被定义了。它在 `CMakeFiles/AVM_HIKVISION.dir/src/main.cpp.o`、`CMakeFiles/AVM_HIKVISION.dir/include/AVM/birdview.cpp.o` 和 `CMakeFiles/AVM_HIKVISION.dir/src/mediawall.cpp.o` 中都有定义。
要解决这个问题,你可以尝试以下几种方法之一:
1. 检查代码逻辑并删除其中一个 `last` 的定义,确保只有一个定义存在。
2. 如果 `last` 是全局变量,可以将其放在一个单独的源文件中(例如 `last.cpp`),并将其声明为 `extern`。然后,在需要使用 `last` 的其他文件中,包含 `last.h` 头文件,并在使用前进行声明 `extern int last;`。这样可以确保只有一个定义,并在需要使用时进行引用。
3. 如果 `last` 是局部变量,可以检查是否在某个范围内意外地重复定义了。确保每个函数或作用域内的变量命名唯一。
请根据你的需求选择适合的解决方案,并检查代码中是否还有其他地方存在类似的多重定义问题。
希望能帮到你!如果还有其他问题,请随时提问。
阅读全文
相关推荐














