【问题标题】:Trying to build library and getting the following warning: warning: type of bit-field ‘type’ is a GCC extension [-Wpedantic] uint8_t type:1;尝试构建库并收到以下警告:警告:位域“类型”的类型是 GCC 扩展 [-Wpedantic] uint8_t type:1;
【发布时间】:2016-11-02 10:03:52
【问题描述】:

我正在尝试构建 sysrepo 库 https://github.com/sysrepo/sysrepo 作为对 travis ci 的依赖,并且遇到了位字段问题。当我在 14.04 或 16.04 的 ubuntu vm 上进行安装时,我没有遇到这个问题,并且很困惑为什么在使用 travis ci 时会出现。我安装所有内容的构建脚本如下:

#!/bin/bash
set -e
#this script installs sysrepo and all of its dependencies.
INSTALL_PREFIX_DIR=$HOME/local
export PKG_CONFIG_PATH=$INSTALL_PREFIX_DIR/lib/pkgconfig:$PKG_CONFIG_PATH
export CC=gcc

sudo apt-get update -qq
sudo apt-get install -y cmake libev-dev libavl-dev
sudo apt-get install -y build-essential
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$INSTALL_PREFIX_DIR/lib

GODIR=$PWD
cd $HOME

if [ ! -d "$INSTALL_PREFIX_DIR/lib" ]; then

  # libyang
  git clone https://github.com/CESNET/libyang.git
  cd libyang ; mkdir build ; cd build
  cmake -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX_DIR  -DCMAKE_BUILD_TYPE=Debug -DENABLE_BUILD_TESTS=OFF ..
  make -j2 && sudo make install
  cd ../..

  # protobuf
  git clone https://github.com/google/protobuf.git
  cd protobuf
  ./autogen.sh && ./configure --prefix=$INSTALL_PREFIX_DIR
  sudo make -j2
  sudo make install
  cd ..

  # protobuf-c
  git clone https://github.com/protobuf-c/protobuf-c.git
  cd protobuf-c
  ./autogen.sh && ./configure --prefix=$INSTALL_PREFIX_DIR
  sudo make -j2
  sudo make install
  cd ..

else
    echo "Using cached libraries from $INSTALL_PREFIX_DIR"
fi

echo "$INSTALL_PREFIX_DIR/lib" | sudo tee /etc/ld.so.conf.d/sysrepolibs.conf
sudo ldconfig

#sysrepo
git clone https://github.com/sysrepo/sysrepo.git
cd sysrepo
mkdir build ; cd build ;
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX:PATH=$HOME/local ..
make
sudo make install
cd ../..

sudo ldconfig

#start sysrepo
sudo sysrepod -d -l4 &> sysrepod.log &

cd $GODIR

这是在 travis 上的 before_script 中调用的,一切正常,除了我收到以下警告:

 [  3%] Building C object src/CMakeFiles/COMMON.dir/common/sysrepo.pb-c.c.o
    [  5%] Building C object src/CMakeFiles/COMMON.dir/common/sr_common.c.o
    In file included from /home/travis/local/include/libyang/libyang.h:20:0,
                     from /home/travis/sysrepo/src/common/sr_utils.h:36,
                     from /home/travis/sysrepo/src/common/sr_common.h:44,
                     from /home/travis/sysrepo/src/common/sr_common.c:28:
    /home/travis/local/include/libyang/tree_schema.h:241:5: warning: type of bit-field ‘type’ is a GCC extension [-Wpedantic]
         uint8_t type:1;                  /**< 0 - structure type used to distinguish structure from ::lys_submodule */
         ^
    /home/travis/local/include/libyang/tree_schema.h:242:5: warning: type of bit-field ‘version’ is a GCC extension [-Wpedantic]
         uint8_t version:4;               /**< yang-version:
         ^
    /home/travis/local/include/libyang/tree_schema.h:246:5: warning: type of bit-field ‘deviated’ is a GCC extension [-Wpedantic]
         uint8_t deviated:2;  

我不是构建 C 项目的专家,但如果有任何帮助将不胜感激?

【问题讨论】:

  • 唯一的问题是,你关心这些是 gcc 扩展吗? (如果你不知道这意味着什么:这段代码不会用另一个编译器编译。)
  • 代码可能缺少一些#include &lt;stdint.h&gt;
  • 如果您认为任何答案已经解决了您的问题,请考虑接受它(答案左侧的绿色复选标记)。这将向社区表明该问题已得到回答,并将给您和回答者一些声誉。

标签: c shell gcc travis-ci gnu


【解决方案1】:

C 仅支持 int、unsigned int 和 _Bool 作为位域的类型。其他类型由实现定义。

使用 -Wpedantic 标志,编译器将严格遵循标准,并在您使用某些实现定义的功能或扩展时尝试警告您。

删除标志 -Wpedantic。

【讨论】:

  • 我无法更改它在我所依赖的库的 cmake 文件中设置的标志。我无法理解的是为什么相同的代码不会在不同的机器上触发这个警告。
  • @Westy10101 也许是不同的 gcc 版本?如果您无法更改任何内容,那么您将不得不忽略看起来的警告。
  • 我检查了 gcc 版本,它们似乎匹配。我完全迷路了。它实际上并没有给我带来任何问题,它只是我不想在那里的事情之一。谢谢
【解决方案2】:

根据标准,位字段是有符号整数、无符号整数或布尔值

C11 6.7.2.1 - 第 5 节

位域的类型应该是 _Bool、signed int、unsigned int 或其他一些实现定义的类型的限定或非限定版本。是否允许原子类型由实现定义。

【讨论】:

    猜你喜欢
    • 2018-03-29
    • 2021-07-11
    • 2020-05-22
    • 2020-09-24
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    相关资源
    最近更新 更多