open-mpi / hwloc

Hardware locality (hwloc)
https://www.open-mpi.org/projects/hwloc
Other
577 stars 174 forks source link

support building hwloc using a C++11 compiler #362

Open jeffhammond opened 5 years ago

jeffhammond commented 5 years ago

I tried to build MPICH with the Intel C++ compiler . The configure step fails in hwloc configure, specifically the C99 step. I extracted the configure test and reproduced the errors (see below).

Only two changes are required to get this code to compile as C++:

  1. s/restrict/__restrict__/g
  2. Add the explicit cast for this malloc:
struct incomplete_array *ia = (struct incomplete_array *) malloc (sizeof (struct incomplete_array) + (sizeof (double) * 10));

I don't see any usage of restrict in hwloc and explicit casting of malloc, while potentially obnoxious, doesn't break anything.

Is C++ compilation something that you are willing to support, if the changes were provided? This appears to require quite a few explicit casts on malloc.

$ icpc  -c -std=gnu++11 -I/Users/jrhammon/Work/MPI/MPICH/git/build-icpc-c11/src/mpl/include -I/Users/jrhammon/Work/MPI/MPICH/git/src/mpl/include -I/Users/jrhammon/Work/MPI/MPICH/git/src/openpa/src -I/Users/jrhammon/Work/MPI/MPICH/git/build-icpc-c11/src/openpa/src  hwloc-c99.c 
hwloc-c99.c(118): error: expected a ")"
  test_restrict (ccp restrict text)
                              ^

hwloc-c99.c(123): error: identifier "text" is undefined
    for (unsigned int i = 0; *(text+i) != '\0'; ++i)
                               ^

hwloc-c99.c(172): error: expected a ";"
    char *restrict newvar = "Another string";
                   ^

hwloc-c99.c(180): error: a value of type "void *" cannot be used to initialize an entity of type "incomplete_array *"
      malloc (sizeof (struct incomplete_array) + (sizeof (double) * 10));
      ^

hwloc-c99.c(198): error: identifier "newvar" is undefined
    return (!success || bignum == 0LL || ubignum == 0uLL || newvar[0] == 'x'
                                                            ^

compilation aborted for hwloc-c99.c (code 2)
bgoglin commented 5 years ago

Hello Jeff. Sorry I completely forgot about this issue. I am not sure I understand what replacing restrict with restrict means, can you clarify? That said, this restrict seems to come from AC_PROG_CC_C99, I am not sure we want to duplicate/change that macro. I don't think I would be against adding some explicit mallocs, but I guess we'd need to add quite a lot of them. When configuring with gcc and building with CC=g++-8, I see quite a lot of other C++-related errors (arithmetic on enums, etc).

mratsim commented 5 years ago

I'm pretty sure it was Github markdown eagerly transforming __restrict__ into restrict.

Note that for windows support, the correct keyword is __restrict for Visual Studio, but I think GCC and Clang also accept that one.