Solved

multiple definition of a symbol.

Posted on 2002-07-12
11
1,482 Views
Last Modified: 2010-08-05
Im compiling a program in Linux.  The program contains.....
  2 .cpp files (file1.cpp, main.cpp)
  2 .h files (file1.h, ftplib.h)
  1 library (libftp)

The two cpp files includes the header file file1.h.  This header file includes the ftplib.h file.  The ftplib.h contains a code snippet like this...

#ifdef __cplusplus
extern "C" {
#endif

typedef struct NetBuf netbuf;extern "C" {
#endif

typedef struct NetBuf netbuf;
typedef int (*FtpCallback)(netbuf *nControl, int xfered, void *arg);

/* v1 compatibility stuff */
#if !defined(_FTPLIB_NO_COMPAT)
netbuf *DefaultNetbuf;
...

Now, the problem is that when I compile my code against this library the linker compains with the error...
> multiple definition of `DefaultNetbuf'

I used the the 'nm' command and found out that both of files contain this in the listing...

00000000 B DefaultNetbuf

Does anyone know why I can't link the files?
It seems that "DefaultNetbuf" becomes part of both of my object files and creates a multiple definition.

This is the line in my makefile that links the two object files and the library...

g++ -o myprogram file1.o main.o -L. -lftp

Thanks for the help,
-DM
0
Comment
Question by:dmaroff
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7150686
You will have to make sure that this pointer appears ax "extern" to the other file:

#ifdef MAIN_CPP
extern netbuf *DefaultNetbuf = NULL; // the file that "sees" this will hold the symbol
#else
extern netbuf *DefaultNetbuf;
#endif

and add

#define MAIN_CPP

to main.cpp before including the header.
0
 

Expert Comment

by:desktop2
ID: 7150760
The two cpp files includes the header file file1.h.  This header file includes the ftplib.h file.  The ftplib.h contains a code snippet like this...


It means that compiler assumes that your header file included twice in to code, so all declaration are made twice.
To avoid this you should add 3 lines (2 at top and 1 at bottom) to each of your headers like this:
(Example for file ftplib.h)


#if !defined(FTPLIB_H)
#define FTPLIB_H
here is your original ftplib.h file content
#endif//include file only once
0
 

Author Comment

by:dmaroff
ID: 7151037
Ok, I will try on Monday and let you guys know.

Thanks,
-DM
0
 
LVL 22

Accepted Solution

by:
ambience earned 300 total points
ID: 7153379
i suggest that instead of an #ifdef you change the line in ftplib.h to

extern netbuf *DefaultNetbuf;

and then in either main or file1 you add a global like after inclusion of headers.

netbuf *DefaultNetbuf = 0;

whereever you feel appropriate.
0
 

Author Comment

by:dmaroff
ID: 7154501
Well all of you were close but this was the most direct answer.  Thanks for the help everyone.  All I had to do was put one keyword in front and it fixed it.

One more quick question...

Why is it that when I now make that line an "extern" in the .h file and then put ...

extern netbuf *DefaultNetbuf;

in both .cpp files, I dont get a "multiple definition" error now?  Wouldn't both object files contain the symbol?  I even did an nm command on the object files and did not find any trace of DefaultNetbuf.

Thanks,
-DM
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 22

Expert Comment

by:ambience
ID: 7156096
extern declares a reference to a DefaultNetBuf defined elsewhere, this in effect makes it visible to the current tranlsation unit, so it is ok to extern it multiple times across many files, (infact that is the use of it).

Note that

extern netbuf *DefaultNetbuf = 0;  // these two are equivalent
netbuf *DefaultNetbuf = 0;

is different from

extern netbuf *DefaultNetbuf;

the former two are called defining declarations and the later is known as referencing declaration.

A referencing declaration can exist multiple times whereas a defining declaration can exist only once. The good thing is that both can exist together in the same translation unit.

Hope this helps ..
0
 

Author Comment

by:dmaroff
ID: 7156176
what files do the referencing declarations go (.cpp or .h) and defining declarations?  Wouldn't all modules share the same variable?

-DM
0
 
LVL 22

Expert Comment

by:ambience
ID: 7156200
yes they all share the same variable, and that variable has to be declared by any of the modules exactly once, though it can be referenced multiple times.

delcarations can go anywhere .h or .cpp or .c, as long as there is exacly one instance of the variable (defining declaration) across all modules/translation units (this is much easily achieved by placing it in some source .cpp/.c file, but that is not a rule).

0
 

Author Comment

by:dmaroff
ID: 7156215
so lets say I have....

file.h --> extern int x;

file2.cpp --> extern int x=1;

file4.cpp --> extern int x=2;

will x be 1 or 2?
0
 
LVL 22

Expert Comment

by:ambience
ID: 7156254
in file2 x is instantiated with value 1 and in file4 it is 2, there are two declarations (instances) for x.

linking the two together should lead to multiple definition error.
0
 

Author Comment

by:dmaroff
ID: 7156999
So something like this would be correct?

file.h --> extern int x;

file2.cpp --> extern int x = 1;

file3.cpp --> extern int x;

file4.cpp --> extern int x;

x in file 2,3 & 4 would be = 1.

-DM
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now