"multiple definition of" 尝试模拟 SystemC 文件时的消息

"multiple definition of" message when trying to simulate a SystemC file

我正在尝试模拟一个添加两个像素的 SystemC 模块,但是当我编译代码时,我收到以下错误消息:

/tmp/ccb1wY9C.o: In function `sc_dt::sc_uint_base::length() const':
Test/pixels.h:24: multiple definition of `sc_trace(sc_core::sc_trace_file*, pixels const&, std::string const&)'

/tmp/ccpklMFz.o:/Test/pixels.h:24: first defined here
collect2: error: ld returned 1 exit status

当我尝试将添加像素的模块分离到单独的 header 时,会发生此问题。如果我在同一个文件中编写所有代码,模拟运行没有任何问题。

这是可以模拟的代码:

文件main.cpp:

#include <systemc.h>
#include "pixels.h"
//I use this header when I try to separate the module
//#include "addpixels.h"

// This is the module I'm trying to code within a separate header    
SC_MODULE(addpixels){
    sc_in<pixels> pixel1;
    sc_in<pixels> pixel2;
    sc_out<pixels> pixelout;

    SC_CTOR(addpixels){
        SC_METHOD(addition);
        sensitive << pixel1 << pixel2;
    }

    void addition(){
    sc_uint<5> ir;
    sc_uint<6> ig;
    sc_uint<5> ib;
    pixels temp1 = pixel1.read();
    pixels temp2 = pixel2.read();
    ir = temp1.r + temp2.r;
    ig = temp1.g + temp2.g;
    ib = temp1.b + temp2.b;

    pixelout = pixels(ir, ig, ib);
    }

};

//

//Main function
int sc_main (int argc, char * argv[])
{

    sc_signal<pixels> pix1("pix1");
    sc_signal<pixels> pix2("pix2");
  sc_signal<pixels> pixout("pixout");

    addpixels addpixels_m("addpixels_m");
    addpixels_m.pixel1(pix1);    
    addpixels_m.pixel2(pix2);
    addpixels_m.pixelout(pixout);

    sc_trace_file *trace_fpixels;
    trace_fpixels = sc_create_vcd_trace_file("pixels_trace");
    trace_fpixels->set_time_unit(100, SC_NS);

    sc_trace(trace_fpixels, pix1, "pixel1");
    sc_trace(trace_fpixels, pix2, "pixel2");
    sc_trace(trace_fpixels, pixout, "pixelOut");

    int r, g, b;
    int k = 1;

    while (k < 21){
        r = rand() % (256-1);
        g = rand() % (256-1);
        b = rand() % (256-1);

        pix1 = pixels(r,g,b);

        r = rand() % (256-1);
        g = rand() % (256-1);
        b = rand() % (256-1);

        pix2 = pixels(r,g,b);

        sc_start(300,SC_NS);

        cout << "Test number " << k << endl;
        cout << "--> @ " << sc_time_stamp() << " P1 = " << pix1 << endl;
        cout << "--> @ " << sc_time_stamp() << " P2 = " << pix2 << endl;
        cout << "--> @ " << sc_time_stamp() << " Pout = " << pixout << endl;
        k++;

    }

    sc_close_vcd_trace_file(trace_fpixels);

    return 0;
}

文件pixels.h

#ifndef PIXELS_H
#define PIXELS_H

struct pixels {
    sc_uint<8> r;
    sc_uint<8> g;
    sc_uint<8> b;

    //Constructor with values by default
    pixels( sc_uint<8> _r = 0, sc_uint<8> _g = 0, sc_uint<8> _b = 0): r(_r), g(_g), b(_b) { }

    bool operator == (const pixels &other) {
        return (r == other.r) && (g == other.g) && (b == other.b);
    }

    //Displaying of this struct
    friend ostream& operator << ( ostream& o, const pixels& P ) {
        o << "{" << P.r << "," << P.g << "," << P.b << "}" ;
        return o;
    }
};

//Overloading of sc_trace function
void sc_trace( sc_trace_file* _f, const pixels& _foo, const std::string& _s ) {
   sc_trace( _f, _foo.r, _s + "_r" );
   sc_trace( _f, _foo.g, _s + "_g" );
   sc_trace( _f, _foo.b, _s + "_b" );
}

#endif

我想了解的是,为什么当我从 main 函数中删除代码并将其分离到另一个 header 文件中时无法编译代码,该文件在 main函数。

头文件中定义的自由函数如果在多个翻译单元中使用,则需要使用 inline 关键字作为前缀:

   //Overloading of sc_trace function
   inline void sc_trace( sc_trace_file* _f, const pixels& _foo, const std::string& _s ) {
// ^^^^^^
       sc_trace( _f, _foo.r, _s + "_r" );
       sc_trace( _f, _foo.g, _s + "_g" );
       sc_trace( _f, _foo.b, _s + "_b" );
    }

另一种选择是在单独的翻译单元中对该函数的定义进行外部定义。