robotology / blockfactory

A tiny framework to wrap algorithms for dataflow programming
https://robotology.github.io/blockfactory
GNU Lesser General Public License v2.1
40 stars 16 forks source link

Fix for interference with Windows.h #57

Closed aguther closed 3 years ago

aguther commented 3 years ago

In the frame of my development of a SimConnect Toolbox library, I needed to include Windows.h and got an issue due to the naming of blockfactory::core::Log::Type.

Now I fully agree that Windows.h is broken here, but I currently don't see any other way than fixing the issue by renaming the enum value names.

This pull-request contains a proposal.

On top of this change I suggest also some additional entries for the git ignore file. They are helpfuly when using CLion or Visual Studio Code.

diegoferigo commented 3 years ago

Hi @aguther, thanks for your contribution! Even though it changes our public APIs, I'm not against accepting this PR. Downstream code typically uses the bf{Error,Warning} macros, there should not be any breaking change.

Can you please provide for future users the error that was generated without this fix and more context about your need to include Windows.h? I noticed you are using it in your headers, even though, if needed, it could be moved into your cpp files.

aguther commented 3 years ago

Hello,

you're welcome! Thank you for providing the library!

An portion of the messages are here (there are MANY) and absolutely not obvious what is going wrong:

C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(50,9): error C2059: syntax error: 'constant' [C:\Users\Andreas\git\fs2020\test\simconnect-toolbox\build\SimConnectToolbox.
vcxproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(52,5): error C2143: syntax error: missing ';' before '}' [C:\Users\Andreas\git\fs2020\test\simconnect-toolbox\build\SimCon
nectToolbox.vcxproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(52,5): error C2238: unexpected token(s) preceding ';' [C:\Users\Andreas\git\fs2020\test\simconnect-toolbox\build\SimConnec
tToolbox.vcxproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(60,1): error C2059: syntax error: 'private' [C:\Users\Andreas\git\fs2020\test\simconnect-toolbox\build\SimConnectToolbox.v
cxproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(66,1): error C2059: syntax error: 'public' [C:\Users\Andreas\git\fs2020\test\simconnect-toolbox\build\SimConnectToolbox.vc
xproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(67,10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int [C:\Users\Andreas\git\fs
2020\test\simconnect-toolbox\build\SimConnectToolbox.vcxproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(68,9): error C2588: '::~Log': illegal global destructor [C:\Users\Andreas\git\fs2020\test\simconnect-toolbox\build\SimConn
ectToolbox.vcxproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(68,12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int [C:\Users\Andreas\git\fs
2020\test\simconnect-toolbox\build\SimConnectToolbox.vcxproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(68,6): error C2610: 'int Log(void)': is not a special member function or comparison operator which can be defaulted [C:\Us
ers\Andreas\git\fs2020\test\simconnect-toolbox\build\SimConnectToolbox.vcxproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(88,53): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int [C:\Users\Andreas\git\fs
2020\test\simconnect-toolbox\build\SimConnectToolbox.vcxproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(88,53): error C2143: syntax error: missing ',' before '&' [C:\Users\Andreas\git\fs2020\test\simconnect-toolbox\build\SimCo
nnectToolbox.vcxproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(97,34): error C2270: 'getErrors': modifiers not allowed on nonmember functions [C:\Users\Andreas\git\fs2020\test\simconnec
t-toolbox\build\SimConnectToolbox.vcxproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(103,36): error C2270: 'getWarnings': modifiers not allowed on nonmember functions [C:\Users\Andreas\git\fs2020\test\simcon
nect-toolbox\build\SimConnectToolbox.vcxproj]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(119,1): error C2059: syntax error: '}' [C:\Users\Andreas\git\fs2020\test\simconnect-toolbox\build\SimConnectToolbox.vcxpro
j]
C:\Users\Andreas\git\fs2020\test\blockfactory\install\include\BlockFactory/Core/Log.h(119,1): error C2143: syntax error: missing ';' before '}' [C:\Users\Andreas\git\fs2020\test\simconnect-toolbox\build\SimCo
nnectToolbox.vcxproj]
C:\Users\Andreas\git\fs2020\test\simconnect-toolbox\src\SimConnectInput\SimConnectInput.cpp(48,7): error C2039: 'getSingleton': is not a member of 'blockfactory::core::Log' [C:\Users\Andreas\git\fs2020\test\s
imconnect-toolbox\build\SimConnectToolbox.vcxproj]

After doing some search, I found it though.

The reason why I needed to include Windows.h is that I created a Simulink library to interface with Microsoft Flight Simulator, see here: https://github.com/aguther/simconnect-toolbox. The API (called SimConnect) needs Windows.h to be included to work.

I think we should discuss two points:

Best, Andreas

traversaro commented 3 years ago

is there maybe any other solution known to fix the issue with Windows.h?

A common strategy is to always try to include Windows.h just in .cxx/.cc files (never in headers), and try to include as the last header, but clearly it is just a mitigation.

aguther commented 3 years ago

Ok, it seems I did not search deep enough. I found a solution not requiring a change in blockfactory by adding

add_definitions(-DWIN32_LEAN_AND_MEAN -DNOMINMAX -DNOGDI)

to my CMakeLists.txt. The term ERROR is defined in the GDI API which I do not need. Therefore excluding this API, it compiles and links without issues.

This info might be also helpful to others facing this problem.

diegoferigo commented 3 years ago

Awesome, I'm glad you found a workaround! I'm closing this PR, thanks for sharing the fix.