I recently installed KDevelop 4 for C++ development on my Macbook Pro running Ubuntu 12.04 LTS.
我最近在我的Macbook Pro运行Ubuntu 12.04 LTS上安装了KDevelop 4,用于c++开发。
I want to embed Python application in my C++ code. To do that, one needs to include the Python.h header file. So, I did that.
我想在c++代码中嵌入Python应用程序。要做到这一点,需要包括Python。h头文件。所以,我这样做。
#include <iostream>
#include <Python.h>
int main(int argc, char **argv) {
Py_Initialize();
return 0;
}
However, on running, I received the following response from the IDE:
但是,在运行时,我收到了来自IDE的响应:
fatal error: Python.h: No such file or directory
However, I found the problem soon enough. I hadn't downloaded the python-dev package. So, I did that. I ran again but the same error was there again. So, I thought it must be an issue with the header file not being included by KDevelop. Thus, I added the relevant folder to the include path and KDevelop immediately recognized that by removing the red underline beneath the second include statement in the code above.
然而,我很快就发现了这个问题。我没有下载python-dev包。所以,我这样做。我又跑了一遍,但同样的错误又出现了。因此,我认为它必须是一个没有被KDevelop包含的头文件的问题。因此,我将相关的文件夹添加到include路径中,KDevelop立即意识到,在第二个include语句中删除红色下划线包含在上面的代码中。
But still, the problem remains. I get the same error. Would appreciate any help or inputs you guys can provide :-)
但问题依然存在。我也有同样的错误。感谢你们提供的任何帮助或投入:-)
Thanks a lot.
非常感谢。
EDIT: Some details that I missed mentioning earlier are that KDevelop is using cmake for my project. I guess the reason my problem is occurring is because cmake doesn't know the appropriate compiler and linker paths. I would appreciate any help in setting the correct paths for cmake.
编辑:我之前漏掉的一些细节是KDevelop正在为我的项目使用cmake。我想我的问题出现的原因是cmake不知道合适的编译器和链接器路径。如果能帮助我为cmake设置正确的路径,我会非常感激。
6 个解决方案
#1
26
In your CMakeLists.txt, try adding the following:
在你CMakeLists。txt,尝试添加以下内容:
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(<your exe or lib> ${PYTHON_LIBRARIES})
For details of the commands, run:
有关命令的详细信息,请运行:
cmake --help-module FindPythonLibs
cmake --help-command find_package
cmake --help-command include_directories
cmake --help-command target_link_libraries
#2
28
sudo apt-get install python2.7-dev
worked for me on a "Python.h: No such file or directory" issue
在“Python”上为我工作。h:没有这样的文件或目录。
#3
10
You want to include the following on the compile line:
您希望在编译行中包含以下内容:
`python-config --cflags`
and this on the link line:
在链接线上:
`python-config --ldflags`
#4
4
Most likely Python.h
is not in your build systems' include path. You can find out where your Python.h is by running
最有可能的Python。h不在您的构建系统中包含路径。您可以找到您的Python。h是通过运行
dpkg -L python-dev | grep Python.h
This will also verify that the python-dev package actually installed a Python.h.
这还将验证python-dev包是否实际安装了python。
I don't have a kdevelop here, but most IDEs have a setting somewhere where you can specify the include path used by the build system, and you should be able to add the path where Python.h lies there.
我这里没有kdevelop,但是大多数ide都有一个设置,您可以指定构建系统使用的include路径,并且您应该能够添加Python的路径。h的谎言。
EDIT:
编辑:
As Nikolai implied, you will also need to add the correct library path for the linking stage. (Output of python-config --ldflags).
正如Nikolai所暗示的,您还需要为链接阶段添加正确的库路径。python-config——ldflags(输出)。
#5
1
I assume that it is already installed. Find the path with:
我假设它已经安装好了。找到的路径:
find / -iname python.h
and when you have done so, when compiling add
当你这样做的时候,编译add。
-I python_h_path
#6
0
For Linux Ubuntu Putty Users try this:
对于Linux Ubuntu Putty用户来说:
sudo apt-get update
sudo apt-get install python-dev
then compile it
然后编译它
gcc -o check xyz.c -I/usr/include/python2.7/ -lpython2.7
then run it
然后运行它
./check
#1
26
In your CMakeLists.txt, try adding the following:
在你CMakeLists。txt,尝试添加以下内容:
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(<your exe or lib> ${PYTHON_LIBRARIES})
For details of the commands, run:
有关命令的详细信息,请运行:
cmake --help-module FindPythonLibs
cmake --help-command find_package
cmake --help-command include_directories
cmake --help-command target_link_libraries
#2
28
sudo apt-get install python2.7-dev
worked for me on a "Python.h: No such file or directory" issue
在“Python”上为我工作。h:没有这样的文件或目录。
#3
10
You want to include the following on the compile line:
您希望在编译行中包含以下内容:
`python-config --cflags`
and this on the link line:
在链接线上:
`python-config --ldflags`
#4
4
Most likely Python.h
is not in your build systems' include path. You can find out where your Python.h is by running
最有可能的Python。h不在您的构建系统中包含路径。您可以找到您的Python。h是通过运行
dpkg -L python-dev | grep Python.h
This will also verify that the python-dev package actually installed a Python.h.
这还将验证python-dev包是否实际安装了python。
I don't have a kdevelop here, but most IDEs have a setting somewhere where you can specify the include path used by the build system, and you should be able to add the path where Python.h lies there.
我这里没有kdevelop,但是大多数ide都有一个设置,您可以指定构建系统使用的include路径,并且您应该能够添加Python的路径。h的谎言。
EDIT:
编辑:
As Nikolai implied, you will also need to add the correct library path for the linking stage. (Output of python-config --ldflags).
正如Nikolai所暗示的,您还需要为链接阶段添加正确的库路径。python-config——ldflags(输出)。
#5
1
I assume that it is already installed. Find the path with:
我假设它已经安装好了。找到的路径:
find / -iname python.h
and when you have done so, when compiling add
当你这样做的时候,编译add。
-I python_h_path
#6
0
For Linux Ubuntu Putty Users try this:
对于Linux Ubuntu Putty用户来说:
sudo apt-get update
sudo apt-get install python-dev
then compile it
然后编译它
gcc -o check xyz.c -I/usr/include/python2.7/ -lpython2.7
then run it
然后运行它
./check