2016-06-14 3 views
0

Ich versuche, ein SystemC-Modul zu simulieren, die zwei Pixel addiert, aber wenn ich kompilieren Sie den Code, den ich die folgende Fehlermeldung erhalten:„multiple Definition von“ Nachricht beim Versuch, eine SystemC-Datei zu simulieren

/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 

Diese Problem tritt auf, wenn ich versuche, das Modul zu trennen, das die Pixel in einem separaten Header hinzufügt. Wenn ich den gesamten Code innerhalb der gleichen Datei schreibe, läuft die Simulation ohne Probleme.

Dies ist der Code, die simuliert werden können:

Datei 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; 
} 

Datei 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 

Was würde ich gerne verstehen Deshalb kann ich den Code nicht kompilieren, wenn ich ihn aus der main Funktion und sep lösche arate es in einer anderen Header-Datei, die in der main-Funktion aufgerufen wird.

Antwort

2

Freie definierte Funktionen in einer Header-Datei Notwendigkeit, mit dem inline Schlüsselwort vorangestellt werden, wenn sie in mehreren Übersetzungseinheiten verwendet werden:

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

Die andere Option ist die Definition dieser Funktion in einer separaten Übersetzung extern Einheit.

Verwandte Themen