安装 pynauty 时出错:“错误:命令‘x86_64-linux-gnu-gcc’失败,退出状态为 1”

安装 pynauty 时出错:“错误:命令‘x86_64-linux-gnu-gcc’失败,退出状态为 1”

我正在尝试安装皮诺蒂Python 包。当我运行 时python setup.py build,出现以下错误。我已经安装了依赖项。我该如何成功安装此 Python 包?

$ python setup.py build
running build
running build_py
running build_ext
building 'pynauty._pynauty' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Inauty -Isrc -I/usr/include/python2.7 -c src/pynauty.c -o build/temp.linux-x86_64-2.7/src/pynauty.o -O4
In file included from src/pynauty.c:16:0:
nauty/nauty.h:40:0: warning: "_FILE_OFFSET_BITS" redefined
 #define _FILE_OFFSET_BITS 0
 ^
In file included from /usr/include/python2.7/pyconfig.h:3:0,
                 from /usr/include/python2.7/Python.h:8,
                 from src/pynauty.c:15:
/usr/include/x86_64-linux-gnu/python2.7/pyconfig.h:1157:0: note: this is the location of the previous definition
 #define _FILE_OFFSET_BITS 64
 ^
src/pynauty.c: In function ‘_make_nygraph’:
src/pynauty.c:363:33: warning: passing argument 2 of ‘PyDict_Next’ from incompatible pointer type
     while (PyDict_Next(adjdict, &i, &key, &adjlist)) {
                                 ^
In file included from /usr/include/python2.7/Python.h:101:0,
                 from src/pynauty.c:15:
/usr/include/python2.7/dictobject.h:114:17: note: expected ‘Py_ssize_t *’ but argument is of type ‘int *’
 PyAPI_FUNC(int) PyDict_Next(
                 ^
src/pynauty.c: In function ‘init_pynauty’:
src/pynauty.c:537:15: warning: variable ‘m’ set but not used [-Wunused-but-set-variable]
     PyObject *m;
               ^
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl,-z,relro -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/src/pynauty.o nauty/nauty.o nauty/nautil.o nauty/naugraph.o -o build/lib.linux-x86_64-2.7/pynauty/_pynauty.so
/usr/bin/ld: nauty/nauty.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
nauty/nauty.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

答案1

完整的错误消息告诉您解决方案的一部分:您需要使用该-fPIC标志来编译 nauty。

  1. 下载 nauty,解压,进入目录
  2. CFLAGS=-fPIC ./configure
  3. make
  4. 下载 pynauty,解压,进入目录
  5. ln -s ../nauty* nauty
  6. 但是,pynauty 的setup.py文件似乎配置错​​误:它不包含正确的 nauty 文件,因此如果此时构建和安装,导入将失败。修改(第 37 行)extra_objects中的参数:setup.py

    # from
    extra_objects = [ nauty_dir + '/' + 'nauty.so', ],
                          nauty_dir + '/' + 'nautil.o',
                          nauty_dir + '/' + 'naugraph.o'
                        ],
    # change it to
    extra_objects = [nauty_dir + '/' + 'nauty.a'],
    
  7. python setup.py build

  8. python setup.py install(如果你要安装到系统,请使用sudo,但最好使用虚拟环境反而)

虽然包现在已经安装并可导入,但整个测试套件失败。这是一个应该由库维护者修复的问题;请考虑向他们报告该错误。

答案2

不,请不要这样做!pynauty 的作者 Peter Dobcsányi 告诉我,这个版本本质上是有缺陷的,并且不会产生正确的结果(特别是在“认证”功能中)。

他最近发布了全新版本 0.6.0(https://web.cs.dal.ca/~peter/software/pynauty/pynauty-0.6.0.tar.gz) 并且此版本绝对正确并且与 Python 2.7 和最新的 Nauty 兼容!

我已经测试过了,一切正常

答案3

除了 davidism 的回答之外,我还发现测试被破坏了,因为我在使用 Python 2.7 和 nauty25r9 编译 pynauty 0.5 时收到了一个警告:

src/pynauty.c: In function ‘_make_nygraph’:
src/pynauty.c:363:33: warning: passing argument 2 of ‘PyDict_Next’ from incompatible pointer type
     while (PyDict_Next(adjdict, &i, &key, &adjlist)) {
                                 ^
In file included from /usr/include/python2.7/Python.h:101:0,
                 from src/pynauty.c:15:
/usr/include/python2.7/dictobject.h:114:17: note: expected ‘Py_ssize_t *’ but argument is of type ‘int *’
 PyAPI_FUNC(int) PyDict_Next(

要修复此警告,请更改

int i,j;

进入src/pynauty.c

Py_ssize_t i;
int j;

并重新编译 pynauty。警告应该会消失,测试也应该会通过!

答案4

尽管您进行了编辑,但我仍不清楚您的步骤。假设您:

./configure # note this command for later make nauty.o nautil.o naugraph.o ln -s ../nautyXX nauty

并遇到了您在问题开头段落中描述的错误,那么您最有可能遇到的问题是 Python 默认配置为没有共享库。不幸的是,您下载的软件包正在搜索正在编译的共享库,但找不到。

建议的解决方案:运行./configure --enable-shared。这样,您就告诉 Python 也实际编译它们。您可能会遇到麻烦,具体取决于您那里有哪些共享库,但如果不了解更多信息,只能猜测。

资料来源:

我推荐 Github 链接,这里最清楚。

相关内容