My Qt project links to a library that is linux-only. When the project is run under linux, I wish to have a signal fired on an event using a type defined in that library. A complication that I have, though, is that the project must also build in Windows. Obviously, this signal and the slot catching it wouldn't exist in Windows, and that's fine. I am, however, finding issues with Qt's moc tool failing to recognize the existence of an #ifdef __linux__
around the code that emits the signal. My code looks like this:
[SomeFile.h]
#ifdef __linux__
signals:
void SomeSignal(SomeTypeDefinedInTheLinuxLibrary);
#endif
[SomeFile.cpp]
#ifdef __linux__
emit SomeSignal(someObject);
#endif
When I attempt to compile this with g++, I get the error:
SomeFile.cpp:(.text+0x858c): undefined reference to SomeFile::SomeSignal(SomeTypeDefinedInTheLinuxLibrary)
Any ideas how to get moc and #ifdefs to play well together?
A much better solution is to always provide the signal and just comment out the code that fires it on Windows. That way, the public API is the same on all platforms.
[EDIT] The moc tool is really dumb. It doesn't actually understand the code; instead it just reacts on certain patterns. That's why it ignores the #ifdef
.
To solve the issue, wrap the type or use #ifndef __linux__
and define your own dummy type in there so it compiles on Windows. Since the signal won't be emitted on Windows, the slot will never be used so any type that makes the code compile should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With