Compile .c files to .pyd - python

I used Cython to convert a .pyx file to .c. Now, I'm trying to compile it to .pyd, using the tcc compiler:
C:\Users\USER>"C:\Program Files\tcc\tcc.exe" tkExtra.c -o tkExtra.pyd -
shared -IC\Python27\include -LC\Python27\libs -lpython27
However, I get this error:
tkExtra.c:8: error: include file 'pyconfig.h' not found
C:\Python27\include does have pyconfig.h. I had looked at this answer to get the command line code for this, only substituting gcc with tcc.
How can I fix this, or is there a better way to do this?

Seems like you're missing some colons. Try:
C:\Users\USER>"C:\Program Files\tcc\tcc.exe" tkExtra.c -o tkExtra.pyd -shared -IC:\Python27\include -LC:\Python27\libs -lpython27

Related

fatal error: 'Eigen/Dense' file not found

I am an Ultra Linux newbie, and I am trying to install this program and when I try to build the python wrapper I'd get this
~/Downloads/DeepMimic-master/DeepMimicCore$ make python
clang++ -c -g -std=c++11 -O3 -Wall -fPIC -I./ -I../../libraries/eigen -I../../libraries/bullet3/src -I/usr/include/python3.6m -I/usr/lib/ -lpython3.6m -o objs/Main.o Main.cpp
clang: warning: -lpython3.6m: 'linker' input unused [-Wunused-command-line-argument]
In file included from Main.cpp:3:
In file included from ./DeepMimicCore.h:3:
In file included from ./util/ArgParser.h:6:
./util/MathUtil.h:5:10: fatal error: 'Eigen/Dense' file not found
#include "Eigen/Dense"
You're missing a dependency, Eigen, which is listed under 'Dependencies' in the DeepMimic readme.
I see this problem has been encountered before:
fatal error: Eigen/Dense: No such file or directory
Looks like that program depends on Eigen. Try download Eigen and putting it in the appropriate directory.
Eigen is a template library, so you just have to download it, unzip it, and copy the folder called Eigen inside of the directory of the program.
Eigen website

OSX Caffe Compilation Fails With Expected Expression Error

I have been trying to install Caffe on my mac running OSX 10.13.6
I have followed several guides, including the installation guide on the caffe site. I have erased everything and restarted several times. I get the same problem no matter what. When I go to compile everything I am getting this
Scanning dependencies of target caffeproto
[ 1%] Building CXX object src/caffe/CMakeFiles/caffeproto.dir/__/__/include/caffe/proto/caffe.pb.cc.o
In file included from /Users/Name/Documents/Programming/PythonEnv/caffe/build/include/caffe/proto/caffe.pb.cc:4:
In file included from /Users/Name/Documents/Programming/PythonEnv/caffe/build/include/caffe/proto/caffe.pb.h:9:
/usr/local/include/google/protobuf/stubs/common.h:209:17: error: expected
expression
OnShutdownRun([](const void* p) { delete static_cast<const T*>(p); }, p);
^
In file included from /Users/Name/Documents/Programming/PythonEnv/caffe/build/include/caffe/proto/caffe.pb.cc:4:
In file included from /Users/Name/Documents/Programming/PythonEnv/caffe/build/include/caffe/proto/caffe.pb.h:25:
In file included from /usr/local/include/google/protobuf/generated_message_table_driven.h:34:
In file included from /usr/local/include/google/protobuf/map.h:49:
In file included from /usr/local/include/google/protobuf/map_type_handler.h:35:
In file included from /usr/local/include/google/protobuf/wire_format_lite_inl.h:43:
/usr/local/include/google/protobuf/message_lite.h:117:3: error: unknown type
name 'constexpr'
constexpr const T& get() const { return reinterpret_cast<const T&>(union_); }
I have also tried using CMake and run into the same problem. I'm not sure where to go from here. I am not incredibly knowledgable about building processes so I apologize if this is vague and will be happy to provide whatever other information might help fix this.
Thank you!
If compiling with make, change Makefile as follows:
- CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
+ CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11
This is the same problem as reported here: https://trac.macports.org/ticket/57093#comment:1
The compiler needs to be using C++11. Try making this change in CMakeLists.txt:
if(UNIX OR APPLE)
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -std=c++11")
endif()
You just need to replace the newest version of protobuf with protobuf v3.5.1.
wget https://github.com/protocolbuffers/protobuf/archive/v3.5.1.zip
You need to download to source code, and compile it yourself.

