首页 > 解决方案 > Swig 记得旧的函数声明 (gnuradio)。编辑函数的返回类型会导致错误

问题描述

当我尝试更改我编写的库函数的声明时,我观察到一些奇怪的行为。我想将函数的返回类型calc_sequenceintto更改为,vector<gr_complex>但在生成 swig.py 文件时会忽略这些更改

我的标题是:

namespace gr {
  namespace Channel_prediction {

    /*!
     * \brief <+description+>
     *
     */
    class CHANNEL_PREDICTION_API Chpr_lib
    {
    public:
      Chpr_lib();
      ~Chpr_lib();
      static std::vector<gr_complex> calc_sequence(int Nzc, int M); // return type has just been changed from int
                                                 
    private:
    };

  } // namespace Channel_prediction
} // namespace gr

源文件Chpr_lib.cc是:

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/io_signature.h>
#include <Channel_prediction/Chpr_lib.h>

namespace gr {
  namespace Channel_prediction {

    Chpr_lib::Chpr_lib()
    {
    }

    Chpr_lib::~Chpr_lib()
    {
    }
    std::vector<gr_complex> Chpr_lib::calc_sequence(int Nzc, int M)
    {
      std::vector<gr_complex> sequence;
      gr_complex arg=4;
      sequence.push_back(arg);
      return sequence;

    }

  } /* namespace Channel_prediction */
} /* namespace gr */

当我make现在重新运行时,我收到这个错误,告诉我我无法投射vector<gr_complex>int

Scanning dependencies of target gnuradio-Channel_prediction
[  6%] Building CXX object lib/CMakeFiles/gnuradio-Channel_prediction.dir/Chpr_lib.cc.o
[ 13%] Linking CXX shared library libgnuradio-Channel_prediction.so
[ 20%] Built target gnuradio-Channel_prediction
[ 20%] Built target pygen_apps_9a6dd
[ 26%] Built target doxygen_target
[ 40%] Built target _Channel_prediction_swig_doc_tag
[ 53%] Built target Channel_prediction_swig_swig_doc
[ 60%] Built target Channel_prediction_swig_swig_compilation
Scanning dependencies of target Channel_prediction_swig
[ 66%] Building CXX object swig/CMakeFiles/Channel_prediction_swig.dir/CMakeFiles/Channel_prediction_swig.dir/Channel_prediction_swigPYTHON_wrap.cxx.o
/home/student/Documents/channel-prediction-with-gnuradio/OOT/gr-Channel_prediction/build/swig/CMakeFiles/Channel_prediction_swig.dir/Channel_prediction_swigPYTHON_wrap.cxx: In function ‘PyObject* _wrap_Chpr_lib_calc_sequence(PyObject*, PyObject*, PyObject*)’:
/home/student/Documents/channel-prediction-with-gnuradio/OOT/gr-Channel_prediction/build/swig/CMakeFiles/Channel_prediction_swig.dir/Channel_prediction_swigPYTHON_wrap.cxx:8640:78: error: invalid cast from type ‘std::vector<std::complex<float> >’ to type ‘int’
 8640 |       result = (int)gr::Channel_prediction::Chpr_lib::calc_sequence(arg1,arg2);
      |                                                                              ^
make[2]: *** [swig/CMakeFiles/Channel_prediction_swig.dir/build.make:63: swig/CMakeFiles/Channel_prediction_swig.dir/CMakeFiles/Channel_prediction_swig.dir/Channel_prediction_swigPYTHON_wrap.cxx.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:408: swig/CMakeFiles/Channel_prediction_swig.dir/all] Error 2
make: *** [Makefile:141: all] Error 2

当然,它不应该尝试将其转换为int. 该Channel_prediction_swig.py文件仍然错误地将目标类型定义calc_sequenceint

class Chpr_lib(object):
    """Proxy of C++ gr::Channel_prediction::Chpr_lib class."""

    thisown = _swig_property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc='The membership flag')
    __repr__ = _swig_repr

    def __init__(self):
        """__init__(gr::Channel_prediction::Chpr_lib self) -> Chpr_lib"""
        this = _Channel_prediction_swig.new_Chpr_lib()
        try:
            self.this.append(this)
        except __builtin__.Exception:
            self.this = this
    __swig_destroy__ = _Channel_prediction_swig.delete_Chpr_lib
    __del__ = lambda self: None

    def calc_sequence(Nzc: 'int', M: 'int') -> "int":
        """calc_sequence(int Nzc, int M) -> int"""
        return _Channel_prediction_swig.Chpr_lib_calc_sequence(Nzc, M)

    calc_sequence = staticmethod(calc_sequence)
