使用 GNuplot 的多重定义错误
multiple definition error using gnuplot
我在stackoverflow上读了很多关于链接错误的信息,但我不知道我的问题在哪里。
主.cpp
#include "traffic.h"
using namespace std;
int main(){
traffic t();
...
}
交通.h
#ifndef _TRAFFIC
#define _TRAFFIC
#include "gnuplot.h"
using namespace std;
class traffic{
public:
int step;
Gnuplot gpl;
...
#endif
在我制作了.o文件之后,我想将它们全部链接起来。出现以下错误。
壳
g++ -o tr traffic.o main.o
main.o:(.bss+0x0): multiple definition of `Gnuplot::tmpfile_num'
traffic.o:(.bss+0x0): first defined here
以及更多关于 Gnuplot 的多重定义错误。
gnuplot 仅用于流量.cpp
我的问题是:我的错误在哪里?顺便说一句:信息太少了,无法解决问题吗?
这是
在任何 cpp 中都不可用,但在 one直接来自 gnuplot.h 标头
//------------------------------------------------------------------------------
//
// initialize static data
//
int Gnuplot::tmpfile_num = 0;
在头文件中初始化静态似乎是一种不好的做法,而它们应该只在 cpp 文件中完成以避免重复定义。
在您的情况下,您可以通过不在任何头文件中包含 gnuplot.h 标头来修复它,而仅在 cpp 文件中。这将需要通过包装器包装对 gnuplot 标头函数的所有调用,因为它
在 cpp 文件中使用 #include "gnuplot.h"
而不是header
文件意味着.h
文件然后您的文件将被编译
相关文章:
- 使用命名空间时出现多个定义错误
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 链接阶段出现多重定义错误
- 避免模板类中的重定义错误
- 即使我没有包含多个文件,C++中的多个定义错误
- 跨多个类的全局变量而不会出现重定义错误?
- 尝试运行 wasm 函数时出现模块未定义错误
- C++ 预期的左大括号以及重定义错误
- C++:成员的越界声明必须是纯虚函数的定义错误
- c++中数组的未定义错误
- Visual C:模板类中的自定义错误消息
- 为什么C++模板不会导致多个定义错误?
- 只有一个定义/声明时标头声明变量的多堆定义错误
- C++ 在多个其他类中使用单个类 - 编译时出现多个定义错误
- 基类未定义.错误 C2504
- Q 斯坦达项重定义错误
- 可视化C++中的结构定义错误
- VC++ C2011 重定义错误 - 未使用的头文件