opisthofulax
opisthofulax

Reputation: 561

graphtool ImportError: <myobject>.so: invalid ELF header

I am trying to write my extensions with graph-tool following this guide about Writing extensions in C++ to extend graph-tool with code written in C++. Following the guide at the link i downloaded the same files for the making of a shared object called libkcore.so which has to be used precompiled in the python program.

The whole process can be reproduced downloading files from the page (kcore.hh, kcore.cc, kcore.py) and then running the make command on the following Makefile

PY3DOTVERSION=$(shell python -c "import sys; print(sys.version_info[0],sys.version_info[1],sep='.')")
CXX=g++
CXXFLAGS=-O3 -fopenmp -std=gnu++17 -Wall -fPIC `pkg-config --cflags --libs graph-tool-py$(PY3DOTVERSION)` -shared
ALL: libkcore.so

libkcore.so: kcore.hh kcore.cc
    ${CXX} ${CXXFLAGS} kcore.cc -o libkcore.so

In my case I have graph-tool installed with conda and so gcc (g++) so that i had to specify the pkg-config path adding

$(shell PKG_CONFIG_PATH=$(CONDA_PREFIX)/lib/pkgconfig pkg-config --cflags --libs graph-tool-py$(PY3DOTVERSION))

to my CXXFLAGS instead of

`pkg-config --cflags --libs graph-tool-py$(PY3DOTVERSION)`

in the Makefile. Now the output of the compilation seems all ok with no warnings or errors

g++ -O3 -fopenmp -std=gnu++17 -Wall -fPIC -pthread -I<homedir>/anaconda3/envs/lrgsgenv/include 
    -I<homedir>/anaconda3/envs/lrgsgenv/lib/python3.12/site-packages/graph_tool/include 
    -I<homedir>/anaconda3/envs/lrgsgenv/lib/python3.12/site-packages/graph_tool/include/boost-workaround 
    -I<homedir>/anaconda3/envs/lrgsgenv/lib/python3.12/site-packages/graph_tool/include/pcg-cpp 
    -I<homedir>/anaconda3/envs/lrgsgenv/lib/python3.12/site-packages/cairo/include 
    -I<homedir>/anaconda3/envs/lrgsgenv/include/python3.12 
    -I<homedir>/anaconda3/envs/lrgsgenv/include 
    -I<homedir>/anaconda3/envs/lrgsgenv/lib/python3.12/site-packages/numpy/core/include 
    -L<homedir>/anaconda3/envs/lrgsgenv/lib 
    -lboost_python312 
    -I<homedir>/anaconda3/envs/lrgsgenv/include 
    -shared kcore.cc -o libkcore.so

where <homedir> is my home directory. Sadly if i run

$ ldd libkcore.so
    not a dynamic executable

i suddenly realize that the shared object, which is supposed to be an executable, is not a binary file, rather is a ASCII text file so that when i try importing it in python i get

>>> from kcore import kcore_decomposition
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<homedir>/PhD/projects/complex-networks/lrgsglib/src/lrgsglib/gt_patches/cpp/kcore.py", line 4, in <module>
    import libkcore
ImportError: <homedir>/PhD/projects/complex-networks/lrgsglib/src/lrgsglib/gt_patches/cpp/libkcore.so: invalid ELF header

Inspecting the libkcore.so i see a bunch of lines resembling a dump file, a long list of functions and methods beginning with

# 0 "kcore.cc"
# 0 "<built-in>"
# 0 "<command-line>"
# 1 "kcore.cc"
# 1 "<homedir>/anaconda3/envs/lrgsgenv/include/boost/python.hpp" 1
# 11 "<homedir>/anaconda3/envs/lrgsgenv/include/boost/python.hpp"
# 1 "<homedir>/anaconda3/envs/lrgsgenv/include/boost/python/args.hpp" 1

As always <homedir> stands for my home directory. What am I missing? WHat am I doing wrong? I suspect something with g++ or with the conda environment which is causing this weird compilation

gcc specs

$ which g++
<homedir>/anaconda3/envs/lrgsgenv/bin/g++
$ g++ --version
g++ (conda-forge gcc 14.2.0-1) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

make file with CONDA_PREFIX

This is my Makefile as it is, i am already pinting the CONDA_PREFIX variable, and seems to be correct

PKG_CONFIG_PATH := $(CONDA_PREFIX)/lib/pkgconfig


PY3DOTVERSION=$(shell python -c "import sys; print(sys.version_info[0],sys.version_info[1],sep='.')")
CXX := g++
CXXFLAGS := -O3 -fopenmp -std=gnu++17 -Wall -fPIC $(shell PKG_CONFIG_PATH=$(CONDA_PREFIX)/lib/pkgconfig pkg-config --cflags --libs graph-tool-py$(PY3DOTVERSION)) -I$(CONDA_PREFIX)/include -shared

all: libkcore.so
    @echo "CONDA_PREFIX is: $(CONDA_PREFIX)"
    @echo "PKG_CONFIG_PATH is: $(PKG_CONFIG_PATH)"
    @echo "PY3DOTVERSION is: $(PY3DOTVERSION)"

libkcore.so: kcore.hh kcore.cc
    ${CXX} ${CXXFLAGS} kcore.cc -o libkcore.so

clean:
    rm -f *.so

The compilation takes place with make all command

g++ -O3 -fopenmp -std=gnu++17 -Wall -fPIC -pthread 
    -I<homedir>/anaconda3/envs/lrgsgenv/include 
    -I<homedir>/anaconda3/envs/lrgsgenv/lib/python3.12/site-packages/graph_tool/include 
    -I<homedir>/anaconda3/envs/lrgsgenv/lib/python3.12/site-packages/graph_tool/include/boost-workaround 
    -I<homedir>/anaconda3/envs/lrgsgenv/lib/python3.12/site-packages/graph_tool/include/pcg-cpp 
    -I<homedir>/anaconda3/envs/lrgsgenv/lib/python3.12/site-packages/cairo/include 
    -I<homedir>/anaconda3/envs/lrgsgenv/include/python3.12 
    -I<homedir>/anaconda3/envs/lrgsgenv/include 
    -I<homedir>/anaconda3/envs/lrgsgenv/lib/python3.12/site-packages/numpy/core/include 
    -L<homedir>/anaconda3/envs/lrgsgenv/lib 
    -lboost_python312 
    -I<homedir>/anaconda3/envs/lrgsgenv/include 
    -shared kcore.cc -o libkcore.so
CONDA_PREFIX is: <homedir>/anaconda3/envs/lrgsgenv
PKG_CONFIG_PATH is: <homedir>/anaconda3/envs/lrgsgenv/lib/pkgconfig
PY3DOTVERSION is: 3.12

Upvotes: 1

Views: 55

Answers (0)

Related Questions