문자열을 사용하여 정수를 설정하는 다음과 같은 C++ 함수가 있습니다.파이썬 ctypes를 사용하여이 extern "C"함수가 작동하지 않는 이유는 무엇입니까?
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
extern "C" {
int a() {
int number;
string value("100");
std::istringstream strm(value);
strm >> number;
if (strm.fail()) {
cout << "Ouch!" << endl;
}
else {
cout << "Number set to:" << number << endl;
};
return (int)strm.bad();
}
}
int main(int argc, char **argv)
{
a();
}
프로그램으로 컴파일하면 작동합니다.
$ g++ ./streamtest.cc -o streamtest;./streamtest
Number set to:100
하지만하는 ctypes에서 같은 함수를 호출하는 경우는 정수와 "STRM"을 설정하지 않습니다는 "나쁜"상태로 남아있다.
$ g++ -shared streamtest.cc -o libstreamtest.so
$ python -c "import ctypes;a = ctypes.CDLL('libstreamtest.so').a();print 'Got [%s] from a()' %a"
Ouch!
Got [1] from a()
이렇게하면 당황 스럽습니다. ctypes에서이 함수를 어떻게 작동시킬 수 있습니까?
는 파이썬 라이브러리 로더가 제대로 표준 라이브러리의 스트림에 필요한 약간의 정적/글로벌 생성자를 호출되지 않도록 될 수 있을까? –
당신은 하나의 스테핑을 시도하고 그들이 어디에서 벗어나기 시작했는지 비교해 보았습니까? – PlasmaHH
strm에 오류 기능이 있습니까? (하나는 strm.fail() 후 오류를 인쇄) – KevinDTimm