CPython sources - how to build static python26.lib?

I am trying to compile a hello.pyx file in exe using Cython.

The first step was to compile hello.pyx into a hello.cpp file using the command "cython --cplus --embed hello.pyx". Embed means Generate a main() function that embeds the Python interpreter . I am trying to create an independent exe without dependencies.

In hello.cpp , I have #include "Python.h" , so I download Python sources from here: http://www.python.org/download/releases/2.6.6/ , choosing the source of the Gzipped ball (2.6.6 ) I add include dir and get the error about Python26.lib missing. Therefore, I am trying to compile it. By default, the assembly creates python26.lib, but it is only 200 KB, it is not a static library when trying to compile hello.cpp. I get the missing link errors.

In README, I do not see any useful instructions on how to statically put it. However, I googled the online README file, for the latest python version in trunk (3.x), there is some useful information about the static building:

http://svn.python.org/projects/python/trunk/PCbuild/readme.txt

The solution has no configuration for static libraries. However, this is easy; he creates a static library instead of a DLL. You just need to set the “Configuration Type” to “Static Library (.lib)” and change the macro preprocessor “Py_ENABLE_SHARED” to “Py_NO_ENABLE_SHARED". You can also change the "Runtime Library" from "Multithreaded DLL (/ MD)" to "Multithreaded (/ MT)".

But still there are not many details here, am I installing all the projects in the build solution as a static library? Or just pythoncore? The "Python" and "Pythonw" projects are "Applications", so I can only change the runtime library to / MT. These are the steps I am doing:

  • Switch to the holiday mode.
  • Python Project - Configuring /MT and Adding the Py_NO_ENABLE_SHARED Preprocessor Py_NO_ENABLE_SHARED
  • The "Pythoncore" project is a configuration type before Static library (.lib) , / MT, replacing the Py_ENABLE_SHARED preprocessor Py_ENABLE_SHARED with Py_NO_ENABLE_SHARED
  • The Pythonw project is the same as the Python project.
  • I right click on "Python".

Build Summary:

Assembly: 5 successful, 1 failed , 0 updated, 0 missing

 kill_python (ok) make_buildinfo (ok) make_versioninfo (ok) pythoncore (ok) w9xpopen (ok) python (4 errors) 

Python project failed to execute, here is the log:

 ------ Build started: Project: python, Configuration: Release Win32 ------ Compiling... python.c Compiling resources... Microsoft (R) Windows (R) Resource Compiler Version 6.1.7600.16385 Copyright (C) Microsoft Corporation. All rights reserved. Linking... pythoncore.lib(sysmodule.obj) : error LNK2019: unresolved external symbol __Py_svnversion referenced in function _svnversion_init pythoncore.lib(getversion.obj) : error LNK2019: unresolved external symbol _Py_GetBuildInfo referenced in function _Py_GetVersion pythoncore.lib(dynload_win.obj) : error LNK2019: unresolved external symbol __Py_DeactivateActCtx referenced in function __PyImport_GetDynLoadFunc pythoncore.lib(dynload_win.obj) : error LNK2019: unresolved external symbol __Py_ActivateActCtx referenced in function __PyImport_GetDynLoadFunc d:\python\src4\PCbuild\\python.exe : fatal error LNK1120: 4 unresolved externals Build Time 0:02 

I look at the / PCbuild / directory and I see that there is "pythoncore.lib" (10 MB) but no "python26.lib".

When I send Google these errors, I find only one conversation on mail.pythong.org: http://www.groupsrv.com/computers/about397568.html

I tried to remove the prereocessor definition of "_USRDLL" in the pythoncore project, but still the errors are the same.

Preprocessor definitions in pythoncore:

 _USRDLL Py_BUILD_CORE Py_NO_ENABLE_SHARED WIN32 

When I click the Edit button, I see the Inherited Values ​​field in another field:

 NDEBUG _WIN32 

That Ndebug is strange because I changed the mode to Release.

I am using Visual Studio 2008 (not Express) with all service packs installed.

Btw. I previously tried to build the whole solution, but there were some errors regarding the missing files, the path of which begins with: "../../dba -".


I tried compiling Python versions 2.6.2 and 2.6.6, but they both threw the same errors: pythoncore.lib(sysmodule.obj) : error LNK2019: unresolved external symbol

+4
source share
1 answer

I created static Python from 2.4 to 2.7, and it takes a bit of work to set it up.

You need to update the configuration for all allowed projects in the solution by installing them in / MT. To create static Python, everything it references, including libraries such as SQLite or OpenSSL, must also be static. This is true even for .pyd modules that are not actually included, because otherwise they will not be imported later.

You may also need to do this editing at the top of the /socketmodule.h module:

 #else /* MS_WINDOWS */ # define inet_pton _msvc_inet_pton # define inet_ntop _msvc_inet_ntop # include <winsock2.h> # include <ws2tcpip.h> # include <wspiapi.h> # undef inet_pton # undef inet_ntop 

Once you go beyond the immediate error, some other things that are probably necessary:

  • Disable the creation of embedded manifests; see line ~ 650 in distutils msvc9compiler.py; the fact that he creates them even when creating with / MT is a mistake.

  • Change / MD to / MT in distutils msvccompiler / msvc9compiler.py, otherwise libs libraries will not be built correctly.

+2
source

All Articles