Issue creating f2py shared object file when linking to libraries and modules

I am able to compile a fortran program normally with these commands:
ifort -O2 -I/work/GSI/include -c create_upperair.f90
ifort -o create_upperair.exe -O2 create_upperair.o -L/work/GSI/lib -lbufr
I am trying to take the same fortran program, but compile it as a python shared object file so that I can call this subroutine from a python script. However the f2py command is giving me issues.
Here is the command I am currently using:
f2py -c -L/work/GSI/lib -lbufr -I/work/GSI/include --f90flags=-O2 --fcompiler=ifort -m create_upperair create_upperair.f90
However that prints out errors at the end of the f2py command with something like:
ld: /work/GSI/lib/libbufr.a(closbf.o): relocation R_X86_64_32S against `nulbfr_' can not be used when making a shared object: recompile with -fPIC
I tried to add --f90flags="-O2 -fPIC" to my f2py command, but it didn't work either. Any ideas of what direction to go with this problem would be much appreciated.
Here is the full error at the end of f2py (note that I changed a few directory names above for simplicity):
ifort:f90: prepbufr_encode_upperair.f90
/glade/apps/opt/modulefiles/ys/cmpwrappers/ifort -shared -shared -nofor_main /tmp/tmpzYKBY5/tmp/tmpzYKBY5/src.linux-x86_64-2.7/prepbufr_encode_upperairmodule.o /tmp/tmpzYKBY5/tmp/tmpzYKBY5/src.linux-x86_64-2.7/fortranobject.o /tmp/tmpzYKBY5/prepbufr_encode_upperair.o - L/glade/p/work/sdegelia/softwares/comGSI_v3.3/lib -L/glade/apps/opt/python/2.7.7/gnu-westmere/4.8.2/lib -lbufr_i4r8 -lpython2.7 -o ./prepbufr_encode_upperair.so
ld: /glade/p/work/sdegelia/softwares/comGSI_v3.3/lib/libbufr_i4r8.a(closbf.o): relocation R_X86_64_32S against `nulbfr_' can not be used when making a shared object; recompile with -fPIC
/glade/p/work/sdegelia/softwares/comGSI_v3.3/lib/libbufr_i4r8.a: could not read symbols: Bad value
ld: /glade/p/work/sdegelia/softwares/comGSI_v3.3/lib/libbufr_i4r8.a(closbf.o): relocation R_X86_64_32S against `nulbfr_' can not be used when making a shared object; recompile with -fPIC
/glade/p/work/sdegelia/softwares/comGSI_v3.3/lib/libbufr_i4r8.a: could not read symbols: Bad value
error: Command "/glade/apps/opt/modulefiles/ys/cmpwrappers/ifort -shared -shared -nofor_main /tmp/tmpzYKBY5/tmp/tmpzYKBY5/src.linux-x86_64-2.7/prepbufr_encode_upperairmodule.o /tmp/tmpzYKBY5/tmp/tmpzYKBY5/src.linux-x86_64-2.7/fortranobject.o /tmp/tmpzYKBY5/prepbufr_encode_upperair.o -L/glade/p/work/sdegelia/softwares/comGSI_v3.3/lib -L/glade/apps/opt/python/2.7.7/gnu-westmere/4.8.2/lib -lbufr_i4r8 -lpython2.7 -o ./prepbufr_encode_upperair.so" failed with exit status 1

Compile Cython extensions from the command line with gcc (mingw32) on windows

I'm trying to test a small cython module on win32, and I'm having trouble building it.
The file is called linalg_cython.pyx and has these contents:
from __future__ import absolute_import, division, print_function
import numpy as np
cimport numpy as np
import cython
##cython.boundscheck(False)
#np.ndarray[np.float32]
##cython.wraparound(False)
def L2_sqrd_float32(np.ndarray hist1, np.ndarray hist2):
""" returns the squared L2 distance
seealso L2
Test:
hist1 = np.random.rand(4, 2)
hist2 = np.random.rand(4, 2)
out = np.empty(hist1.shape, dtype=hist1.dtype)
"""
return (np.abs(hist1 - hist2) ** 2).sum(-1) # this is faster
L2_sqrd = L2_sqrd_float32
I was able to get this compiling by using a setup.py, but I don't want to have to rely on setup.py to build the extensions. This is because I haven't fully understood the cython compilation process yet. I want to compile it on my own first before I start trusting setup.py. That being said I was able get a good start by looking at the output of "setup.py build_ext":
C:\MinGW\bin\gcc.exe -mdll -O -Wall ^
-IC:\Python27\Lib\site-packages\numpy\core\include ^
-IC:\Python27\include -IC:\Python27\PC ^
-c vtool\linalg_cython.c ^
-o build\temp.win32-2.7\Release\vtool\linalg_cython.o
writing build\temp.win32-2.7\Release\vtool\linalg_cython.def
C:\MinGW\bin\gcc.exe -shared \
-s \
build\temp.win32-2.7\Release\vtool\linalg_cython.o \
build\temp.win32-2.7\Release\vtool\linalg_cython.def \
-LC:\Python27\libs \
-LC:\Python27\PCbuild \
-lpython27 \
-lmsvcr90 \
-o build\lib.win32-2.7\vtool\linalg_cython.pyd
The pyd file that this created seemed to work, but my goal is understanding, not just making it work.
Copying this format (and trying some things myself) I'm currently using these commands to build everything manually.
C:\Python27\Scripts\cython.exe vtool\linalg_cython.pyx
C:\MinGW\bin\gcc.exe -mdll -O -DNPY_NO_DEPRECATED_API -Wall -Wno-unknown-pragmas
-Wno-format -Wno-unused-function -m32 -shared
-IC:\Python27\Lib\site-packages\numpy\core\include -IC:\Python27\include
-IC:\Python27\PC -IC:\Python27\Lib\site-packages\numpy\core\include
-LC:\Python27\libs -LC:\Python27\PCbuild -lpython27 -lmsvcr90 -o
vtool\linalg_cython.pyd -c vtool\linalg_cython.c
The main difference between my command and the setup.py command is that I'm trying to call gcc in one line instead of splitting it up into two lines. I would have called it in two commands, but the def file seems to be autogenerated by setup.py and I'm not sure what its all about.
Its contents seem simple:
LIBRARY linalg_cython.pyd
EXPORTS
initlinalg_cython
but I'd like to know more about what it is before I split my command into two steps and autogenerate this def file myself. Either way shouldn't it be possible to create the .pyd in one call to gcc?
With the command that I'm using I'm able to get a .pyd file in the right place, but when I try to import it I get
<type 'exceptions.ImportError'>: DLL load failed: %1 is not a valid Win32 application.
which is supposed to be a x86/x64 mismatch, hence why I tried adding the flag -m32.
In summary: When trying to comiple a simple cython module my gcc command is giving me 32/64 bit errors. How do I fix my gcc command such that it generates a valid 32 bit pyd file.
You didn't mention if your python is 32bit or 64bit..this kind of behavior usually happens when you try to import 32bit module in 64bit python, or vice versa.
Make sure your python and module, you`re trying to import, are same bit architecture. Easiest way to fix this is by downloading and installing right python.

Compile file .c with embedded Python/C functions

I'm starting the study of Python/C API and I make the first code to test some functions, I write this:
file: test.c
#include "Python.h"
int main() {
PyObject* none = Py_BuildValue("");
}
I compile with command:
gcc -I/usr/include/python2.7 test.c
I've the error undefined reference to `Py_BuildValue'
After I run:
gcc -I/usr/include/python2.7 --shared -fPIC hashmem.c
this compile without errors, but when I run the compiled file I've a
Segmentation fault (core dumped)
How do I set the gcc parameters?
I've ubuntu 12.04, python 2.7.3, gcc 4.6.3 and I installed python-dev.
Thanks.
In the comments #Pablo has provided the solution
gcc -I/usr/include/python2.7 test.c -lpython2.7
I forgot to link the python library with the "-l" parameter.
-llibrary
-l library
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX) compliance and is not recommended.)It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o' searches libraryz' after file foo.o but before bar.o. If bar.o refers to functions in z', those functions may not be loaded.The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specified precisely by name.The directories searched include several standard system directories plus any that you specify with -L.Normally the files found this way are library files—archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an -l option and specifying a file name is that - l surrounds library withlib' and `.a' and searches several directories. 
Parameter description source

Categories

Resources