473,324 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Link question

I am not a newbie C++, but I haven't coded multiple files project under
g++.
Here is my quesiont:

b.h
////////////////////
#ifndef b_h
#define b_h

class b{
int b1;
public:
b(){}
void set(int b1_);
};

void b::set(int b1_)
{
b1=b1_;
}

#endif


a.h
===========================
#include "b.h"

class a{
b* b_ptr;
public:
void set(b & b_obj);
};

void a::set(b & b_obj)
{
b_ptr=&b_obj;
}
main.cpp
////////////////////////////
#include "a.h"
#include "b.h"

int main()
{
a a_obj;
b b_obj;

a_obj.set(b_obj);

return 0;
}

For these 3 files, I can successfully compile and link by using "g++
main.cpp".
However, if I separate a.h into a.h and a.cpp, like here:
a.h
===========================
#include "b.h"

class a{
b* b_ptr;
public:
void set(b & b_obj);
};

a.cpp
/////////////////////////
#include "a.h"

void a::set(b & b_obj)
{
b_ptr=&b_obj;
}

then I use the command "g++ a.cpp main.cpp", I got a link error
"ld: fatal: symbol 'b::set(int)' is multiply-defined:
(file /var/tmp/ccUoZBm0.o type=FUNC; file /var/tmp/ccG0aQup.o
type=FUNC)"

and if I also separate b.h into b.h and b.cpp in the same way, the
error is gone.

What's going on here?
I just want to separate one of the files because in my real project,
there's only one .h file is so long.

Jul 23 '05 #1
4 1542
fi******@yahoo.com.tw wrote:
I am not a newbie C++, but I haven't coded multiple files project under
g++.
Here is my quesiont:

b.h
////////////////////
#ifndef b_h
#define b_h

class b{
int b1;
public:
b(){}
void set(int b1_);
};

void b::set(int b1_)
If you want to keep the _definitions_ of your functions in a header,
declare them 'inline', or define them in the class definition (where
they are 'inline' implicitly).
{
b1=b1_;
}

#endif


a.h
===========================
#include "b.h"

class a{
b* b_ptr;
public:
void set(b & b_obj);
};

void a::set(b & b_obj)
{
b_ptr=&b_obj;
}
main.cpp
////////////////////////////
#include "a.h"
#include "b.h"

int main()
{
a a_obj;
b b_obj;

a_obj.set(b_obj);

return 0;
}

For these 3 files, I can successfully compile and link by using "g++
main.cpp".
However, if I separate a.h into a.h and a.cpp, like here:
a.h
===========================
#include "b.h"

class a{
b* b_ptr;
public:
void set(b & b_obj);
};

a.cpp
/////////////////////////
#include "a.h"

void a::set(b & b_obj)
{
b_ptr=&b_obj;
}

then I use the command "g++ a.cpp main.cpp", I got a link error
"ld: fatal: symbol 'b::set(int)' is multiply-defined:
(file /var/tmp/ccUoZBm0.o type=FUNC; file /var/tmp/ccG0aQup.o
type=FUNC)"

and if I also separate b.h into b.h and b.cpp in the same way, the
error is gone.

What's going on here?
You're putting its definition in more than one module. No surprises.
I just want to separate one of the files because in my real project,
there's only one .h file is so long.


The number of '.h' files doesn't matter. What matters is in how many
'.cpp' files you're including it. Just think about it. What you do,
essentially, is duplicate all the code in all the files where you include
the header. '#include' directive is nothing but a text processing stunt.

V
Jul 23 '05 #2
Well, I think I find the answer.

Every .o file will contain the implematitions of the files it included.
In my example, if I have 4 files, "b.h", "a.h", "a.cpp", "main.cpp",
there would be "a.o" and "main.o". Both of them will have the function
implementation writed in "b.h".( because they both can see "b.h") In
the linking stage, this would cause a duplicate definition problem when
the linker tries to link "a.o" and "main.o" together.

