473,756 Members | 6,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

still confused about headers, inline functions, linking, and redefining

Let's say I have headers Sal.h with this class

class Sal
{
public:
int doit() { return 1;}
};

now I know that the compilier can choose NOT to inline this function.
So if I include this in two different libraries, they both choose to
NOT inline it, and then I link to both libs, am I going to get/should I
get a redefinition error?

-sal

Jul 18 '06 #1
6 2913

po************* *@yahoo.com wrote:
Let's say I have headers Sal.h with this class

class Sal
{
public:
int doit() { return 1;}
};

now I know that the compilier can choose NOT to inline this function.
So if I include this in two different libraries, they both choose to
NOT inline it, and then I link to both libs, am I going to get/should I
get a redefinition error?
You will get a redefinition error.

Say both library A and library B have a source file including this
header. This means that, say, a.cpp and b.cpp, after preprocessing,
will include the code above -- the declaration for class Sal and the
definition for Sal::doit.

If you link these libraries together, the linker will simply see two
definitions for the same name, and issue an error.

If you really want to, you can use "extern inline" to provide a
definition only for inlining -- and then provide another definition
somewhere, probably in a source file for the library, when the compiler
decides not to inline the call.

Something like this:

Sal.h:

class Sal
{
public:

extern inline
int doit() { return 1; }
};

Sal.cpp:

int Sal::doit() { return 1; }

Jul 18 '06 #2
po************* *@yahoo.com wrote:
Let's say I have headers Sal.h with this class

class Sal
{
public:
int doit() { return 1;}
};

now I know that the compilier can choose NOT to inline this function.
So if I include this in two different libraries, they both choose to
NOT inline it, and then I link to both libs, am I going to get/should I
get a redefinition error?
No. The compiler will handle it.
Jul 18 '06 #3
Pedro Lamarão wrote:
po************* *@yahoo.com wrote:
>>Let's say I have headers Sal.h with this class

class Sal
{
public:
int doit() { return 1;}
};

now I know that the compilier can choose NOT to inline this function.
So if I include this in two different libraries, they both choose to
NOT inline it, and then I link to both libs, am I going to get/should I
get a redefinition error?


You will get a redefinition error.
No, the compiler will handle it.
Jul 18 '06 #4
Pete Becker wrote:
Pedro Lamarão wrote:
po************* *@yahoo.com wrote:
>Let's say I have headers Sal.h with this class
You will get a redefinition error.

No, the compiler will handle it.
This is why I get confused. Wouldn't it be a linker issue anyway? Is
this covered by any "standard"?

Jul 18 '06 #5
po************* *@yahoo.com wrote:
Pete Becker wrote:
>>Pedro Lamarão wrote:

>>>po********** ****@yahoo.com wrote:
Let's say I have headers Sal.h with this class
You will get a redefinition error.

No, the compiler will handle it.


This is why I get confused. Wouldn't it be a linker issue anyway? Is
this covered by any "standard"?
It is covered by the C++ standard.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Jul 18 '06 #6
Artie Gold wrote:
>>
This is why I get confused. Wouldn't it be a linker issue anyway? Is
this covered by any "standard"?
It is covered by the C++ standard.
Yes, the standard says that duplicates of inline functions are allowed
as long as they are the same sequence of tokens in the function definition.

This is actually the only practical difference inline does. Whether
the declaration does anything else is an implementation detail (a
implementation is free to not inline inline functions or to inline
those not declared inline).
Jul 18 '06 #7

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

Similar topics

14
2786
by: Chris Mantoulidis | last post by:
I am not clear with the use of the keyword inline... I believe you add it do a function when you implement the function inside the header file where the class is stored... But is that all? What am I missing? If that's all, then why did Bjarne even bother adding it to the language? If that's not all, what else can I do with "inline"?
47
3882
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
8
3729
by: Sreenivas | last post by:
Hi, We cannot return a reference to an automatic variable from a function, as per the ANSI C++ standard the behaviour is undefined. Does this hold for inline functions too? or can I return a reference to the automatic variable from the inline function. I meant automatic variable by stack variable strictly local to that function. Thanks, Sreenivas.
7
2860
by: Srini | last post by:
Hello, Rules for inline functions say that they have to be defined in the same compilation unit as their declarations. For class member functions this means that the inline member functions must be defined either within the class or within the same header file. But its generally a good programming practice to have the declarations and definitions in seperate files. This would make the future maintenance of the code easier.
6
2455
by: John Ratliff | last post by:
I was reading the C++ FAQ Lite about inline functions, and it says the following (http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.7) " It's usually imperative that the function's definition (the part between the {...}) be placed in a header file. If you put the inline function's definition into a .cpp file, and if it is called from some other .cpp file, you'll get an "unresolved external" error from the linker. " Why...
9
2661
by: Bilgehan.Balban | last post by:
Hi, If I define an inline function in one .c file, and use it from another, after compiling and linking the two, it seems the function is not inlined but rather called as a regular function. I would expect to see it inlined during linkage of the two object files. Does inlining only occur if the inline function is defined within the same file that it is called? Thanks,
5
5057
by: ciccio | last post by:
Hi, I have a problem with my code that the compiler does not find any inline functions which are static. The simple code example is written below, this is what the compiler throws at me. ] $ g++ main.cpp foo.cpp /home/klaas/tmp/cciAcYgl.o: In function `main':
1
2397
by: arjor | last post by:
hey guys When i try to make the following mutating method inline i get the following error: //method inline void vertex::setValue(int new_value) throw() { value_ = new_value; } error LNK2019: unresolved external symbol "public: void __thiscall vertex::setValue(int)" (?setValue@vertex@@QAEXH@Z) referenced in
17
8381
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
0
9384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9212
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9973
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8645
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5247
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.