Solved

multiple definition of a symbol.

Posted on 2002-07-12
11
1,542 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

777 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