I'm using a shared library that defines inline functions in its header.
Here is a reduced test case, as seen by the compilation unit linking to the library (for the version seen by the library, just replace dllimport
by dllexport
).
class __declspec(dllimport) MyClass {
public:
int myFunc2();
int myFunc1();
};
inline int MyClass::myFunc2(void) {
return myFunc1();
}
inline int MyClass::myFunc1(void) {
return 0;
}
Compiling this gives the warning:
warning: 'int MyClass::myFunc1()' redeclared without dllimport attribute after being referenced with dll linkage [enabled by default]
Please note that the order in which the functions are defined is important, as putting the definition of myFunc1
before the definition of myFunc2
results in no warnings.
Note also that this code compiles without warnings under Visual C++. These warnings are specific at least to MinGW, maybe to GCC in general. Edit: it came to my mind that I may have to verify if the warning is not inhibited by one of the flags set by the project.
My questions are then:
- Why this behavior ?
- Declaring
myFunc1
asinline
inside the class declaration fixes the problem. Why is that ? It's also against the recommended way of doing things. - Is there another (better ?) way to fix this problem ?
__declspec(dllimport)
to the second declaration gets me awarning: inline function 'int MyClass::myFunc1()' declared as dllimport: attribute ignored [-Wattributes]
(the same formyFunc2
). Which is also kind of baffling, but I'm supposing all of this is arbitrary. – Exceedingly