R
Richard Hayden
Hi,
I have the following code:
/******************************** file1.c
#include <iostream>
extern void dummy();
inline int testfunc() {
return 1;
}
int main(int argc, char** argv) {
std::cout << testfunc();
dummy();
return 0;
}
/****************************************
/******************************** file2.c
#include <iostream>
inline int testfunc() {
return 2;
}
void dummy() {
std::cout << testfunc();
}
/****************************************
Now, I wasn't sure exactly what this code should do (by 'this code', I
mean the program obtained by compiling these two files and then linking
their respective object files). I was thinking it would output 12 or
there'd be an error of some description.
However, depending on the order of linkage, it succeeds and outputs 11
or 22. In other words, the linker is somehow removing multiple
definitions of this inline function and resolving both 'calls' to one of
them... but surely if it's inline, it will be dealt with at compile time
and the linker should have nothing to do with it? I know that 'inline'
doesn't guarantee inlining, but surely the linker can't decide not to
inline it, since surely that decision must be made at compile time. It
only behaves as expected if I force internal linkage on the testfuncs
with the 'static' keyword. There's something going on, because the
linker obviously 'doesn't mind' that this function has been multiply
defined, which I assume is because the compiler needed to have a
definition in each translation unit in order to be inlined it. I'm
confused though how the linker is resolving these multiple definitions
though. Any insight into inlines and external linkage etc. would be
greatly appreciated.
FYI, I'm using g++.
Thanks,
I have the following code:
/******************************** file1.c
#include <iostream>
extern void dummy();
inline int testfunc() {
return 1;
}
int main(int argc, char** argv) {
std::cout << testfunc();
dummy();
return 0;
}
/****************************************
/******************************** file2.c
#include <iostream>
inline int testfunc() {
return 2;
}
void dummy() {
std::cout << testfunc();
}
/****************************************
Now, I wasn't sure exactly what this code should do (by 'this code', I
mean the program obtained by compiling these two files and then linking
their respective object files). I was thinking it would output 12 or
there'd be an error of some description.
However, depending on the order of linkage, it succeeds and outputs 11
or 22. In other words, the linker is somehow removing multiple
definitions of this inline function and resolving both 'calls' to one of
them... but surely if it's inline, it will be dealt with at compile time
and the linker should have nothing to do with it? I know that 'inline'
doesn't guarantee inlining, but surely the linker can't decide not to
inline it, since surely that decision must be made at compile time. It
only behaves as expected if I force internal linkage on the testfuncs
with the 'static' keyword. There's something going on, because the
linker obviously 'doesn't mind' that this function has been multiply
defined, which I assume is because the compiler needed to have a
definition in each translation unit in order to be inlined it. I'm
confused though how the linker is resolving these multiple definitions
though. Any insight into inlines and external linkage etc. would be
greatly appreciated.
FYI, I'm using g++.
Thanks,