用于Raspberry Pi CEC代码的SWIG产生未定义的符号错误

时间:2014-07-01 04:27:31

标签: python c++ linux raspberry-pi swig

我想在Python和Raspberry Pi的CEC代码之间创建一个接口(例如,vc_cecservice.h)。我创建了一个小的C ++文件来开始这个界面。

mycec.cpp:

#include <cstdio>

#include <interface/vmcs_host/vc_cecservice.h>
#include <interface/vchiq_arm/vchiq_if.h>

int openClose() {
  VCHI_INSTANCE_T vchiq_instance;
  int res = vchi_initialise(&vchiq_instance);
  if (res != VCHIQ_SUCCESS) {
    printf("failed to open vchiq instance\n");
    return -1;
  }
  if (vchi_connect(NULL, 0, vchiq_instance) != 0) {
    printf( "VCHI connection failed\n" );
    return -1;
  }

  VCHI_CONNECTION_T *vchi_connection;
  vc_vchi_cec_init(vchiq_instance, &vchi_connection, 1);

  vc_vchi_cec_stop();

  return 0;
}

我正在使用SWIG为这个C ++文件生成CPython。

mycec.i:

%module mycec
%{
extern int openClose();
%}

extern int openClose();

我在Raspberry Pi上运行以下bash脚本来编译代码。

build.sh:

#!/bin/bash

swig -python mycec.i

g++ -c -fpic -L=/opt/vc/lib \
    -I=/usr/include/python2.7 \
        -I=/opt/vc/include/interface/vcos/pthreads -I=/opt/vc/include \
    mycec.cpp mycec_wrap.c

g++ -shared -Wl,--no-as-needed -L=/opt/vc/lib \
    -I=/usr/include/python2.7 \
        -I=/opt/vc/include/interface/vcos/pthreads -I=/opt/vc/include \
    -o _mycec.so mycec.o mycec_wrap.o \
    -lbcm_host -lvcos -lvchiq_arm

然后我尝试在python中加载它:

>>> import _mycec
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: ./_mycec.so: undefined symbol: _Z16vc_vchi_cec_initP29opaque_vchi_instance_handle_tPP17vchi_connection_tj

解码名称为vc_vchi_cec_init(opaque_vchi_instance_handle_t*, vchi_connection_t**, unsigned int)

$ grep vc_vchi_cec_init /opt/vc/lib/libbcm_host.so
Binary file /opt/vc/lib/libbcm_host.so matches.

感谢。

1 个答案:

答案 0 :(得分:0)

我在extern "C"周围失踪了#includes。这有效:

#include <cstdio>

extern "C" {
#include <interface/vmcs_host/vc_cecservice.h>
#include <interface/vchiq_arm/vchiq_if.h>
}

// rest of program...