Solved

multiple definition of a symbol.

Posted on 2002-07-12
11
1,592 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

734 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