简体   繁体   中英

Problems with compiling C code in Python

I use Anaconda for Python 2.7.10 in Windows 7, 64 bit. I also use Visual Studio 2010. I installed Microsoft Visual Studio for Python.

When I try to compile a C code in python (inside cmd):

C:\Anaconda\sms-tools-master\software\transformations_interface>python compileModule.py build_ext --inplace

I get a lot of warnings and some errors that the final part of it are as follows:

C:\Program Files (x86)\Microsoft Visual Studio

10.0\VC\BIN\amd64\link.exe /DLL / nologo /INCREMENTAL:NO /LIBPATH:C:\Anaconda\libs /LIBPATH:C:\Anaconda\PCbuild\am d64 m.lib

/EXPORT:initutilFunctions_C build\temp.win-amd64-2.7\Release\utilFunct
ions.obj build\temp.win-amd64-2.7\Release\cutilFunctions.obj

/OUT:C:\Anaconda\sm
s-tools-master\software\models\utilFunctions_C\utilFunctions_C.pyd

/IMPLIB:build \temp.win-amd64-2.7\Release\utilFunctions_C.lib

/MANIFESTFILE:build\temp.win-amd
64-2.7\Release\utilFunctions_C.pyd.manifest LINK : fatal error

LNK1181: cannot open input file 'm.lib' error: command 'C:\\Program
Files (x86)\\Microsoft Visual Studio 10.0\\VC\\BIN\\ amd64\\link.exe'

failed with exit status 1181

Please let me see how this problem can be fixed.

And if I only comment

libraries=['m']

and use

ext_modules = [Extension("utilFunctions_C",sourcefiles, include_dirs=py_inc + np_inc)]    

then I get lots of warning (not errors hopefully) whose final part is:

C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\BIN\\amd64\\link.exe /DLL / nologo /INCREMENTAL:NO /LIBPATH:C:\\Anaconda\\libs /LIBPATH:C:\\Anaconda\\PCbuild\\am d64 /EXPORT:initutilFunctions_C build\\temp.win-amd64-2.7\\Release\\utilFunctions.o bj build\\temp.win-amd64-2.7\\Release\\cutilFunctions.obj /OUT:C:\\Anaconda\\sms-tool s-master\\software\\models\\utilFunctions_C\\utilFunctions_C.pyd /IMPLIB:build\\temp. win-amd64-2.7\\Release\\utilFunctions_C.lib /MANIFESTFILE:build\\temp.win-amd64-2.7 \\Release\\utilFunctions_C.pyd.manifest cutilFunctions.obj : warning LNK4197: export 'initutilFunctions_C' specified mul tiple times; using first specification Creating library build\\temp.win-amd64-2.7\\Release\\utilFunctions_C.lib and obj ect build\\temp.win-amd64-2.7\\Release\\utilFunctions_C.exp

I think the problem is now resolved. Thank you LPs!!

Comment the line in setup.py that say libraries=['m'] , and run again.

The need to link to m might be a GCC thing to link Math library, that is managed by MSVCR in Windows environment.

Just removing .. libraries=['m'] .. from ext_modules helped me compile on windows 2010 too. No errors or warnings. This is how it looks finally,

ext_modules = [Extension("utilFunctions_C",sourcefiles , include_dirs=py_inc + np_inc)]

Thanks a bunch

Edit .....

Here is entire code for compileModule.py Please refer to comments for details.

from distutils.core import setup, Extension
from distutils.sysconfig import *
from distutils.util import *
from Cython.Distutils import build_ext
import numpy
import os
import os.path

#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

try:
   from distutils.command.build_py import build_py_2to3 \
   as build_py
except ImportError:
   from distutils.command.build_py import build_py

try:
   from Cython.Distutils import build_ext
except ImportError:
   use_cython = False
else:
   use_cython = True


py_inc = [get_python_inc()]

np_lib = os.path.dirname(numpy.__file__)
np_inc = [os.path.join(np_lib, 'core/include')]
ext_inc = os

sourcefiles = ["utilFunctions.c", "cutilFunctions.pyx"]

setup(
    cmdclass = {'build_ext': build_ext},
    #ext_modules = [Extension("utilFunctions_C",sourcefiles, libraries=['m'], include_dirs=py_inc + np_inc)]
    ext_modules = [Extension("utilFunctions_C",sourcefiles , include_dirs=py_inc + np_inc)]

  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM