Tôi muốn bắt đầu tìm hiểu thêm về cách sử dụng SWIG và các phương thức khác để giao tiếp Python và C++. Để bắt đầu, tôi muốn để biên dịch chương trình đơn giản này đề cập đến trong another post:Cố gắng hiểu quy trình liên kết để viết Python/C++ hybrid
#include <Python.h>
int main()
{
Py_Initialize();
PyRun_SimpleString ("import sys; sys.path.insert(0, '/home/ely/Desktop/Python/C-Python/')");
PyObject* pModule = NULL;
PyObject* pFunc = NULL;
pModule = PyImport_ImportModule("hello");
if(pModule == NULL){
printf("Error importing module.");
exit(-1);
}
pFunc = PyObject_GetAttrString(pModule, "Hello");
PyEval_CallObject(pFunc, NULL);
Py_Finalize();
return 0;
}
mà tập tin "hello.py" chỉ có các nội dung:
def Hello():
print "Hello world!"
Lưu ý: Tôi đã có python2 .7-dev và python-dev và libboost-python-dev được cài đặt. Nhưng khi tôi biên dịch mã, tôi nhận được các lỗi mà tôi tin là do liên kết không đúng với các thư viện Python.
[email protected]:~/Desktop/Python/C-Python$ gcc -I/usr/include/python2.7 test.cpp /tmp/ccVnzwDp.o: In function `main':
test.cpp:(.text+0x9): undefined reference to `Py_Initialize'
test.cpp:(.text+0x23): undefined reference to `PyImport_ImportModule'
test.cpp:(.text+0x58): undefined reference to `PyObject_GetAttrString'
test.cpp:(.text+0x72): undefined reference to `PyEval_CallObjectWithKeywords'
test.cpp:(.text+0x77): undefined reference to `Py_Finalize'
/tmp/ccVnzwDp.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Tôi đang tìm kiếm các ví dụ về trực tuyến này và tôi đã tìm cú pháp sau, khiến mã biên dịch thành tệp đối tượng, nhưng sau đó tôi không thể thực thi tệp.
[email protected]:~/Desktop/Python/C-Python$ gcc -c -g -I/usr/include/python2.7 test.cpp
[email protected]:~/Desktop/Python/C-Python$ ./test.o
bash: ./test.o: Permission denied
[email protected]:~/Desktop/Python/C-Python$ chmod ug=rx ./test.o
[email protected]:~/Desktop/Python/C-Python$ ./test.o
bash: ./test.o: cannot execute binary file
[email protected]:~/Desktop/Python/C-Python$ sudo chmod ug=rx ./test.o
[email protected]:~/Desktop/Python/C-Python$ ./test.o
bash: ./test.o: cannot execute binary file
Các hành vi tương tự như trên vẫn thấy nếu tôi sử dụng g++
thay vì gcc
.
Giúp hiểu được lỗi của tôi khi liên kết sẽ tuyệt vời và thậm chí tốt hơn cho bất kỳ loại giải thích nào giúp tôi hiểu "logic" đằng sau loại liên kết tôi cần làm, để tôi nhớ rõ hơn những gì có thể những điều tôi quên trong lần sau. Cảm ơn!
Một nhận xét khác: bạn không thể thực thi trực tiếp các tệp '.o'. Trước tiên, bạn cần phải liên kết chúng thành một tập tin thực thi 'ELF'. – ulidtko