我用 g++ 编译了一个测试代码,没有任何问题。
#include "Python.h"
int main(int argc, char** argv)
{
Py_Initialize();
PyRun_SimpleString("import pylab");
PyRun_SimpleString("pylab.plot(range(5))");
PyRun_SimpleString("pylab.show()");
Py_Exit(0);
}
g++ -o test test.cpp -I/usr/include/python2.7/ -lpython2.7
工作正常并运行。
但是当我尝试将此代码嵌入另一个项目时,它失败了。这真的让我很困惑。
Makefile 如下所示。
CXX=g++
CXXFLAGS=-DIB_USE_STD_STRING -Wall -Wno-switch -g
ROOT_DIR=..
BASE_SRC_DIR=${ROOT_DIR}/PosixSocketClient
INCLUDES=-I${ROOT_DIR}/Shared/ -I${BASE_SRC_DIR} -I/usr/include/python2.7
LIBRARY=-L/usr/lib/python2.7/config
TARGET=eu
$(TARGET):
$(CXX) $(CXXFLAGS) $(INCLUDES) -o EClientSocketBase.o -c $(BASE_SRC_DIR)/EClientSocketBase.cpp
$(CXX) $(CXXFLAGS) $(INCLUDES) -o EPosixClientSocket.o -c $(BASE_SRC_DIR)/EPosixClientSocket.cpp
$(CXX) $(CXXFLAGS) $(INCLUDES) -o PosixTestClient.o -c PosixTestClient.cpp
$(CXX) $(CXXFLAGS) $(INCLUDES) -o Main.o -c Main.cpp
$(CXX) $(LIBRARY) -lpython2.7 -o $@ EClientSocketBase.o EPosixClientSocket.o PosixTestClient.o Main.o
clean:
rm -f $(TARGET) *.o
该项目编译良好并运行,我所做的唯一更改是在 Main.cpp 文件中添加测试代码。警告/错误消息显示:
在 /usr/include/python2.7/Python.h:8:0 包含的文件中,
来自 Main.cpp:15:
/usr/include/python2.7/pyconfig.h:1158:0:警告:“_POSIX_C_SOURCE”重新定义 [默认启用]
/usr/include/features.h:163:0: 注意:这是之前定义的位置
/usr/include/python2.7/pyconfig.h:1180:0: warning: "_XOPEN_SOURCE " redefined [默认启用]
/usr/include/features.h:165:0: 注意:这是之前定义
g++ -L/usr/lib/ -lpython2.7 -ldl -lutil -o eu EClientSocketBase的位置.o EPosixClientSocket.o PosixTestClient.o Main.o
Main.o:在函数Py_Initialize' /home/bbc/TWS/IBJts/cpp/eu-ats/Main.cpp:82 中:未定义对PyRun_SimpleStringFlags' 的引用main':
/home/bbc/TWS/IBJts/cpp/eu-ats/Main.cpp:81: undefined reference toPyRun_SimpleStringFlags'
/home/bbc/TWS/IBJts/cpp/eu-ats/Main.cpp:83: undefined reference to
/home/bbc/TWS/IBJts/cpp/eu-ats/Main.cpp:84:未定义对Py_Exit'collect2 的 引用:ld 返回 1 退出状态 make:* [eu] 错误 1PyRun_SimpleStringFlags'
/home/bbc/TWS/IBJts/cpp/eu-ats/Main.cpp:85: undefined reference to
有什么帮助吗?谢谢你!