如何使用 C++ SWIG 中的优化标志?

发布于 2024-11-15 19:45:19 字数 1571 浏览 1 评论 0原文

我正在创建一个用 C++ 实现的 python 模块。我正在使用 SWIG 创建界面。有多种方法可以创建扩展,我使用“首选方法”,它是通过 python 的 distutils 进行描述的 此处。我的模块的名称是“ParseEvents”,为了编译它,我运行以下两个命令:

swig -c++ -python ParseEvents.i
python setup.py build_ext --inplace

第一个命令创建一个文件 ParseEvents_wrap.cxx

第二个命令使用以下 setup.py 文件:

from distutils.core import setup, Extension

ParseEvents_module = Extension('_ParseEvents',
                               sources=['ParseEvents_wrap.cxx',],
                               extra_compile_args=["-Wno-deprecated","-O3"],
                               )
setup (name = 'ParseEvents',
              ext_modules = [ParseEvents_module,],
              py_modules = ["ParseEvents"]
              )

问题:在哪里以及如何指定我希望使用 -O3 编译器标记来编译我的 C++ 代码?我猜想它只是在 setup.py 文件的“extra_compile_args”部分中,但情况似乎并非如此。当我运行第二个命令(python setup.py build_ext --inplace)时,输出如下:

running build_ext
building '_ParseEvents' extension
creating build
creating build/temp.linux-x86_64-2.6
gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fPIC -I/usr/include/python2.4 -c ParseEvents_wrap.cxx -o build/temp.linux-x86_64-2.4/ParseEvents_wrap.o -Wno-deprecated -O3
c++ -pthread -shared build/temp.linux-x86_64-2.4/ParseEvents_wrap.o -o _ParseEvents.so

请注意,-O2 和 -O3 标志都出现在输出的倒数第二行中——我想删除-O2。

I am creating a python module that is implemented in C++. I am using SWIG to create the interface. There are various ways to create the extension, I'm using the "preferred approach," which is via python's distutils and which is described here. The name of my module is "ParseEvents," and to compile it I run the following two commands:

swig -c++ -python ParseEvents.i
python setup.py build_ext --inplace

The first command creates a file ParseEvents_wrap.cxx

The second command uses the following setup.py file:

from distutils.core import setup, Extension

ParseEvents_module = Extension('_ParseEvents',
                               sources=['ParseEvents_wrap.cxx',],
                               extra_compile_args=["-Wno-deprecated","-O3"],
                               )
setup (name = 'ParseEvents',
              ext_modules = [ParseEvents_module,],
              py_modules = ["ParseEvents"]
              )

Question: Where and how do I specify that I want my C++ code to be compiled with the -O3 compiler tag? I guessed that it would just be in the "extra_compile_args" part of the setup.py file, but that doesn't seem to be the case. When I run the second command (python setup.py build_ext --inplace), here's the output:

running build_ext
building '_ParseEvents' extension
creating build
creating build/temp.linux-x86_64-2.6
gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fPIC -I/usr/include/python2.4 -c ParseEvents_wrap.cxx -o build/temp.linux-x86_64-2.4/ParseEvents_wrap.o -Wno-deprecated -O3
c++ -pthread -shared build/temp.linux-x86_64-2.4/ParseEvents_wrap.o -o _ParseEvents.so

Note that both the -O2 and -O3 flags are present in the second to last line in the output---I'd like to remove the -O2.

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

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

发布评论

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

评论(2

薄荷梦 2024-11-22 19:45:19

GCC 文档明确指出:

http://gcc.gnu .org/onlinedocs/gcc-4.1.2/gcc/Optimize-Options.html

如果您使用多个 -O 选项,无论有或没有级别编号,最后一个此类选项就是有效。

这意味着您的代码将按照您想要的方式使用 -O3 进行编译。无需担心重复的优化标志。

The GCC doc explicitly says:

http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Optimize-Options.html

If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.

This means your code will be compiled with -O3 in effect, just as you want it. No need to bother for duplicate optimization flags.

海拔太高太耀眼 2024-11-22 19:45:19

Distutils 具有一个可爱的功能,可以提供与 Python 编译时使用的所有相同的标志。结果是添加额外的标志很容易,但删除它们却非常痛苦。这样做涉及到编译器类的子类化、捕获参数并从编译函数使用的参数列表中手动删除有问题的标志。无论如何,这就是理论,文档太差了,无法真正指导您完成必须做的事情才能实现这一点。

但就像路德所说,在你的情况下,额外的 -O2 不会造成任何伤害。

Distutils has the lovely feature of providing all the same flags that Python was compiled with. The result is that adding extra flags is easy, but removing them is a total pain. Doing so involves subclassing the compiler class, catching the arguments and manually removing the offending flag from the argument list used by the compile function. That's the theory anyway, the docs are too poor to actually guide you through what you have to do to make that happen.

But like Luther said, in your case the extra -O2 doesn't hurt anything.

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