473,383 Members | 1,891 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,383 software developers and data experts.

Why this doesn't violate One definition rule and how the linker resolves this

I am using gcc compiler
gcc B.o Main.o -lMyLib

//A.h
class A {
public:
A();
void abc ();
void bcd ();
};

Class A is defined in MyLib ie (A.cpp) is in MyLib and it also contains
A.h

Main.cpp also #include "A.h"

Now if change the A.h #included in Main.cpp only as

//A.h changed only in Main.cpp

//A.h
class A {
public:
A();
void abc ();
void bcd ();
Test () {std::cout << "hello world " << std::endl; }

};

This still compiles and I can invoke the Test in Main.cpp. This
compiles even if Test is non-inline.
Does n't this violate the One Definition rule of C++. Also how does the
linker resolve this

Mar 2 '06 #1
3 2104
Ninan wrote:
This still compiles and I can invoke the Test in Main.cpp. This
compiles even if Test is non-inline.
Does n't this violate the One Definition rule of C++. Also how does the
linker resolve this


Compilers rarely enforce that rule. The C languages use definitions and
compilation models that are all based - indirectly - on the original C
linker, which was amazingly broken. Our language uses .h files, instead of
modules, essentially because nobody wanted to fix and upgrade that linker.

So to this day linkers for C languages permit all kinds of excesses. For
example, you can sometimes say 'extern int foo = 42;' in every translation
unit, and never write 'int foo = 42;' anywhere, and the compiler will
accept this and create storage for foo.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 2 '06 #2
Ninan posted:
I am using gcc compiler
gcc B.o Main.o -lMyLib

//A.h
class A {
public:
A();
void abc ();
void bcd ();
};

Class A is defined in MyLib ie (A.cpp) is in MyLib and it also contains
A.h

Main.cpp also #include "A.h"

Now if change the A.h #included in Main.cpp only as

//A.h changed only in Main.cpp

//A.h
class A {
public:
A();
void abc ();
void bcd ();
Test () {std::cout << "hello world " << std::endl; }

};

This still compiles and I can invoke the Test in Main.cpp. This
compiles even if Test is non-inline.
Does n't this violate the One Definition rule of C++. Also how does the
linker resolve this

Just because a particular compiler compiles it doesn't mean it's not bad
code.

For instance, make an inline function like this:

inline int GetNumber()
{
static int blah = 4;

return ++blah;
}

Put it in a header file and include it in two cpp files.

Now make another inline function, but change it:

inline int GetNumber()
{
static int blah = 242352235;

return ++blah;
}
Now inlcude this in a few cpp files.

Most, if not all, compilers will compile that with no errors.

Bottom Line: The Standard says you can't do that.

-Tomás
Mar 2 '06 #3
On 2 Mar 2006 10:56:51 -0800, "Ninan" <ni****@yahoo.com> wrote in
comp.lang.c++:
I am using gcc compiler
gcc B.o Main.o -lMyLib

//A.h
class A {
public:
A();
void abc ();
void bcd ();
};

Class A is defined in MyLib ie (A.cpp) is in MyLib and it also contains
A.h

Main.cpp also #include "A.h"

Now if change the A.h #included in Main.cpp only as

//A.h changed only in Main.cpp

//A.h
class A {
public:
A();
void abc ();
void bcd ();
Test () {std::cout << "hello world " << std::endl; }

};

This still compiles and I can invoke the Test in Main.cpp. This
compiles even if Test is non-inline.
Does n't this violate the One Definition rule of C++. Also how does the
linker resolve this


Yes, it violates the ODR. That means your program has undefined
behavior, and the C++ language places no requirements at all on the
results.

As to what your particular linker does, that is not an issue the
language either knows or cares about, and it is specific to your
linker. In any case, since you have broken the rules, once again C++
does not care what happens.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 3 '06 #4

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

Similar topics

7
by: CoolPint | last post by:
I read that there has to be only one definition of classes, functions, etc. in a project. I just realized I might have broken the rule by defining the exception classes in the header file. But...
4
by: Gianni Mariani | last post by:
I posted a gcc bug http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13332 here is the text: ----------------------------------------------------------- This code will compile fine but fail on...
9
by: Felix Kater | last post by:
Hi, if I link my code to other o-files: Does the linker include the whole o-file or does it use the needed functions from that only? Felix
10
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
4
by: Belebele | last post by:
In a cpp file, say a.cpp I have the following code: #include <element.h> struct Functor { void operator (Element const& ) const { /* ... */ } }; /* ... */
4
by: Mike | last post by:
Hi ! I have some strange problem and I would like to know if it is a bug or not : In my projects, in 2 different .cpp files, I use the same name to define a local structure: file1.cpp : ...
11
by: Old Wolf | last post by:
Does the following program require a diagnostic? Which section of the Standard deals with this? (I read the section on function calls and didn't see anything). void f(void); int main(void) {...
5
by: aryan | last post by:
Is it not allowed to have template function definition in a .cc file? Here is the scenario. The tempage function declaration is in a header file and the definition in a .cc file. The function is...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.