Compilation error for HW3 init.cpp module on MacOS

Compilation error for HW3 init.cpp module on MacOS

by Christian DiMaria -
Number of replies: 4

Hi everyone,

I am encountering a compiler error when I try to build the init module for Assignment 3. The error message says that the "call to 'abs' is ambiguous", with 'abs' being the absolute value function on lines 29, 30, and 31 of init.cpp. Since we are not supposed to change the modules for this assignment, I am not quite sure how to proceed. Has anyone else encountered this error and if so, how did you resolve it?

Thanks in advance. I've included the full error message from Make below:

c++ -std=c++17 -O3 -Wall -g   -c -o init.o init.cpp

init.cpp:29:38: error: call to 'abs' is ambiguous

    } else if ( (i1==i2 && j1==j2 && abs(k1-k2)==1)

                                     ^~~

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdlib.h:132:6: note: candidate function

int      abs(int) __pure2;

         ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:111:39: note: candidate function

inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT {

                                      ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:114:44: note: candidate function

inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {

                                           ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:120:40: note: candidate function

inline _LIBCPP_INLINE_VISIBILITY float abs(float __lcpp_x) _NOEXCEPT {

                                       ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:124:41: note: candidate function

inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT {

                                        ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:129:1: note: candidate function

abs(long double __lcpp_x) _NOEXCEPT {

^

init.cpp:30:31: error: call to 'abs' is ambiguous

                || (i1==i2 && abs(j1-j2)==1 && k1==k2)

                              ^~~

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdlib.h:132:6: note: candidate function

int      abs(int) __pure2;

         ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:111:39: note: candidate function

inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT {

                                      ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:114:44: note: candidate function

inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {

                                           ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:120:40: note: candidate function

inline _LIBCPP_INLINE_VISIBILITY float abs(float __lcpp_x) _NOEXCEPT {

                                       ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:124:41: note: candidate function

inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT {

                                        ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:129:1: note: candidate function

abs(long double __lcpp_x) _NOEXCEPT {

^

init.cpp:31:21: error: call to 'abs' is ambiguous

                || (abs(i1-i2)==1 && j1==j2 && k1==k2) ) {

                    ^~~

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdlib.h:132:6: note: candidate function

int      abs(int) __pure2;

         ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:111:39: note: candidate function

inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT {

                                      ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:114:44: note: candidate function

inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {

                                           ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:120:40: note: candidate function

inline _LIBCPP_INLINE_VISIBILITY float abs(float __lcpp_x) _NOEXCEPT {

                                       ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:124:41: note: candidate function

inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT {

                                        ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/stdlib.h:129:1: note: candidate function

abs(long double __lcpp_x) _NOEXCEPT {

^

3 errors generated.

Make: *** [init.o] Error 1

In reply to Christian DiMaria

Re: Compilation error for HW3 init.cpp module on MacOS

by Christian DiMaria -
Just a quick update, I couldn't figure it out on my Mac, but it works on Niagara so I'll just work on the cluster for the rest of this assignment.
In reply to Christian DiMaria

Re: Compilation error for HW3 init.cpp module on MacOS

by Ramses van Zon -
It's definitely Mac specific, or more precisely, it's specific to the clang compiler. To use g++ on a Mac, you can try one of compilers listed here: https://hpc.sourceforge.net/. Or try installing it with homebrew.
In reply to Ramses van Zon

Re: Compilation error for HW3 init.cpp module on MacOS

by Aiyan Brown -
I also got the same issue on my Windows machine with the ucrt64 gcc compiler. As Christian mentioned below, you can workaround by specifying using "std::fabs" instead or by casting the unsigned values (e.g., static_cast(k1), etc.). Not sure if there is a more elegant solution than this
In reply to Christian DiMaria

Re: Compilation error for HW3 init.cpp module on MacOS

by Christian DiMaria -
Alternatively, changing the aforementioned calls to "abs" to "std::fabs" worked on my Mac.