Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation warning: ISO C++ prohibits anonymous structs #2303

Closed
juzzlin opened this issue May 8, 2018 · 14 comments
Closed

Compilation warning: ISO C++ prohibits anonymous structs #2303

juzzlin opened this issue May 8, 2018 · 14 comments
Labels
kind: compile error Type of issue needs: author reply Specify why not closed/merged yet status: stale

Comments

@juzzlin
Copy link

juzzlin commented May 8, 2018

Your Environment

  • Operating System and version: Ubuntu 18.04 LTS
  • Compiler: gcc version 5.5.0 20171010 (Ubuntu 5.5.0-12ubuntu1)
  • PCL Version: 1.8.1

Context

I'm compiling my code with -pedantic and GCC gives a warning of a struct in /usr/local/include/pcl-1.8/octree/octree_key.h:

union
  {
    struct
    {
      uint32_t x;
      uint32_t y;
      uint32_t z;
    };
    uint32_t key_[3];
  };

Message: " ISO C++ prohibits anonymous structs" so I guess this is non-standard C++.

Expected Behavior

No warnings.

Current Behavior

Warning "ISO C++ prohibits anonymous structs"

Code to Reproduce

#include <pcl/octree/octree.h>
int main() {return 0;}

Compile with -pedantic

Possible Solution

Don't use anonymous structs..?

@Morwenn
Copy link
Contributor

Morwenn commented Aug 19, 2019

Theoretically it would be possible to avoid it with an array and three reference members initialized to point to the elements of the array in order to remain strictly standard-compliant without changing the the structure API, but reference members increase the size of the class.

@stale
Copy link

stale bot commented May 19, 2020

Marking this as stale due to 30 days of inactivity. It will be closed in 7 days if no further activity occurs.

@stale stale bot added the status: stale label May 19, 2020
@kunaltyagi
Copy link
Member

Until C++ comes with a better method for type-punning than the one in C, PCL will have to rely on this UB. Even though this is strictly a UB, all compilers we test with provide the same behavior as a C code.

Alternatives that don't increase memory size (reference) or require breaking API (using functions everywhere) are welcome. I now memcpy trick (copying the input into source type) is optimized out by most compilers and is the recommended method of looking into a struct, but that can't be done for data members.

There's not much that's actionable here. Sorry.

@stale stale bot removed the status: stale label May 19, 2020
@kunaltyagi kunaltyagi added kind: bug Type of issue needs: author reply Specify why not closed/merged yet kind: compile error Type of issue and removed kind: bug Type of issue labels May 19, 2020
@stale
Copy link

stale bot commented Jun 20, 2020

Marking this as stale due to 30 days of inactivity. It will be closed in 7 days if no further activity occurs.

@stale stale bot added the status: stale label Jun 20, 2020
@kunaltyagi
Copy link
Member

GCC users can disable this warning using -fms-extensions. Sadly, this stays for current PCL

@SergioRAgostinho
Copy link
Member

I'm not sure this is considered UB as of C++11. The user compiled pcl 1.8.1 which is in C++03. According to cpp reference, anonymous structs are a part of the standard from C++11 onwards
https://en.cppreference.com/w/c/language/struct

  1. Similar to union, an unnamed member of a struct whose type is a struct without name is known as anonymous struct. Every member of an anonymous struct is considered to be a member of the enclosing struct or union. This applies recursively if the enclosing struct or union is also anonymous.
  2. Similar to union, the behavior of the program is undefined if struct is defined without any named members (including those obtained via anonymous nested structs or unions).
  1. does not apply to us. So the fact that we use an anonymous struct should not be UB anymore.

@Morwenn
Copy link
Contributor

Morwenn commented Jun 29, 2020

Using anonymous structs is legal, it's assigning to the struct members then accessing them the array which is still considered UB today.

@MattSYoung
Copy link

MattSYoung commented Jun 21, 2021

I've recently encountered this problem when trying to include octree_key.h in Ubuntu 20.04 with ROS2 and PCL 1.10. The specific error I get is:

/usr/include/pcl-1.10/pcl/octree/octree_key.h:155:9: error: ISO C++ prohibits anonymous structs [-Werror=pedantic]
  155 |         };
      |         ^
