繁体   English   中英

Python C++ ctypes 在 ubuntu 中传递 bool

[英]Python C++ ctypes passing bool in ubuntu

I'm trying to link my Python (3.7.6) code to C++ with a pass-back of a boolean from the C++ code to Python. 我的操作系统是 Ubuntu 16.04。 C++ 的 IDE 是 Visual Studio。

在运行 Python 包装代码时,我收到以下消息:

libcppstring.so: undefined symbol: cpp_string

python代码、*.so文件等都在同一个目录下。 c++ 代码是在终端提示符下编译的

g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC -o libcppstring.so cpp_string.cpp  

我的 python 包装代码、c++ 代码和 header 代码非常简单。 只想将两个字符串从 Python 传递给 C++ 共享 object 并将“true”返回给 ZA7F5F35426B62872711 也许我弄乱了编译命令的顺序。

#---------python wrapper code------------------
import ctypes
import sys
import os
localPath = os.path.dirname(os.path.realpath(__file__))
sys.path.append(localPath)
if __name__ == "__main__":
# Load the shared library into c types.

    c_lib = ctypes.CDLL(localPath+"/libcppstring.so")

    # Sample data for our call:
    string1 = "hello, "
    string2 = "world"

    c_lib.cpp_string.restype = ctypes.c_bool
    answer = c_lib.cpp_string( string1, string2)

    print(answer)

cpp 代码:cpp_string.cpp

#include "cpp_string.h"

bool cpp_string( std::string string1, std::string string2){
    /*use this to convert to const char* to use sqlite3 in c */
    /*used once the code is working*/  
    string1.c_str();
    return (true);
}

header 代码:cpp_string.h

#include <string>
#ifdef _MSC_VER
    #define EXPORT_SYMBOL __declspec(dllexport)
#else
    #define EXPORT_SYMBOL
#endif

EXPORT_SYMBOL bool cpp_string( std::string string1, std::string string2);

要将ctypes与 C++ 接口,请将extern "C"函数与 C 兼容类型一起使用。 还要声明.argtypes (参数类型和.restype (返回类型)以更好地检查错误。

测试.cpp

#include <iostream>

#ifdef _WIN32
#   define API __declspec(dllexport)
#else
#   define API
#endif

extern "C" API
int cpp_string(const char* string1, const char* string2) {
    std::cout << string1 << std::endl << string2 << std::endl;
    return 1;
}

测试.py

import ctypes as ct

dll = ct.CDLL('./test')
dll.cpp_string.argtypes = ct.c_char_p,ct.c_char_p
dll.cpp_string.restype = ct.c_int

print(dll.cpp_string(b'Hello',b'World!'))

Output:

Hello
World!
1

请注意,C 中的char*c_char_p中的 c_char_p 并采用 Python bytes类型参数。 如果要传递 Python str类型参数,请使用wchar_t* / c_wchar_p

首先,非常感谢 Tolonen 先生。 来自另一个社区和一个非常乐于助人的人(所有学分 > J*M#),我确实解决了我的问题。 这是我的代码(我想要的只是返回 TRUE,它确实如此。

cppstring.h

#include <string>
#ifdef _MSC_VER
    #define EXPORT_SYMBOL __declspec(dllexport)
#else
    #define EXPORT_SYMBOL
#endif

extern "C" {
EXPORT_SYMBOL bool cppstring( std::string string1, std::string string2);
}

cppstring.cpp

#include <string>
#include "cppstring.h"
bool cppstring( std::string string1, std::string string2){
/*use this to convert to const char* to use sqlite3 in c */
    string1.c_str();
    return (true);
}

和,

cString_test.py

#!/usr/bin/env python
""" Simple examples of calling C functions through ctypes module. """
import ctypes
import sys
import os

localPath = os.path.dirname(os.path.realpath(__file__))

sys.path.append(localPath)
if __name__ == "__main__":
    # Load the shared library into c types.

    c_lib = ctypes.CDLL(localPath+"/libcppstring.so")

    # Sample data for our call:
    string1 = "hello, "
    string2 = "world"
    c_lib.cppstring.restype = ctypes.c_bool
    answer = c_lib.cppstring( string1, string2)
    print(answer)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM