Pergunta

I have created a c program which requires an input (through scanf). Then I created the .so file and called that in a python script, so that when I run the script, input will be asked in the terminal. But when I run the python program, the terminal hangs.

Please note:
1. My c code

#include <stdio.h>
#include <string.h>
int open(void)
{
    char input[20];
    scanf("input = %s\n",&input);
    printf("\n%s\n","input");
}

2. Command I used for compiling the code

gcc -c usb_comm.c

3.Creating .so file

gcc -shared -o libhello.so usb_comm.o

4.Relevant section of python program
Loading the .so file

from ctypes import cdll
mydll = cdll.LoadLibrary('/home/vineeshvs/Dropbox/wel/workspace/Atmel/libhello.so')


Calling the function defined in the c program

mydll.scanf("%c",mydll.open())

Thanks for listening :)

Foi útil?

Solução

mydll.open() call scanf. Why do you call scanf in Python?

Just call mydll.open() only:

mydll.open()

UPDATE

#include <stdio.h>

void open(void)
{
    char input[20];
    printf("input = "); // If you want prompt
    scanf("%s", input);
    printf("\n%s\n", input);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top