1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How to use the cl command?

Discussão em 'Python' iniciado por Stack, Outubro 4, 2024 às 19:52.

  1. Stack

    Stack Membro Participativo

    All, I found a piece of information on how to call c files in python, in these examples: there is a c file, which includes many other header files, the very beginning of this c files is #include Python.h, then I found that #include Python.h actually involves many many other header files, such as pystate.h, object.h, etc, so I include all the required header files. In an cpp IDE environment, it did not show errors. What I am trying to do is call this c code in python, so from ctypes import *, then it seems that a dll should be generated by code such as: cl -LD test.c -test.dll, but how to use the cl in this case? I used the cygwin: gcc, it worked fine. Could anyone help me with this i.e.: Call the C in python? Do I make myself clear? Thank you in advance!!

    Well, Now I feel it important to tell me what I did: The ultimate goal I wanna achieve is: I am lazy, I do not want to re-write those c codes in python, (which is very complicated for me in some cases), so I just want to generate dll files that python could call. I followed an example given by googleing "python call c", there are two versions in this examples: linux and windows: The example test.c:

    #include <windows.h>
    BOOL APIENTRY
    DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved) {
    return TRUE;
    }
    __declspec(dllexport) int
    multiply(int num1, int num2) {
    return num1 * num2;
    }


    Two versions: 1, Complie under linux

    gcc -c -fPIC test.c
    gcc -shared test.o -o test.so


    I did this in cygwin on my vista system, it works fine; :)

    2, Compile under windows:

    cl -LD test.c -test.dll


    I used the cl in windows command line prompt, it won't work!

    These are the python codes:

    from ctypes import *
    import os
    libtest = cdll.LoadLibrary(os.getcwd() + '/test.so')
    print test.multiply(2, 2)


    Could anyone try this and tell me what you get? thank you!

    Continue reading...

Compartilhe esta Página