""的多重定义尝试模拟SystemC文件时出现错误消息

时间:2016-06-14 18:00:46

标签: c++ module overloading systemc

我试图模拟添加两个像素的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

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

这是可以模拟的代码:

文件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函数中删除代码并将其与main函数中调用的另一个头文件分开时,我无法编译代码。

1 个答案:

答案 0 :(得分:2)

如果在多个翻译单元中使用了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" ); } 关键字作为前缀:

$( "#myFieldId" ).click(function() {
            alert( "Handler for .click() called." );
        });

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