HilscherAutomation / nxdrvlinux

cifX device driver for linux.
https://www.hilscher.com
GNU General Public License v2.0
13 stars 3 forks source link

min definition in cifxToolkit.h causes compilation errors #5

Closed wlfbck closed 3 months ago

wlfbck commented 3 months ago

Because of the min definition in cifxToolkit.h:

#ifndef min
  #define min(a,b)  ((a > b)? b : a)
#endif

we are getting compilation problems because of this:

In file included from /usr/include/c++/12/bits/specfun.h:45,
                 from /usr/include/c++/12/cmath:1935,
                 from /usr/include/c++/12/math.h:36,
                 from EthercatComponents.cpp:48,
                 from RTComponents.cpp:929:
/usr/include/c++/12/bits/stl_algobase.h:278:56: error: macro "min" passed 3 arguments, but takes just 2
  278 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |                                                        ^
In file included from /media/main/austausch/Firmware/libcifx/libcifx/cifxlinux_internal.h:27,
                 from BigPBSlaveInstance.h:8,
                 from BigPBSlaveComponents.h:3,
                 from RTComponents.cpp:34:
/media/main/austausch/Firmware/libcifx/libcifx/Toolkit/Source/cifXToolkit.h:41: note: macro "min" defined here
   41 |   #define min(a,b)  ((a > b)? b : a)
      |
In file included from /usr/include/c++/12/bits/specfun.h:46:
/usr/include/c++/12/limits:317:11: error: macro "min" requires 2 arguments, but only 1 given
  317 |       min() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }
      |           ^
/media/main/austausch/Firmware/libcifx/libcifx/Toolkit/Source/cifXToolkit.h:41: note: macro "min" defined here
   41 |   #define min(a,b)  ((a > b)? b : a)
      |
/usr/include/c++/12/limits:389:11: error: macro "min" requires 2 arguments, but only 1 given
  389 |       min() _GLIBCXX_USE_NOEXCEPT { return false; }
      |           ^

And several dozens more in various parts of the standard library.

I noticed that in our previous, very old, version of libcifx (1.0.0) this min define wasn't present yet.

Googling macro "min" passed 3 arguments, but takes just 2 gives me a lot of results to not do that define. So is it necessary? Can it possibly be removed?

(Sorry, usually we are not doing a lot of work in C++)

sdoell-hilscher commented 3 months ago

Hello, even though the include order may fix it, including the file cifXToolkit.h from your application should not be necessary. It's a toolkit internal file. It seems it's included by cifxlinux_internal.h (which is as well a driver internal) which is included by BigPBSlaveInstance.h. So please remove the include of cifxlinux_internal.h in BigPBSlaveInstance.h.

wlfbck commented 3 months ago

Oh, good catch. Removing those (completely unused) includes of cifxlinux_internal.h solved the issue :)

Just out of curiosity, why does that define exist? Isn't that the same as the standard min?

sdoell-hilscher commented 3 months ago

To guarantee the most flexability the toolkit relies only on the minimal set of the standard C (ISO/IEC 9899) and there is no min/max function or macro. Some c libraries may provide fmin/fmax but they are used for floating point numbers.