cc1plus: all warnings being treated as errors

I haven't yet figured out how to compile my code with -fms-extensions set. But so far I've tried:

  1. Using #pragma GCC diagnostic warning "-pedantic" and similar
  2. Compiling my code in C++14
  3. Adding set(CMAKE_CXX_FLAGS "-fms-extensions") to my CMakeLists file

And none of those have gotten me past this compilation error. Can anyone give me more specific instructions on how to set -fms-extensions or another solution that fixes this issue?

@lockunkey
Copy link

lockunkey commented Dec 30, 2021

I've recently encountered this problem when trying to include octree_key.h in Ubuntu 20.04 with ROS2 and PCL 1.10. The specific error I get is:

/usr/include/pcl-1.10/pcl/octree/octree_key.h:155:9: error: ISO C++ prohibits anonymous structs [-Werror=pedantic]
155 | };
| ^
cc1plus: all warnings being treated as errors

I haven't yet figured out how to compile my code with -fms-extensions set. But so far I've tried:

Using #pragma GCC diagnostic warning "-pedantic" and similar
Compiling my code in C++14
Adding set(CMAKE_CXX_FLAGS "-fms-extensions") to my CMakeLists file

And none of those have gotten me past this compilation error. Can anyone give me more specific instructions on how to set -fms-extensions or another solution that fixes this issue?

i too have the same issue can anyone tell me how to solve this

@ajay1606
Copy link

ajay1606 commented Jan 14, 2022

Hello @MattSYoung @lockunkey
In case if you found any fix o above error, could you please share here. My case also getting same error.

Ubuntu : 20.04
PCL: 1.10
ROS2: galactic

/usr/include/pcl-1.10/pcl/octree/octree_key.h:155:9: error: ISO C++ prohibits anonymous structs [-Werror=pedantic]
  155 |         };

Appreciate any response.

@kunaltyagi
Copy link
Member

I think the CMake files for you applications are wrong. They might have something like: include_directories(${PCL_INCLUDE_DIRS}) or include_directories(${ament_INCLUDE_DIRS}). Please ensure PCL is considered a system library

@ajay1606
Copy link

@kunaltyagi
Thanks for your quick response, Anyhow I tried with commenting the line include_directories(${PCL_INCLUDE_DIRS}) But still getting the same error

cmake_minimum_required(VERSION 3.5)
project(pointcloud_preprocessor)

### Compile options
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
  set(CMAKE_CXX_EXTENSIONS OFF)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic -Werror)
endif()

# Ignore PCL errors in Clang
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wno-gnu-anonymous-struct -Wno-nested-anon-types)
endif()

find_package(ament_cmake_auto REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(Boost REQUIRED)
find_package(PCL REQUIRED)
find_package(pcl_conversions REQUIRED)
find_package(OpenMP)
ament_auto_find_build_dependencies()


###########
## Build ##
###########

include_directories(
  include
  ${Boost_INCLUDE_DIRS}
  ${PCL_INCLUDE_DIRS}
  ${EIGEN3_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
  ${GRID_MAP_INCLUDE_DIR}
)

Above is the actual CMake File, where am getting errors. Would you please suggest me proper configuration?

Regards,
Ajay

@kunaltyagi
Copy link
Member

include_directories(
  include
)
include_directories(SYSTEM
  ${Boost_INCLUDE_DIRS}
  ${PCL_INCLUDE_DIRS}
  ${EIGEN3_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
  ${GRID_MAP_INCLUDE_DIR}
)

@ajay1606
Copy link

ajay1606 commented Jan 15, 2022

@kunaltyagi Thanks so much for your quick response, it built successfully now.

shu-beg pushed a commit to bosch-engineering/off_highway_sensor_drivers that referenced this issue Apr 10, 2024
Anonymous structs are used in PCL for type punning:
PointCloudLibrary/pcl#2303

Subobject braces are not needed:
https://bugs.llvm.org/show_bug.cgi?id=21629
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25137

Signed-off-by: Robin Petereit <robin.petereit@de.bosch.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: compile error Type of issue needs: author reply Specify why not closed/merged yet status: stale
Projects
None yet
Development

No branches or pull requests

7 participants