erpc-io / eRPC

Efficient RPCs for datacenter networks
Other
851 stars 138 forks source link

compile error of "-Werror=address-of-packed-member" #65

Open skyao opened 3 years ago

skyao commented 3 years ago

I just followed the “eRPC quickstart”:

cmake . -DPERF=OFF -DTRANSPORT=infiniband; make -j; sudo ctest.

When I executed make -j;, I meet following errors:

In file included from /home/sky/work/code/erpc/eRPC/src/pkthdr.h:4,
                 from /home/sky/work/code/erpc/eRPC/src/msg_buffer.h:4,
                 from /home/sky/work/code/erpc/eRPC/src/transport.h:9,
                 from /home/sky/work/code/erpc/eRPC/src/transport_impl/infiniband/ib_transport.h:9,
                 from /home/sky/work/code/erpc/eRPC/src/transport_impl/infiniband/ib_transport_datapath.cc:3:
/home/sky/work/code/erpc/eRPC/src/transport_impl/eth_common.h: In function ‘uint16_t erpc::get_ipv4_checksum(const erpc::ipv4_hdr_t*)’:
/home/sky/work/code/erpc/eRPC/src/transport_impl/eth_common.h:183:59: error: converting a packed ‘const erpc::ipv4_hdr_t’ pointer (alignment 1) to a ‘const short unsigned int’ pointer (alignment 2) may result in an unaligned pointer value [-Werror=address-of-packed-member]
  183 |   auto* ptr16 = reinterpret_cast<const uint16_t*>(ipv4_hdr);
      |      

And this is the source code for this error in file src/transport_impl/eth_common.h:

static uint16_t get_ipv4_checksum(const ipv4_hdr_t* ipv4_hdr) {
  auto* ptr16 = reinterpret_cast<const uint16_t*>(ipv4_hdr);

I search with google and found some answers in stackoverflow about this error of address-of-packed-member:

I have tried to add -Wno-address-of-packed-member to CMAKE_CXX_FLAGS by modifying file CMakeLists.txt:

# Add additional compilation flags only after adding subprojects
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -march=native -Wall -Wextra -Werror -pedantic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-conversion -Wold-style-cast -Wno-unused-function")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types -Wno-keyword-macro -Wno-deprecated-declarations")
# add this line
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-address-of-packed-member")

Then make -j passed.

So, should it be a bug or risk that need to be fixed? if not, should we add this no-address-of-packed-member to CMAKE_CXX_FLAGS?

Some background, I run this in ubuntu 20.04 with gcc 9.3.0:

gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
anujkaliaiitd commented 3 years ago

Thanks for pointing this out! Do you know if adding this flag affects compilation with (a) older gcc versions, and (b) clang?

If you get a chance to test out that those scenarios work and submit a patch disabling this warning, I will merge it in. In any case, I'll plan to look into this eventually.

skyao commented 3 years ago

I will try it in older gcc version later and update this issue.

gshanemiller commented 2 years ago

I encountered this issue too:

$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
shenweihai1 commented 2 years ago
# Add additional compilation flags only after adding subprojects
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -march=native -Wall -Wextra -Werror -pedantic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-conversion -Wold-style-cast -Wno-unused-function")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types -Wno-keyword-macro -Wno-deprecated-declarations")
# add this line
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-address-of-packed-member")

Then make -j passed.

So, should it be a bug or risk that need to be fixed? if not, should we add this no-address-of-packed-member to CMAKE_CXX_FLAGS?

succeed in compiling eRPC after adding -Wno-address-of-packed-member