Chpr_lib_swigregister = _Channel_prediction_swig.Chpr_lib_swigregister
Chpr_lib_swigregister(Chpr_lib)

def Chpr_lib_calc_sequence(Nzc: 'int', M: 'int') -> "int":
    """Chpr_lib_calc_sequence(int Nzc, int M) -> int"""
    return _Channel_prediction_swig.Chpr_lib_calc_sequence(Nzc, M)

swig 必须在某个地方注意到函数的原始声明并继续重用它。我试图删除整个构建文件夹并重建所有内容。我还通过 grep 在项目中搜索了函数声明的其他提及,但一无所获。

这里的Channel_prediction_swig.i文件:

/* -*- c++ -*- */

#define CHANNEL_PREDICTION_API

%include "gnuradio.i"           // the common stuff

//load generated python docstrings
%include "Channel_prediction_swig_doc.i"

%{
#include "Channel_prediction/zadoffchu_c.h"
#include "Channel_prediction/Chpr_lib.h"
%}

%include "Channel_prediction/zadoffchu_c.h"
GR_SWIG_BLOCK_MAGIC2(Channel_prediction, zadoffchu_c);


%include "Channel_prediction/Chpr_lib.h"

不过那里也没有魔法。希望有人以前偶然发现过这个。它很容易重新创建。Chpr_lib 已添加到带有gr_modtool add noblock. 之后,我首先添加了calc_sequence带有 returntype的函数,int编译所有内容都没有问题,并编辑了 returntype 以解决std::vector<gr_complex>产生的问题。

我不确定Channel_prediction_swig.py是在什么时候创建的。我使用cmake -DCMAKE_INSTALL_PREFIX=~/prefix-3.8 -DCMAKE_BUILD_TYPE=Debug ../然后make出现 swig.py 文件。所有的 cmake 文件都是由 gnuradio 通过 pybombs 附带的 gr_modtool 创建的。前缀 Python 版本为:3.8.5 PyBOMBS 版本 2.3.4,系统为 #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021 x86_64。

这是我在删除构建文件夹后运行 cmake 时得到的:

-- The CXX compiler identification is GNU 9.3.0
-- The C compiler identification is GNU 9.3.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Found LOG4CPP: /usr/lib/x86_64-linux-gnu/liblog4cpp.so
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- Checking for module 'gmp'
--   Found gmp, version 6.2.0
-- Found GMP: /usr/lib/x86_64-linux-gnu/libgmpxx.so  
-- Checking for module 'mpir >= 3.0'
--   No package 'mpir' found
-- Could NOT find MPIR (missing: MPIRXX_LIBRARY MPIR_LIBRARY MPIR_INCLUDE_DIR) 
-- Found MPLIB: /usr/lib/x86_64-linux-gnu/libgmpxx.so  
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found suitable version "1.71.0", minimum required is "1.71.0") found components: date_time program_options filesystem system regex thread unit_test_framework 
-- Found VOLK: Volk::volk  
-- User set python executable /usr/bin/python3
-- Found PythonInterp: /usr/bin/python3 (found version "3.8.5") 
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.8.so (found suitable exact version "3.8.5") 
-- Found Git: /usr/bin/git  
-- Found Doxygen: /usr/bin/doxygen (found version "1.8.17") found components: doxygen missing components: dot
-- Using install prefix: /home/student/prefix-3.8
-- Building for version: v1.0-compat-xxx-xunknown / 1.0.0git
-- No C++ unit zadoffchu_tapss... skipping
-- 
-- Checking for module SWIG
-- Found SWIG version 3.0.12.
-- Found SWIG: /usr/bin/swig3.0  
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.8.so (found version "3.8.5") 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/student/Documents/channel-prediction-with-gnuradio/OOT/gr-Channel_prediction/build

标签: pythonc++swiggnuradio

解决方案


我还没有找到问题的根源,但我找到了一个有效的解决方案,并且很好地引导了可以找到问题的地方。

Pybombs 安装前缀包含一个文件夹include/,其中包含来自每个模块的头文件。如果我删除文件夹并重建模块,错误就会消失:

/prefix-3.8/include/Channel_prediction/

据我了解,make 和 cmake 在构建模块时不应引用安装目录中的文件,因为它们包含过时的信息。在我手头有一点时间的情况下,我将进一步调查错误发生的确切位置。


推荐阅读