Error when building CFFI - API out-line on Windows 10

Issue #374 resolved
Stefan Haid
created an issue

Hello guys,

Currently I am trying to wrap a simple c-function to Python using cffi out-line mode.
I have seen some other people having the same issues but all the fixes didn't work for me.
Does anyone know a solution for this error?

I am using Python 3.6.11

This are my files:

prime.c:

#include <stdio.h>
#include "prime.h"

int is_prime(long num){
     if (num <= 1) return 0;
     if (num % 2 == 0 && num > 2) return 0;
     for(int i = 3; i < num / 2; i+= 2)
     {
         if (num % i == 0)
             return 0;
     }
     return 1;
}

prime.h:

#ifndef _PRIME_H_
#define _PRIME_H_

int is_prime(long num);

#endif

cffi_build.py

from cffi import FFI

ffibuilder = FFI()

ffibuilder.set_source("_example",
                      r"""

                      int is_prime_from_c(long num){
                          return is_prime(num);
                      }

                      """,
                      libraries=[])

ffibuilder.cdef("""
    int is_prime(long num);
""")

if __name__ == "__main__":
    ffibuilder.compile(verbose=True)

Error:

_example.c(496): warning C4013: "is_prime" undefiniert; Annahme: extern mit Rckgabetyp int
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\workdir\github_python\python_wrap_c_libraries\venv\libs /LIBPATH:C:\Users\lwnhas6\AppData\Local\Programs\Python\Python36\libs /LIBPATH:C:\Users\lwnhas6\AppData\Local\Programs\Python\Python36 /LIBPATH:C:\workdir\github_python\python_wrap_c_libraries\venv\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\ATLMFC\lib\x64" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x64" /EXPORT:PyInit__example .\Release\_example.obj /OUT:.\_example.cp36-win_amd64.pyd /IMPLIB:.\Release\_example.cp36-win_amd64.lib
   Bibliothek ".\Release\_example.cp36-win_amd64.lib" und Objekt ".\Release\_example.cp36-win_amd64.exp" werden erstellt.
_example.obj : error LNK2001: Nicht aufgelstes externes Symbol "is_prime".
.\_example.cp36-win_amd64.pyd : fatal error LNK1120: 1 nicht aufgelste Externe
Traceback (most recent call last):
  File "C:\Users\lwnhas6\AppData\Local\Programs\Python\Python36\lib\distutils\_msvccompiler.py", line 519, in link
    self.spawn([self.linker] + ld_args)
  File "C:\Users\lwnhas6\AppData\Local\Programs\Python\Python36\lib\distutils\_msvccompiler.py", line 542, in spawn
    return super().spawn(cmd)
  File "C:\Users\lwnhas6\AppData\Local\Programs\Python\Python36\lib\distutils\ccompiler.py", line 909, in spawn
    spawn(cmd, dry_run=self.dry_run)
  File "C:\Users\lwnhas6\AppData\Local\Programs\Python\Python36\lib\distutils\spawn.py", line 38, in spawn
    _spawn_nt(cmd, search_path, dry_run=dry_run)
  File "C:\Users\lwnhas6\AppData\Local\Programs\Python\Python36\lib\distutils\spawn.py", line 81, in _spawn_nt
    "command %r failed with exit status %d" % (cmd, rc))
distutils.errors.DistutilsExecError: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.14.26428\\bin\\HostX86\\x64\\link.exe' failed with exit status 1120

During handling of the above exception, another exception occurred:
.......

Comments (2)

  1. Armin Rigo
    ffibuilder.set_source("_example",
                          r"""
                          #include "prime.h"
                          """, sources=["prime.c"])
    

    In general I'd recommend the mailing list instead of this issue tracker for this kind of question. The issue tracker is for bugs of cffi.

  2. Log in to comment