473,769 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inline functions...

I was reading the C++ FAQ Lite about inline functions, and it says the
following
(http://www.parashift.com/c++-faq-lit...s.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 will the linker not be able to resolve this?

Thanks,

--John Ratliff
Jul 27 '05 #1
6 2456
Lets take some example:
You have some header in which u have declared that function as inline

//SomeHeader.h
inline void Somefunction();

//SomeCpp.cpp
#include "SomeHeader .h"

inline void Somefunction()
{
// Implementaion..
}

Suppose u are using this function SomeFunction..
//SomeOtherCpp.cp p

void Function()
{
....
Somefunction();
...
}

Now, it has to get the defination of SomeFunction during compile time for
the inline replacement.
But with this compiler unit ( not linking unit) which includes SomeHeader.h
and someOtherCpp.cp p file, there it wont get the implementation of inline
function !!
So, is the problem.

So, usually we implement them in .h files only.
Or, if you take Symbian, it prefers .inl (inline) file which defines inline
function implementation and is included at the end of the corresponding .h
file

Regards
Girish
"John Ratliff" <so*****@micros oft.com> wrote in message
news:i3GFe.1841 94$x96.65403@at tbi_s72...
I was reading the C++ FAQ Lite about inline functions, and it says the
following
(http://www.parashift.com/c++-faq-lit...s.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 will the linker not be able to resolve this?

Thanks,

--John Ratliff

Jul 27 '05 #2
John Ratliff wrote:
I was reading the C++ FAQ Lite about inline functions, and it says the
following
(http://www.parashift.com/c++-faq-lit...s.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 will the linker not be able to resolve this?

Thanks,

--John Ratliff


I'm trying to become an expert in linking in my spare time. ;) It really is
a broad subject.

My guess as to why you "need" to put the inline function sin the "header"
file is because they must be visible when the compiler uses the class
definition.

Note that the definition of a class begins with the class heading, and ends
with the };. Even if the member functions are not defined at the time the
compiler finishes with the class body, the class is defined.

inline functions get placed directly into the object code for the routine
that invokes them. This is different than what happens with non-inline
functions. With them, all you need as a signature. The linker can take
care of finding them when they are needed.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 27 '05 #3
Thanks. I understand now.

--John Ratliff
Jul 27 '05 #4
* John Ratliff:
I was reading the C++ FAQ Lite about inline functions, and it says the
following
(http://www.parashift.com/c++-faq-lit...s.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 will the linker not be able to resolve this?


A particular C++ implementation may do anything. The FAQ just mentions the
most common in-practice case (there should perhaps be some lawyereese
qualification and disclaimer, but then it would soon get out of hand and
become unreadable, sort of like the standard). Formally §3.2/3 requires
that an inline function is defined in every translation unit it's used in.

In fact, with MSVC 7.1 you can do e.g.

inline void f();
int main(){ f(); }

in one .cpp file, and

#include <iostream>
#include <ostream>
void f() { std::cout << "huh" << std::endl; }

in another, and the linker will be happy.

This program is not formally valid because §7.1.2/4 says that if a function
is declared inline in one translation unit, it shall be declared inline in
all translation unit it appears in, which is why the FAQ recommends doing it
in a header file. However, as the standard says, "no diagnostic is
required". And that's what MSVC 7.1 does, no diagnostic.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 27 '05 #5
* Steven T. Hatton:

inline functions get placed directly into the object code for the routine
that invokes them.
Correction: may.

This is different than what happens with non-inline functions.


Correction: this is the same as...
Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 27 '05 #6
> I'm trying to become an expert in linking in my spare time. ;) It really is
a broad subject. My guess as to why you "need" to put the inline function sin the "header"
file is because they must be visible when the compiler uses the class
definition.
Note that the definition of a class begins with the class heading, and ends
with the };. Even if the member functions are not defined at the time the
compiler finishes with the class body, the class is defined.
inline functions get placed directly into the object code for the routine
that invokes them. This is different than what happens with non-inline
functions. With them, all you need as a signature. The linker can take
care of finding them when they are needed.

I thought a little history might be useful. You are right as to what
inline implies, but slightly off from what it has become.
The experimental compiler (early days) would generate error for many
cases of inline. For instance, if you included a for-loop it would say,
"Sorry, for-loop not implemented for inline, yet", or something
like that.
This became annoying, and would not be easy to maintain for commercial
compilers. For instance, to remove the error (or warning) when it
became possible to do so required maintenance.
It was decided to treat inline as a request to the compiler. Thus,
compiler will do inline if it can, otherwise it will pass it on to
linker for (external) linkage.
Recently (well, a few years ago) there were some complications in
separate compilations (projects involving multiple source files)
involving C and CPP source files, with regard to inline. It was decided
to allow external linkage for inline functions, anyway. That should
explain the behavior of VC++.
Again, your statement correctly explains the linkage error. However,
there were deviations from the original intent. In the case posted
here, the particular compiler is not creating external linkage, so the
linker cannot resolve the symbol (the call). The designers are taking
your view.

Hope this helps.

Regards,
zo****@ZHMicro. com
http://www.zhmicro.com
http://distributed-software.blogspot.com

Jul 27 '05 #7

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

Similar topics

13
6571
by: A | last post by:
Hi, I'm having problems completing a project in C++. I have been using inline functions in some of my header files. I have only done so for simple functions that only have 1 statement (eg. accessor and mutator methods to access private data members). I would like to know if there are any issues with using inline functions that may have attributed to my errors before I start separting them out into "outline functions". Regards
14
2788
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
3884
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
7
2861
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.
4
1821
by: Tony Johansson | last post by:
Hello experts! I'm reading a book about C++ and there is something about inline that the book says that is unclear for me. The book says the following "Because inline functions are expanded at compile time, definitions of these functions, unlike other definitions, cannot be separately compiled and must be placed in header files. This creates a problem if the compiler does not actually inline a
7
2035
by: Alvin | last post by:
Hello all, I'm curious as to your opinions on explicitly inlining function? I'm talking about functions as members of a class. For example, so class A defines a operator==() and a operator!=(): class_a.h: class A {
43
13304
by: Patrick Laurent | last post by:
Hello I have a program with many many inlined template functions It is essential for the execution speed that every (or almost every) function marked as inlined, becomes really inlined by the compiler. I already compiled the program with Intel Compiler (ICL) on Visual C++, and it works fine and fast. I verified that the functions are really inlined. But with GCC 3.4 (Linux+Cygwin) or ICC (Linux), The same program is about 5
18
5069
by: Method Man | last post by:
If I don't care about the size of my executable or compile time, is there any reason why I wouldn't want to inline every function in my code to make the program run more efficient?
12
676
by: sam_cit | last post by:
Hi Everyone, I have few questions on inline functions, when i declare a function as inline, is it for sure that the compiler would replace the function call with the actual body of the function? or is it a call taken by compiler? Second, i see that it is same as what Macro's used to do for c, if so what is the advantage for going in for inline functions than to Macros?
2
2116
by: aaragon | last post by:
Hi everyone, I would like to create a very simple function: // header file inline void point_map() { PointMap pointMap = get(vertex_point_t(), g); } // main.cpp
0
9422
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,...
1
9987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
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
7404
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
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5294
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...
1
3952
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
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.