SWIG 错误:被声明为“extern”以及后来的“静态”

发布于 2024-11-14 15:58:54 字数 983 浏览 3 评论 0原文

我正在使用 swig 生成 python C++ 接口,以便我可以在 python 中编写脚本来调用我的 c++ 函数。我已经得到了:

swig -c++ python filename.i

gcc -c filename.cpp filename_wrap.cxx -shared -Ic:\includepath

其中包含路径是我的 python27 的包含目录。

在尝试使用 gcc 进行编译时,我收到了一大堆错误,指出我的许多函数被声明为 extern 而现在是静态的。在原始的 cpp 文件中,我已将一些函数声明为静态的。我从未将任何东西声明为外部的。这可能是由什么引起的?

这是我的接口文件

/*  Interface */
%module ReadWrite
%{
    #include "ReadWrite.h"

%}
%include "ReadWrite.h"

头文件的片段(名称已更改)看起来像这样(没有任何内容被声明为 extern)

static bool myfunc1(void);

static bool myfunc2 (type1 *Entry, type2 *Block, type2 *Data);

static bool myfunc3 (type2 *Data, type3 **Records, type2 **Header);

type4 myfunc4 (void) ;

当我执行 gcc -c ReadWrite.cpp ReadWrite_wrap.cxx -shared -Ic:\includepath 时,我会收到错误就像来自海湾合作委员会:

ReadWrite.cpp:682:79: 错误:“bool myfunc3 (type2 *Data, type3 **Records, type2 **Header)”被声明为“extern”,后来声明为“static”

I'm using swig to produce a python C++ interface such that I can make scripts in python to call my c++ functions. I've gotten as far as:

swig -c++ python filename.i

gcc -c filename.cpp filename_wrap.cxx -shared -Ic:\includepath

Where the include path is my python27's include directory.

Upon attempting to compile with gcc, I get a whole slew of errors stating that many of my functions were declared as extern and now static. In the original cpp file, I had declared some of my functions as static. I have never declared anything as extern. What could this be caused by?

This is my interface file

/*  Interface */
%module ReadWrite
%{
    #include "ReadWrite.h"

%}
%include "ReadWrite.h"

A snippet of header file (names changed) looks like this (nothing is declared as extern)

static bool myfunc1(void);

static bool myfunc2 (type1 *Entry, type2 *Block, type2 *Data);

static bool myfunc3 (type2 *Data, type3 **Records, type2 **Header);

type4 myfunc4 (void) ;

When I do a gcc -c ReadWrite.cpp ReadWrite_wrap.cxx -shared -Ic:\includepath, I'll get errors like from gcc:

ReadWrite.cpp:682:79: error: 'bool myfunc3 (type2 *Data, type3 **Records, type2 **Header)' was declared 'extern' and later 'static'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚气少女 2024-11-21 15:58:54

事实上,它们作为原型出现在标头中,隐式地将它们定义为外部函数。但是,您还声明它们是静态的。

考虑到 C++ 标签,我将在这里做出一个假设,所以如果您已经知道这一点并且它看起来像是居高临下,请原谅我。 C 中的static 的作用与 C++ 完全不同。在 C++ 中,静态意味着特定方法不属于类的实例,任何人都可以随时调用。在 C 语言中,static 关键字的意思是“该函数仅在此文件的范围内可见”。因此,当您声明某个静态内容时,您基本上是在禁止该文件之外的任何人使用它(将其视为 private 的 C 等效项)。

因此,如果这不是您的意图,请不要将其声明为静态。

The fact that they appear in a header as a prototype implicitly define them as an extern function. However you're ALSO declaring them as static.

Given the C++ tag, I'm going to make an assumption here, so please pardon me if you already know this and it comes off as patronizing. static in C does something completely different than C++. In C++, static means that a particular method doesn't belong to an instance of a class, and can be called by anyone at any time. In C, the static keyword means, "this function is only visible within the scope of this file". So when you declare something static, you're basically forbidding anyone outside of that file from using it (think of it as the C equivalent of private).

So if that's not your intention, don't declare it static.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文