PDA

View Full Version : Linking with Cygwin (error!)


Gundamflare
10-16-05, 05:59 PM
Hey guys,

I've got an error that I can't pin down, and it's quite upsetting that it would happen during linking, especially when I'm almost done!

Anyway, the error is


LinkedList.o:LinkedList.cpp:(.bss+0x0): multiple definition of `_DEFAULT'
LinkTest.o:LinkTest.cpp:(.bss+0x0): first defined here
LinkedList.o:LinkedList.cpp:(.bss+0x4): multiple definition of `_INIT'
LinkTest.o:LinkTest.cpp:(.bss+0x4): first defined here
LinkedList.o:LinkedList.cpp:(.data+0x0): multiple definition of `_NULLVAL'
LinkTest.o:LinkTest.cpp:(.data+0x0): first defined here
collect2: ld returned 1 exit status


DEFAULT, INIT, NULLVAL are all global definitions (as per instructor's instructions)

and the file hierarchy is something like this:

LinkedList.h <-Interface
LinkedList.cpp <-Implementation
LinkTest.cpp <-Client program / Tester

cygwin commands:

$ g++ -c LinkedList.h LinkedList.cpp LinkTest.cpp
no errors
$ g++ LinkedList.o LinkTest.o -o Linktest.exe

Get the above error.

I conditionally compiled the .h so I'm not sure why I'm getting that error! meaning i have:

#ifndef LINKEDLIST_H


#define LINKEDLIST_H

//code

#endif

Any ideas? Will post source if necesary, but would rather not want to.

pilot2000
11-20-05, 06:26 PM
i'v never programmed in c++ nor used cygwin.anyway i do know something about c.
anyway let me make some definitions first:
what is .bss -> it's the part of a file called ELF where global uninitialized data size are stored rather than the data themselves for size decrease.
what is ELF -> it's a standard file format that describes executable , relocatable .o and library files.
why multiple definition? -> when a file or files are compiled the compiler needs to know all the information about the vars , functions ... etc you use in your code. in order to keep in track of all these it uses a data structure named symbol table to keep this kind of information. so when it parses your file and finds more than once something declared it prompts you. everything must be unique .
how to solve? -> (1) try not to compile your .h .usually .h have only definitions and not implementations(that's how it's supposed top be).
(2) if you use vars in more than one file then something like int x;(file1)
int x;(file2)
is not allowed.try to declare it in one file and refer only to it in another file
e.g int x;(in file 1)
extern int x;(in other files)