-
PreamblePresently, we have some mixed configurations in our software to deal with math constants such as
This way, we might have the following warning, depending on the inclusion order:
I'd suggest to always stick to solution 2, which is way more principled. However, the use of One way to get around this order-related problem is to put in the add_definitions(-D_USE_MATH_DEFINES) ProblemWith these premises, if I aim to update for example Do we have an automatic way in |
Beta Was this translation helpful? Give feedback.
Replies: 23 comments
-
This is tricky. Regarding the For example this is the code in my /******************************************************************************
* POSIX/UNIX extensions to the C standard *
******************************************************************************/
#if __DARWIN_C_LEVEL >= 199506L
/* Even though these might be more useful as long doubles, POSIX requires
that they be double-precision literals. */
...
#define M_PI 3.14159265358979323846264338327950288 /* pi */
#define M_PI_2 1.57079632679489661923132169163975144 /* pi/2 */
... I still have to understand that |
Beta Was this translation helpful? Give feedback.
-
Uhm, in Windows Yes, those constants are not in the C/C++ standard, that's true, but at least relying on |
Beta Was this translation helpful? Give feedback.
-
I completely agree on avoiding redefining them as if they are present on windows, then they are present also on unix :) well.. if windows says that, I think we have to do that. And one additional define won't cause any problem on the other systems. 👍 then on exporting from the ctrlLib |
Beta Was this translation helpful? Give feedback.
-
The An The minimum CMake version that we need to support is the 2.8.9 (see http://wiki.icub.org/wiki/YARP_Supported_Distributions) but Windows users normally have a recent version of CMake, given that they are not tied to the "system" version of CMake. I think a sensitive solution would be to add the Random uses of |
Beta Was this translation helpful? Give feedback.
-
Sounds great, thanks @traversaro |
Beta Was this translation helpful? Give feedback.
-
I think ctrllib should not export symbols in the main namespace (i remember advising against this years ago.. :) ). modules that need those symbols should take them from cmath. Maybe for transition you could define those symbols in a specific namespace. |
Beta Was this translation helpful? Give feedback.
-
+1 to @lornat75 reply. What's the reason for having If you really need to do it, @traversaro reply is the way to do it with CMake 2.8.11 or later, but for CMake 2.8.9 you should also add some if(MSVC)
cmake_minimum_required(VERSION 2.8.11)
else()
cmake_minimum_required(VERSION 2.8.9)
endif() |
Beta Was this translation helpful? Give feedback.
-
I think the problem is that there are library header files that include cmath and that the latter publicly define M_PI etc, For this reason you have to fight the order of inclusion using preprocessors flags (-D_USE_MATH_DEFINES). It would be preferable to remove inclusion of cmath from library public header files, in this way all modules could safely do: #define _USE_MATH_DEFINES
#include <cmath> If ctrllib need to export quantities like M_PI etc it should define them as constants and hide them in the ctrllib namespace; modules that depend from ctrllib including iKin could use these symbols. It implies some code change but in the long term it is a better solution because it removes stray preprocessor symbols that can cause clashes in user code. |
Beta Was this translation helpful? Give feedback.
-
@lornat75 and how would you deal with for example cases like this: https://github.com/robotology/icub-main/blob/master/src/libraries/iKin/include/iCub/iKin/iKinFwd.h#L143? |
Beta Was this translation helpful? Give feedback.
-
A possible way would be to duplicate the default constructor, or duplicate whatever other method that would need to use Btw, it's true that |
Beta Was this translation helpful? Give feedback.
-
If I understood correctly @lornat75, I think he suggested something like: // sort of constant.h or a main header for ctrlLib
namespace iCub {
namespace ctrl {
extern const double pi;
}
}
// in the .cpp
#define _USE_MATH_DEFINES
#include <cmath>
namespace iCub {
namespace ctrl {
const double pi = M_PI;
}
} And then use |
Beta Was this translation helpful? Give feedback.
-
I can certainly give a go to this then. Anyway, |
Beta Was this translation helpful? Give feedback.
-
Yes, this is what I had in mind. I see there are still references to: if(MSVC)
add_definitions(-D_USE_MATH_DEFINES)
endif() which should be no longer needed (except for compilation of |
Beta Was this translation helpful? Give feedback.
-
Are you referring to PR robotology/icub-main#259? Those references are no longer needed to compile against The idea behind was to achieve the following:
Then, I believe it should be more appropriate not to propagate the use of What do you think? |
Beta Was this translation helpful? Give feedback.
-
All good. In 2 you mean iKin refers to ctrl::M_PI? I believe it should be safe to use M_PI in .c .cpp files like this: #define _USE_MATH_DEFINES
#include <cmath> If it works this simplifies CMake code and it makes the code more self-contained. If it works I would always define I think there is no problem if modules use Anyway these are minor issues. |
Beta Was this translation helpful? Give feedback.
-
Yes, I started off defining No problem for removing |
Beta Was this translation helpful? Give feedback.
-
Condition |
Beta Was this translation helpful? Give feedback.
-
I thought order of inclusions problems would be solved by removing cmath from ctrlib public headers. strange this is not the case, maybe cmath is included by other libraries? After reading your link it scares me that M_PI is not standard and for example is NOT defined in Linux if -std=c++11 is used (if we trust the information from one post there). In the future we could have problems. In the end it seems better if users use ctrlib::M_PI instead. |
Beta Was this translation helpful? Give feedback.
-
Why don't we move the |
Beta Was this translation helpful? Give feedback.
-
@lornat75 At the moment, However, I'd like to point out that the use of @drdanz |
Beta Was this translation helpful? Give feedback.
-
I agree. |
Beta Was this translation helpful? Give feedback.
-
Cool. I'd say I could keep this ticket open for future reference and in the meantime I can proceed with the PR. |
Beta Was this translation helpful? Give feedback.
-
Hi everyone, I am a time traveler from the future. In 2020, |
Beta Was this translation helpful? Give feedback.
@lornat75
cmath
(ormath.h
) is included in other headers, yes, in yarp as well, as I was saying before, eventually causing inclusion order troubles.At the moment,
ctrlLib
declares its ownCTRL_PI
equal toM_PI
; so the problem would remain, unless we switch to the use of some trigonometric computations to getPI
, which need to be carried out in a sort of init function, though.However, I'd like to point out that the use of
M_PI
is ubiquitous in our code (yarp
,icub-main
, other sister projects) and has nothing to deal withctrlLib
. Therefore, I'd rather avoid tackling this problem within this PR. I think we could conveniently merge the PR and then go on with further considerations.@drdanz
PI