Because of the recent study of boost::p Ython, want to try to build a different language between each other.
Reference: http://blog.csdn.net/joliny/article/details/2457197.
It's amazing that pytont2.7 and VS 2010 are already installed on your computer, and you don't need to install any other APIs or compilers, so you can call Python directly in your C program.
The first attempt was to compile under the VS IDE, but it was found that the Python installed on the web was the release version, and the compilation always appeared to be problematic. So I switched to the second method, under the DOS command.
I think the first method should be done through, but the time is compact, no longer continue, there is time to try it later.
Knowledge Preparation
1.Dos compile command CL parameter detailed
C + + Compiler options-optimize-/o1 minimize space/op[-] improve floating-point consistency/ O2 maximized speed/os preferred code space/OA assumes no alias/ot preferred code speed/ob<n> inline expansion (default n=0) /ow assumes that the crossover function alias/od disables the optimization (default)/ox maximize option. (/OGITYB2/GS)/og enable global optimization/oy[-] enable frame pointer omit/oi enable intrinsic function-code generation-/G3 for 80386 Optimization/gh enable _penter function call/g4 for 80486 optimization/gh enable _pexit function call/g5 optimize for Pentium /gr[-] Enable C + + Rtti/g6 to optimize Ppro, p-ii, P-III/gx[-] enable C + + EH (same as/EHSC)/g7 optimize Pentium 4 or Athlon/E Hs Enable C + + EH (no SEH exception)/GB optimized for mixed model (default)/EHa enable C + + EH (W/seh exception)/gd __cdecl calling convention/EHC Ext Ern "C" defaults to nothrow/gr __fastcall calling convention/gt generate fibre security TLS Access/gz __stdcall calling convention/gm[-] enable minimal re-birth /ga to optimize for Windows applications/gl[-] enable link-time code generation/GF enable string pooling/qiFdiv[-] Enable Pentium Fdiv repair/GF enable read-only string pooling/qi0f[-] enable Pentium 0x0f repair/gy separate linker functions/qif ist[-] Use FIST instead of Ftol ()/gz Enable stack check (/RTCs)/RTC1 Enable quick Check (/RTCSU)/ge force stack Check for all functions/RTCC convert to smaller type Check/gs[num] control stack check call/RTCs stack frame runtime check/GS enable security check/RTCU uninitialized local usage check/clr[:noassembly] for common language Runtime Compilation noassembly-does not produce assembly/arch:<sse| Sse2> The minimum requirements for the CPU structure, one of the following: SSE-enable CPU available for SSE-enabled instructions SSE2-enable CPU available for SSE2 support-output Out file-/fa[file] named assembly list file/fo<file> named Object file/FA[SC] Configuration assembly list/fp<file> naming precompiled header file/fd[file Named. PDB file/fr[file] name the source browser file/fe<file> named executable/fr[file] name extension. SBR file/fm[file] named map file-preprocessor-/ai<dir> add to assembly search path/fx merge the inserted code into a file/FU<FILE&G T Force the use of assemblies/modules/fi<file> naming mandatory include files/C do not extract annotations/u<name> remove predefined macros/D<NAME>{=|#}≪text> define Macro/u remove all predefined macros/E preprocessing to stdout/i<dir> to include the search path/EP preprocessing to stdout, no #line /x ignores "standard location"/p preprocessing to file-language-/zi enable debug information/ze enable extension (default)/zi enable "Edit and Relay "Debug information/zl omitted. Default library name in OBJ/z7 enable legacy debugging information/ZG generate function prototype/ZD only line number debug information/zs only syntax check/zp[n] Wrapping structure on n-byte boundaries /VD{0|1} disable/enable vtordisp/za disable extension (alluding to/op)/vm<x> pointer to member type/ZC:ARG1[,ARG2] C + + language consistency, here are the parameters: F Orscope-Enforcing the use of standard C + + wchar_t-wchar_t for scope rules is a native type, not a typedef-Miscellaneous-@<file> Option response file /wo<n> issue a warning N/?,/help Print this help message/w<l><n> set warning level for n 1-4/c only compile, not link /w<n> Set Warning Level (default n=1)/h<num> maximum external name length/wall enable all warnings/j default char type is unsigned /WP64 enable 64-bit port location warning/nologo Suppress copyright messages/WX treat warnings as Errors/showincludes show include file names/WL enable single-line diagnostics/tc<soUrce file> compiles the file to. C/yc[file] creation. PCH file/tp<source file> compiles the files to. cpp/yd put debug information on each. In OBJ/tc All files are compiled into. C/yl[sym] for the debug library. The PCH reference/TP compiles all files to. cpp/yu[file] use. PCH file/v<string> Set version string/yx[file] Automatic. pch/w Disable all Warnings/y-Disable all PCH options/wd<n> disable warning n/zm<n> maximum memory allocation (% by default)/w E<n> will warn N as error-link-/MD and MSVCRT. LIB links/MDd and MSVCRTD. LIB Debug Library links/ml and LIBC. LIB links/MLd and LIBCD. LIB Debug Library links/mt and LIBCMT. LIB links/MTd and LIBCMTD. LIB Debug Library link/ld created. Dll/f<num> set the stack size/LDD created. DLL Debug Library/LINK [linker options and libraries]
2. Compile and invoke the DLL experiment VS2010
I. Calling Python in a C program
First, it is necessary to add the path to the python in the environment variable. This is the same as usual in the system environment variable path add path (tried, failed, is not in path under the default Add Bin Run program AH) is not the same place,
Two variables include and Lib are added to the system environment variables, respectively, corresponding to the include and Libs in the Python directory.
Then, test the program:
Run: Cl foo.c in the VS. DOS window to generate a target file that shows the results of calling the Python time class library.
Second, the use of C language to generate Python dynamic link
Previously generated DLL files under the VS IDE, this time in the DOS window, test the following files:
File foo.c
#include <python.h>/* Define the method table. */static pyobject *foo_bar (pyobject *self, Pyobject *args); static Pymethoddef foomethods[] = {{"bar", Foo_bar, METH_ Varargs},{null, null}};/* here ' s the initialization function. We don ' t need to does anythingfor our own needs, but Python needs that method table. */void Initfoo () {(void) py_initmodule ("foo", Foomethods);} /* Finally, let's do something ... involved ... as an example function. */static pyobject *foo_bar (pyobject *self, Pyobject *args) {char *string;int len;if (! Pyarg_parsetuple (args, "s", &string)) return Null;len = strlen (string); return Py_buildvalue ("I", Len);
Definition file Foo.def
Exportsinitfoo
Run as follows:
E:\tmp>cl-c Foo.cmicrosoft (R)/C + + optimizing Compiler Version 16.00.30319.01 for X64copyright (c) Microsoft Corpora tion. All Rights Reserved.foo.ce:\tmp>link Foo.obj/dll/def:foo.def/out:foo.dllmicrosoft (R) Incremental Linker Version 10 .00.30319.01Copyright (C) Microsoft Corporation. All rights reserved. Creating library Foo.lib and Objects Foo.exp
Third, next is the experiment of contacting Boost.python
Python and C call each other, compile