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

static inline function not found during linking

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':
inline.cpp:(.text+0x9): undefined reference to `foo::bar()'
collect2: ld returned 1 exit status

Is this normal behaviour??? I seriously doubt that.
If foo::bar(void) is not inline, it works.
When everything is written in one file, it works too.

regards,
This is the simple code

== foo.hpp ==
#ifndef _FOO_
#define _FOO_
class foo {
public:
static int a;
static int bar(void) ;
};
#endif
== foo.cpp ==
#include "foo.hpp"
int foo::a = 0;
inline int foo::bar(void) { return a; }
== main.cpp ==
#include "foo.hpp"
int main(void) {
int b = foo::bar();
return 0;
};
==========
Dec 21 '07 #1
5 4986
ciccio wrote:
I have a problem with my code that the compiler does not find any
inline functions which are static.
Since your function is never declared 'inline' when 'main.cpp' is
compiled, the compiler expects a definition of that function to exist
somewhere. Since the implementation of that function is declared
'inline' in 'foo.cpp', the body is essentially ignored because it is
not called anywhere in 'foo.cpp'.

Either put the function in the header (where declaring it 'inline'
makes sense), or remove the 'inline' modifier from the function
definition in 'foo.cpp'.
>
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':
inline.cpp:(.text+0x9): undefined reference to `foo::bar()'
collect2: ld returned 1 exit status

Is this normal behaviour??? I seriously doubt that.
If foo::bar(void) is not inline, it works.
When everything is written in one file, it works too.

regards,
This is the simple code

== foo.hpp ==
#ifndef _FOO_
#define _FOO_
class foo {
public:
static int a;
static int bar(void) ;
};
#endif
== foo.cpp ==
#include "foo.hpp"
int foo::a = 0;
inline int foo::bar(void) { return a; }
== main.cpp ==
#include "foo.hpp"
int main(void) {
int b = foo::bar();
return 0;
};
==========
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 21 '07 #2
Thanks for the quick response,

I just read it in stroustrup. I had not a clue!

Regards
Victor Bazarov wrote:
ciccio wrote:
>I have a problem with my code that the compiler does not find any
inline functions which are static.

Since your function is never declared 'inline' when 'main.cpp' is
compiled, the compiler expects a definition of that function to exist
somewhere. Since the implementation of that function is declared
'inline' in 'foo.cpp', the body is essentially ignored because it is
not called anywhere in 'foo.cpp'.

Either put the function in the header (where declaring it 'inline'
makes sense), or remove the 'inline' modifier from the function
definition in 'foo.cpp'.
>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':
inline.cpp:(.text+0x9): undefined reference to `foo::bar()'
collect2: ld returned 1 exit status

Is this normal behaviour??? I seriously doubt that.
If foo::bar(void) is not inline, it works.
When everything is written in one file, it works too.

regards,
This is the simple code

== foo.hpp ==
#ifndef _FOO_
#define _FOO_
class foo {
public:
static int a;
static int bar(void) ;
};
#endif
== foo.cpp ==
#include "foo.hpp"
int foo::a = 0;
inline int foo::bar(void) { return a; }
== main.cpp ==
#include "foo.hpp"
int main(void) {
int b = foo::bar();
return 0;
};
==========
Dec 21 '07 #3
Okay I got this part now,

But ... when I use templates this is not the case.

Here is again an example (now without the static, since that does not
matter).

The compilation does not give any linking error here.
So what is the reason of this then? Does this mean that inline
functions of templates are just bogus?

Regards
== foo.hpp ==
#ifndef _FOO_
#define _FOO_

template<typename T>
class foo {
public:
int a;
int bar(void) ;
};
#endif
== foo.cpp ==
#include "foo.hpp"
template<typename T>
inline int foo<T>::bar(void) { return a; }
template class foo<int>;
== main.cpp ==
#include "foo.hpp"
int main(void) {
int b = foo<int>().bar();
return 0;
};

Dec 21 '07 #4
ciccio wrote:
Okay I got this part now,

But ... when I use templates this is not the case.
What is not the case?
Here is again an example (now without the static, since that does not
matter).

The compilation does not give any linking error here.
So what is the reason of this then? Does this mean that inline
functions of templates are just bogus?
Not "inline functions of templates", but "function templates declared
inline".

Right after defining your function and declaring it 'inline' (which
you're not really supposed to do, so your compiler _probably_ ignored
it), you _explicitly_ instantiate your template, which basically
introduces the definitions of all member functions (if it can, and it
should be able to, since you just defined your function), which are
of course _not_ inline.

IOW, whatever you declared 'inline' is *not* the function the linker
is looking for when resolving the reference from 'main'. It's looking
for 'foo<int>::bar', not for 'foo<T>::bar'. And your 'foo<int>::bar'
is non-inline (as the result of explicit instantiation).
>
Regards
== foo.hpp ==
#ifndef _FOO_
#define _FOO_

template<typename T>
class foo {
public:
int a;
int bar(void) ;
};
#endif
== foo.cpp ==
#include "foo.hpp"
template<typename T>
inline int foo<T>::bar(void) { return a; }
template class foo<int>;
== main.cpp ==
#include "foo.hpp"
int main(void) {
int b = foo<int>().bar();
return 0;
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 21 '07 #5
ciccio wrote:
Okay I got this part now,

But ... when I use templates this is not the case.

Here is again an example (now without the static, since that does not
matter).

The compilation does not give any linking error here.
So what is the reason of this then? Does this mean that inline
functions of templates are just bogus?

Regards
== foo.hpp ==
#ifndef _FOO_
#define _FOO_

template<typename T>
class foo {
public:
int a;
int bar(void) ;
};
#endif
== foo.cpp ==
#include "foo.hpp"
template<typename T>
inline int foo<T>::bar(void) { return a; }
template class foo<int>;
== main.cpp ==
#include "foo.hpp"
int main(void) {
int b = foo<int>().bar();
return 0;
};
Irrelevant to your issue, but still a problem, is your include guard.

Any identifier with a leading underscore, followed by an uppercase
letter, is reserved to the implementation. Period. You may not use it
for your own purposes.

So your include guard of _FOO_ is illegal.
Dec 21 '07 #6

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

Similar topics

3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
6
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...
5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
1
by: Arnaud Debaene | last post by:
Hello, I think I found a bug in VC 7.1 concerning destruction of stack objects when linking a static, non managed, C++ library within a managed C++ application. Here is a repro case : 1)...
9
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...
4
by: Sean | last post by:
I am a little confused by the "extern inline and static inline" rules. I understand that "extern inline" guarantees no function storage is created (all are inlined). But the following test seems to...
1
by: wenqiang.zhou | last post by:
i only kown that static members doesnt blong to any object and it have to initialize outside class, inline is near the same as define it will replace in code.but i was puzze by the following codes....
0
by: xieml2007 | last post by:
Dear Madam or Sir, I encountered one problem which is quite similiar to the discussions launched at the web site: http://www.thescripts.com/forum/thread280324.html
17
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...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.