Qt signal wrapped in ifdef

My Qt project is linking to a library that is linux only. When a project starts under linux, I want the signal to be triggered by an event using the type defined in this library. However, the complication that I have is that the project must also be built on Windows. Obviously, this signal and the slot will not catch it in Windows, and this is wonderful. However, I found problems with the Qt moc tool that does not recognize the existence of #ifdef __linux__ around code that emits a signal. My code is as follows:

[SomeFile.h]

 #ifdef __linux__ signals: void SomeSignal(SomeTypeDefinedInTheLinuxLibrary); #endif 

[SomeFile.cpp]

 #ifdef __linux__ emit SomeSignal(someObject); #endif 

When I try to compile this with g ++, I get the error: SomeFile.cpp:(.text+0x858c): undefined reference to SomeFile::SomeSignal(SomeTypeDefinedInTheLinuxLibrary)

Any ideas on how to get moc and #ifdefs to play well together?

+4
source share
2 answers

A much better solution is to always provide a signal and simply comment on the code that runs it on Windows. Thus, the public API is the same on all platforms.

[EDIT] The moc tool is really dumb. He really does not understand the code; instead, it simply responds to specific patterns. Therefore, it ignores #ifdef .

To solve this problem, wrap the type or use #ifndef __linux__ and define your own dummy type there so that it compiles on Windows. Since the signal will not go out on Windows, the slot will never be used, so any type that forces you to compile the code should be fine.

+5
source

With Qt 5.3, at least with Visual Studio, I can pass pre-processor macros to the moc tool. To do this, I had to modify the text of my Visual Studio project and manually add command line arguments for each file to pass the preprocessor arguments to the moc tool. You can use -D [Pre-Processor], i.e. -DSPECIAL_BUILD or -DSPECIAL_BUILD = 1, and the moc compiler is smart enough to see #if SPECIAL_BUILD checking your code and not trying to execute these parts.

Just search for "moc.exe" and add the appropriate parameters for each configuration.

0
source

All Articles