使用 GNuplot 的多重定义错误

multiple definition error using gnuplot

本文关键字:定义 错误 GNuplot 使用      更新时间:2023-10-16

我在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

我的

问题是:我的错误在哪里?顺便说一句:信息太少了,无法解决问题吗?

这是

直接来自 gnuplot.h 标头

//------------------------------------------------------------------------------
//
// initialize static data
//
int Gnuplot::tmpfile_num = 0;

头文件中初始化静态似乎是一种不好的做法,而它们应该只在 cpp 文件中完成以避免重复定义。

在您的情况下,您可以通过不在任何头文件中包含 gnuplot.h 标头来修复它,而仅在 cpp 文件中。这将需要通过包装器包装对 gnuplot 标头函数的所有调用,因为它

在任何 cpp 中都不可用,但在 one
而不是header文件意味着.h文件然后您的文件将被编译