The solution to this is to split "b.h" into "b.h" and "b.cpp" in the
same way as "a.h".
In this way, the implementation of class b will be only in "b.o", "a.o"
and "main.o" will contain the information in "b.h" which is only the
declartion of class b. When the three objects are linked together, the
three object "b.o", "a.o" and "main.o" will contain the implementations
of theirselves.

Another workable situation is having "b.h", "b.cpp", "a.h" and
"main.cpp".
In this situation "b.o" contains class b's declaration and
implementation, "main.o" contains class b's declaration and class a's
declaration and implementation. Declarations can be overlapped in
different object files, however, implementation of each class would be
only appear once in corresponding object file.

To Victor:
Use inline for functions in class b is indeed work, but the reason is
not like that you say here. Inline function just tell the compiler to
append the definition of the function into where the function is being
called instead of go into the address of the function in the linking
stage which is the way how function is called in implementation of
functions in a .cpp file. The reason work in my example is because
there will be no implementation of class b in any .o object, so there's
no multiple-definition problem.

I may not be express very well in my poor English, if you have any
comments please feel free to send me.

Jul 23 '05 #3
I find this web page is very helpful.
http://www.gamedev.net/reference/art...rticle1798.asp

Problem 4 mentioned here give me more sense about it.

Jul 23 '05 #4
fi******@yahoo.com.tw wrote:
[...]
To Victor:
Use inline for functions in class b is indeed work, but the reason is
not like that you say here.
I don't say any reason.
Inline function just tell the compiler to
append the definition of the function into where the function is being
called instead of go into the address of the function in the linking
stage which is the way how function is called in implementation of
functions in a .cpp file. The reason work in my example is because
there will be no implementation of class b in any .o object, so there's
no multiple-definition problem.
That's not necessarily so. Inline functions are treated differently than
non-inline ones. It has to "merge" all the definitions of the same inline
function into one (or discard all but one, it doesn't matter, really).
Such operation ("merging" or "discarding") is not attempted for normal,
non-inline functions.
[..]


V
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pjotr Wedersteers | last post by:
Hello again, I have now several consecutive forms in my site which nicely pass variables in POST, so they won't show up in the header. Now I have a page people reach after submitting all kinds...
3
by: Chris Wilkinson | last post by:
Hi there, I'm creating a frames website, which consists of a nav bar down the left of the screen (name="frame1") and a content frame filling the right hand side (name="frame2"). I've used nice...
3
by: joseluismarchetti | last post by:
Hello everybody, Although I am sure this is an important question for this group, I am not sure this question belongs to this group and I will be happy to move it to the correct one after you...
10
by: Gary Hughes | last post by:
I'm getting the following error when attempting to link a managed C++ dll. I can't find any reference to these errors with google. Can anyone help? I've included the class definition causing the...
14
by: Steve McLellan | last post by:
Hi, Sorry to repost, but this is becoming aggravating, and causing me a lot of wasted time. I've got a reasonably large mixed C++ project, and after a number of builds (but not a constant...
14
by: Xah Lee | last post by:
if i want to have a empty link, which way is more proper? <a href=""> <a href="#"> <a href="javascript:void(0);"> Xah xah@xahlee.org ∑ http://xahlee.org/
26
by: Mica Cooper | last post by:
Hi, I need to pass some info in a javascript submit. <a href="javascript:document.formName.submit();">Submit Form</a> Normally a link would do page.jsp?x1=1&x2=2&x3=3 and you would pull x1,...
4
by: ben.dixon | last post by:
Hi, I'm using links for navigation on a test page, so each link will go to a question in the test e.g. Question 1, Question 2 etc. For each link i am using an Outer Border, Middle border and...
3
by: friday13 | last post by:
Hi, I would like to set the link's "visited" pseudo-class with javascript without clicking on the link. My goal is to update the link's color (previously set in the CSS file) to be "visited"...
4
by: henry | last post by:
Folks: As a follow-up to my recent posts, I want to ask some more general questions about multiple instances of a CSS link in a page as seen by browsers due to server-side file inclusion. Let...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.