Swig记住旧的函数声明(gnuradio)。编辑函数的returntype会导致错误

2024-10-03 13:28:44 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图更改我编写的库函数的声明时,我观察到一些奇怪的行为。我想将函数calc_sequence的返回类型从int更改为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

当然,它不应该试图将其强制转换为intChannel_prediction_swig.py文件仍然错误地将calc_sequence的目标类型定义为int

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>,并产生了问题

我不确定在哪一点创建了通道预测\u 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周一1月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


Tags: bincompilerlibusrchannelcxxcalcswig
1条回答
网友
1楼 · 发布于 2024-10-03 13:28:44

我还没有追根究底,但我已经找到了一个有效的解决方案,并且找到了一个很好的解决问题的方法

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

/prefix-3.8/include/Channel_prediction/

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

相关问题 更多 >