You are not logged in.

#1 2011-07-30 10:36:25

linux-ka
Member
From: ADL
Registered: 2010-05-07
Posts: 232

[C++] How to link inline functions

Hi,

I am writing a small tool as kind of practice. The first step to improve my c++ knowledge was to split up my source code to make it more readable. Now I can compile each source to objects.

The problem herein is that I cannot link all the *.o files, because of missing functions, which are definitely defined in one of those source files. The problem vanishes when declaring all functions from inline to non-inline / standard. 

My question, is there any possibility to link objects containing inline functions? Till now I could not find any clue.

Thanks in advance  for any advice,

L-K

Offline

#2 2011-07-30 10:40:51

Ockonal
Member
From: Ukraine
Registered: 2010-10-11
Posts: 48
Website

Re: [C++] How to link inline functions

Don't forget that inline function body should be only in header. Look at this when working with them.

Offline

#3 2011-07-30 10:51:46

linux-ka
Member
From: ADL
Registered: 2010-05-07
Posts: 232

Re: [C++] How to link inline functions

You mean I just have to define the prototype to be inline?

EDIT:

This does not work!

Last edited by linux-ka (2011-07-30 10:54:47)

Offline

#4 2011-07-30 10:55:08

snakebite
Member
From: Norway
Registered: 2009-05-13
Posts: 42

Re: [C++] How to link inline functions

You can't "link" against inline functions because these are handled at compile-time. Put the inline function definitions in a header file, and include that file in every .cpp file were you want to use those functions.

Offline

#5 2011-07-30 11:00:15

linux-ka
Member
From: ADL
Registered: 2010-05-07
Posts: 232

Re: [C++] How to link inline functions

This doesn't work. I have my header with lets say

 inline void test(); 

I can build the object but I cannot link the object. I know I can use those functions when they are directly impolemented in the main file.

Offline

#6 2011-07-30 11:09:11

Ockonal
Member
From: Ukraine
Registered: 2010-10-11
Posts: 48
Website

Re: [C++] How to link inline functions

You can't just write

 inline void test(); 

You have to indicate the body of function in header and than use anywhere you need it.
Can't understand the problem.

Offline

#7 2011-07-30 11:32:41

linux-ka
Member
From: ADL
Registered: 2010-05-07
Posts: 232

Re: [C++] How to link inline functions

I think I understood you in the wrong way....I have to move the body of this inline function to the header, this is correct?

Well this is the way how I did it before and the one I wanted to change...Maybe as a consequence I haveto move away from inline functions. 

I know it is a controversial topic, but which function actually benefit the most from being set to  inline? I Perhaps I won't lose much performance.

Offline

#8 2011-07-30 11:47:08

Ockonal
Member
From: Ukraine
Registered: 2010-10-11
Posts: 48
Website

Re: [C++] How to link inline functions

>I have to move the body of this inline function to the header
If you have inline function it should be only in header. And it should has a body near definition.

Macros are almost the same but I wouldn't recommend to use them.

Offline

#9 2011-07-30 13:57:11

lunar
Member
Registered: 2010-10-04
Posts: 95

Re: [C++] How to link inline functions

@linux-ka:  Anything smaller than say five lines or so can probably profit from being inline.  Especially one-line getter functions, which simple return a private class attribute profit from inlining.  However, inline functions impose severe restrictions on class design, if one wants to maintain a stable ABI (e.g. you can reorder private class attributes, if any inline function accesses private attributes).

Given this, I'd consider inlining functions for no reason as "premature optimization".  Inlining should only be done, if runtime speed is measurably low, and profiling revealed function calls as time consuming task.  By default, just write non-inlined functions.  An exception are not fully specialized template functions.  These can't be externed anyway (at least not with most compilers), thus one can just as well apply inlining right away, if the function is reasonably short.

Big C++ class libraries, which don't use templates, also use inline functions only scarcely.  Qt for instance has inline functions only at some very essential points (e.g. size calculations on widgets), the KDE library style guide completes advises against inline functions.  Class libraries which heavily rely on templates and/or also do heavy computations (e.g. ITK for medical image processing) use many inline functions.

Offline

Board footer

Powered by FluxBB