Linkage Error

Discussion in 'C Programming' started by Morris Dovey, Feb 1, 2008.

  1. Morris Dovey

    Morris Dovey Guest

    Tricky wrote:
    >
    > When I try and link together 3 object files, I get the following
    > problem:
    >
    > gcc fusionFileIo_O fusionAlgorithms.o main.o -o fusionTK.exe
    > main.o:main.c:(.rdata+0x0): multiple definition of `_BAD_IMAGE'
    > fusionFileIo_O:fusionFileIO.c:(.rdata+0x0): first defined here
    > collect2: ld returned 1 exit status
    > make: *** [fusionTK.exe] Error 1
    >
    > The problem in question comes from the following header file, which is
    > included in all of the object files:
    >
    > #define FILE_BUFFER_SIZE 256
    >
    > typedef struct
    > {
    > //image dimensions
    > int pixPerLine;
    > int numLines;
    >
    > //image data
    > char *data;
    > } image_t;
    >
    > const image_t BAD_IMAGE = {-1, -1, 0x0};
    >
    > This is the only time BAD_IMAGE is defined.
    >
    > Any thoughts on where Im going wrong?


    Each object file attempts to produce its own instance of
    BAD_IMAGE. The easy way out might be to move it from the header
    to its very own .c file (which means that you'll have four object
    files to link).

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto
    Morris Dovey, Feb 1, 2008
    #1
    1. Advertising

  2. Morris Dovey

    Tricky Guest

    When I try and link together 3 object files, I get the following
    problem:

    gcc fusionFileIo_O fusionAlgorithms.o main.o -o fusionTK.exe
    main.o:main.c:(.rdata+0x0): multiple definition of `_BAD_IMAGE'
    fusionFileIo_O:fusionFileIO.c:(.rdata+0x0): first defined here
    collect2: ld returned 1 exit status
    make: *** [fusionTK.exe] Error 1

    The problem in question comes from the following header file, which is
    included in all of the object files:

    #define FILE_BUFFER_SIZE 256

    typedef struct
    {
    //image dimensions
    int pixPerLine;
    int numLines;

    //image data
    char *data;
    } image_t;

    const image_t BAD_IMAGE = {-1, -1, 0x0};



    This is the only time BAD_IMAGE is defined.

    Any thoughts on where Im going wrong?
    Tricky, Feb 1, 2008
    #2
    1. Advertising

  3. In article <>,
    Tricky <> wrote:
    >When I try and link together 3 object files, I get the following
    >problem:


    >main.o:main.c:(.rdata+0x0): multiple definition of `_BAD_IMAGE'


    >The problem in question comes from the following header file, which is
    >included in all of the object files:


    >#define FILE_BUFFER_SIZE 256


    >typedef struct
    >{
    > //image dimensions
    > int pixPerLine;
    > int numLines;
    >
    > //image data
    > char *data;
    >} image_t;


    >const image_t BAD_IMAGE = {-1, -1, 0x0};


    >This is the only time BAD_IMAGE is defined.


    >Any thoughts on where Im going wrong?


    const does not define a constant: it indicates that the values
    are read-only (when accessed through that name.)

    You are thus attempting to define actual storage for the object
    BAD_IMAGE in each of your files that includes the header,
    and you are doing so at file scope and you are not using the
    'static' keyword so each of the definitions is a global definition.
    So you are ending up with a global definition for BAD_IMAGE in
    each of the translation units, and of course your linker is complaining.

    To resolve this, you need to give a -definition- for BAD_IMAGE in
    only one translation unit, and in each other translation unit,
    you need to

    extern const image_t BAD_IMAGE;

    to -reference- the global BAD_IMAGE without defining it in those other
    places.
    --
    "Any sufficiently advanced bug is indistinguishable from a feature."
    -- Rich Kulawiec
    Walter Roberson, Feb 1, 2008
    #3
  4. Tricky wrote:
    >
    > When I try and link together 3 object files, I get the following
    > problem:
    >
    > gcc fusionFileIo_O fusionAlgorithms.o main.o -o fusionTK.exe
    > main.o:main.c:(.rdata+0x0): multiple definition of `_BAD_IMAGE'
    > fusionFileIo_O:fusionFileIO.c:(.rdata+0x0): first defined here
    > collect2: ld returned 1 exit status
    > make: *** [fusionTK.exe] Error 1
    >
    > The problem in question comes from the following header file, which is
    > included in all of the object files:

    [...]
    > const image_t BAD_IMAGE = {-1, -1, 0x0};
    >
    > This is the only time BAD_IMAGE is defined.
    >
    > Any thoughts on where Im going wrong?


    Yes. You are defining it in every file that includes that header
    file. Every one of those object files has a global variable called
    "BAD_IMAGE", hence the "multiple definition" error. (OT: execute
    "nm" on each of those ".o" files, and you will see them.) This is
    no different than having "int i = 3;" in the header.

    I think your problem is that "const" doesn't do what you think it
    does. Perhaps it does in C++, but not in C.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Feb 1, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neno
    Replies:
    2
    Views:
    3,244
  2. jakester
    Replies:
    11
    Views:
    578
    James Kanze
    Apr 16, 2007
  3. Replies:
    1
    Views:
    590
    Michael DOUBEZ
    Sep 12, 2008
  4. omnia neo

    error C2375 - redefinition; different linkage

    omnia neo, Apr 23, 2010, in forum: C Programming
    Replies:
    6
    Views:
    2,997
    ImpalerCore
    Apr 23, 2010
  5. omnia neo
    Replies:
    3
    Views:
    1,267
    Alf P. Steinbach
    Apr 24, 2010
Loading...

Share This Page