2016-06-14 3 views
0

をシミュレートしようとしたとき、私は二つの画素を追加した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関数とsepそれをmain関数で呼び出される別のヘッダファイルで生成します。

答えて

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"); 
    } 

他のオプションは、別の翻訳でその関数の定義をexternにあります単位。

関連する問題