本文介绍了g ++用python.h,如何编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用g ++编译一个测试代码,没有任何问题。

  #include 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
可以正常运行。



但是当我尝试嵌入这个代码到另一个项目中,它失败。

  

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文件中添加测试代码。警告/错误消息显示:

有什么帮助吗?

解决方案

查看Lucas对于答案的意见:



为了摆脱_POSIX_C_SOURCE警告,请确保在所有其他头文件之前包含Python.h。



我有同样的问题。我使用Boost Python,所以对我来说,我将boost / python.hpp的include包含在我的.cpp文件的第一行。



(Lukas,一个答案,所以请求的人可以标记为正确的答案,这个问题不会在StackOverflow中保持未回答。)


I compile one test code with g++ without any issue.

#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.7works fine and runs.

But when I try to embed this code into another project, it fails. It really confuses me.

Makefile is like the following.

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

This project compiles fine and runs, the only change I made was adding the test code in the Main.cpp file. warning/error message shows:

any help? thank you!

解决方案

Take a look at Lucas's comment for the answer:

"To get rid of the _POSIX_C_SOURCE warning, make sure to include Python.h before all other header files."

I had the same problem. I use Boost Python, so for me I moved the include of boost/python.hpp to the first line in my .cpp file.

(Lukas, post your comment as an answer so the person who asked can mark it as the right answer, and the question won't remain 'unanswered' in StackOverflow.)

这篇关于g ++用python.h,如何编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:03