Python.h: No such file or directory - c++

I recently installed KDevelop 4 for C++ development on my Macbook Pro running Ubuntu 12.04 LTS.
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.
#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:
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.
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.

In your CMakeLists.txt, try adding the following:
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

sudo apt-get install pythonX.X-dev
For example for 3.8
sudo apt-get install python3.8-dev
Thank you Cristianjs19 for the comment.
Original Answer:
sudo apt-get install python2.7-dev
worked for me on a "Python.h: No such file or directory" issue

You want to include the following on the compile line:
`python-config --cflags`
and this on the link line:
`python-config --ldflags`

Most likely Python.h is not in your build systems' include path. You can find out where your Python.h is by running
dpkg -L python-dev | grep Python.h
This will also verify that the python-dev package actually installed a Python.h.
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.
EDIT:
As Nikolai implied, you will also need to add the correct library path for the linking stage. (Output of python-config --ldflags).

For Linux Ubuntu Putty Users try this:
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

I assume that it is already installed. Find the path with:
find / -iname python.h
and when you have done so, when compiling add
-I python_h_path

Related

How to make cmake find pybind11

I am trying to follow the simple example for embedding python within c++ using pybind11 as found on this page. However, when trying to use cmake to build the solution, I keep getting an error that says
By not providing "Findpybind11.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file
provided by "pybind11", but CMake did not find one.
Could not find a package configuration file provided by "pybind11"
with any of the following names:
pybind11Config.cmake
pybind11-config.cmake
I have a folder called pybindtest on my Desktop which includes CMakeLists.txt and main.cpp as described in the link above, as well as a build folder that I created. While in the build folder, I have tried the following lines to no avail (running on Powershell 7):
cmake ..
cmake .. -Dpybind11_DIR=C:/Users/ben.wolfley/Anaconda3/Library/share/cmake/pybind11/pybind11Config.cmake
cmake .. -DCMAKE_MODULE_PATH=C:/Users/ben.wolfley/Anaconda3/Library/share/cmake/pybind11
I installed pybind11 using conda install pybind11, and pybind11Config.cmake is in C:\Users\ben.wolfley\Anaconda3\Library\share\cmake\pybind11
In case someone having the same issue without Anaconda, like directly with pip pybind11 or manual clone installation, both caused problems in my case. Manual installation of pybind11 with git didn't install the cmake config pybind11Config.cmake, although there is a tools/pybind11Config.cmake.in file, that I couldn't turn into a proper pybind11Config.cmake.
Installation pybind11 global with pip solved it for me, and automatically uninstalled the manual git installation:
pip install "pybind11[global]"
which installed both pybind11 and pybind11-global with proper cmake config like Anaconda does.
Thanks to Tsyvarev for pointing me in the right direction. The following command worked:
cmake .. -G "Visual Studio 15 2017" -A x64 `
-Dpybind11_DIR=C:/Users/ben.wolfley/Anaconda3/Library/share/cmake/pybind11/
I was pointing to the .cmake file instead of the file's directory. I also had to specify the compiler in order for the code to work.
Add following to CMakeLists.txt:
```cmake
# set pybind11 dir
set(pybind11_DIR /Users/Caleb/Softwares/pybind11)
find_package(pybind11 REQUIRED)
```

How to install METIS on ubuntu

I want to install the METIS package on ubuntu.
I tried the instructions on the install.txt file which says to use
$ make install
which I did, after installing make as well.
I also tried the
sudo apt-get install metis
which installed it successfully but
when trying to use it in both cases I get
metis.h: No such file in directory compilation terminated
In case anyone asks I use g++ -I/path/to/file myprogram.cpp to specify the path where metis.h is.
I believe I haven't done something correct in the installation but I can't determine what it is.
Can someone help me with the installation process?
You can try sudo apt-get install libmetis-dev.
BUILD.txt file from metis:
Building METIS requires CMake 2.8, found at http://www.cmake.org/, as
well as GNU make. Assumming CMake and GNU make are installed, two
commands should suffice to build metis:
$ make config
$ make
so, i tried not directly on ubuntu, but on my mac and it works in that order.
after the two make command i have the following folder strucure:
build
-Darwin-x86_64 (the build architecture)
-libmetis
-libmetis.a
-programs
-gpmetis
...
after you can call
make install
I make a little test example and it works. You are maybe interest in my CMake-File. This could be the solution for your problem:
cmake_minimum_required(VERSION 2.8.9)
project (MetisTest)
include_directories("/usr/local/include")
link_directories("/usr/local/lib")
add_executable(metisTest main.cpp)
target_link_libraries(metisTest metis)
You may append your metis installation path to the environment variable CPLUS_INCLUDE_PATH. For example, in your ~/.bashrc, add:
export CPLUS_INCLUDE_PATH=$HOME/metis/include:$CPLUS_INCLUDE_PATH
then,
source ~/.bashrc
Please see more in the question Linux could not find metis.h.

C++ program requiring libsnappy

I am trying to make and build a C++ program (available here https://github.com/mortehu/text-classifier) which requires libsnappy and Capn Proto. I believe I am having an issue with libsnappy as after running the following commands:
configure
make -L/Users/liamflynn/Desktop/Kaggle/Truly_Native/Mortehu/text-classifier-master/tools/text-classifier/capnproto-c++-0.5.3/src
sudo make install
I get the error "base/columnfile.cc:7:10: fatal error: 'snappy.h' file not found"
I have tried linking various folders related to libsnappy in the make stage to no avail. I have also tried to install snappy in a few different ways, such as:
brew install snappy
and
sudo gem install libsnappy
But Im not exactly sure where the library is getting saved too. Ideally, I would be able to find the folder containing 'snappy.h' and I would be able to link this in the make stage. Any help with where I am going wrong would be much appreciated, thank you.
The headers are read by the preprocessor. You can add preprocessor command line flags by changing the CPPFLAGS variable. Use the LDFLAGS variable for linker flags:
./configure CPPFLAGS="-I/path/to/header/" LDFLAGS="-L/path/to/library/"
Since you tagged homebrew I'm assuming that you're on MacOS and have brew installed. With this you can run the following command to install the libsnappy library:
$ brew install snappy

How to add compiler include paths and linker library paths for newly installed Boost?

I have RHEL 5.2, with Boost 1.33 installed.
I downloaded boost_1_44_0.tar.bz2. and built it. On completion it showed:
The Boost C++ Libraries were successfully built!
The following directory should be added to compiler include paths:
/home/dfe/Archive/boost_1_44_0
The following directory should be added to linker library paths:
/home/dfe/Archive/boost_1_44_0/stage/lib
How do I add the above mentioned include paths?
When I do "rpm -q boost", it shows boost-1.33.1-10.el5. Why is that so, when I've installed version 1.44?
Is there a better way to install the latest version of Boost?
There are always three steps to install software on Linux systems:
configure — "check"
make — "build software in current directory"
make install — "copy files to the systems so the other software can use this software"
You likely did the equivalent of make but did not do the equivalent of make install. You need to run
sudo ./b2 install
after running ./b2
Just add the paths to your .bashrc or .profile (or whatever floats your boat) like this:
export LIBS="-L/home/dfe/Archive/boost_1_44_0/stage/lib"
export CPPFLAGS="-I/home/dfe/Archive/boost_1_44_0"
You have to include these directories into makefile which you would use to build your application
CC -I/home/dfe/Archive/boost_1_44_0 -L/home/dfe/Archive/boost_1_44_0/stage/lib yourprogram.cpp
-I option Adds dir to the list of directories that are searched for #include files.
-L option adds dir to the list of directories searched for libraries by linker
CC is sun compiler...
First, I removed the existing boost rpm using
rpm -e boost-1.33.1-10.el5
A message is displayed saying "error: "boost" specifies multiple packages"
Then tried:
rpm -e --allmatches boost
(I don't remember whether I typed 'boost' or 'boost-1.33.1-10.el5')
The packages with dependencies were shown.
I did:
rpm -e [packagename1]
rpm -e [packagename2]
and so on and then did:
rpm -e --allmatches
This erased boost completely from my system.
Then I extracted boost_1_44_0.tar.bz2 using tar -xvjf boost_1_44_0.tar.bz2 and ran bootstrap with:
./bootstrap.sh
Then ran bjam as:
./bjam install
That's it! Boost got installed on my system, and I didn't have to specify any of the linker options while compiling programs! Yay!
Now the 'rpm -q boost' command shows that there is no package installed.

Google protobuf in Linux

I'm working with protobuf and Linux. Where is its compiler protoc? I've downloaded the package from the main site, compiled and installed it successfully but I can't find protoc to build my own format file. Where is it?
UPDATE
Here is folder where I built protobuf:
aclocal.m4 depcomp Makefile.in
autogen.sh editors missing
CHANGES.txt examples protobuf-lite.pc
config.guess generate_descriptor_proto.sh protobuf-lite.pc.in
config.h gtest protobuf.pc
config.h.in install-sh protobuf.pc.in
config.log INSTALL.txt python
config.status java README.txt
config.sub libtool src
configure ltmain.sh stamp-h1
configure.ac m4 vsprojects
CONTRIBUTORS.txt Makefile
COPYING.txt Makefile.am
There isn't a binary file I need.
It's probably installed into /usr/local/bin
On Ubuntu at least, you can apt-get install protobuf-compiler instead.
From the INSTALL.txt:
Installation Names
By default, 'make install' will install the package's files in
'/usr/local/bin', '/usr/local/man', etc. You can specify an
installation prefix other than '/usr/local' by giving 'configure' the
option '--prefix=PATH'.
First, you need to compile you source code from protobuff (in the root folder):
./configure
make
make check
make install
Second:
echo "/usr/local/lib">>/etc/ld.so.conf
echo "/usr/lib">>/etc/ld.so.conf
ldconfig
Third:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
Read README.txt in root folder for more info.
Also, if you are in fact on Ubuntu, then you can also fetch the source package from Debian unstable and rebuild them locally if you want packages that are more current than the last cutoff (which for Ubuntu 9.10 was some time late last summer). That way you end up with .deb packages and you preserve a normal upgrade path (rather than littering /usr/local with one-off installs).
You can find protoc in the path which you set ./configure --prefix=you_path.
When you make install successful, it will general bin, include and lib in that path.
Actually you don't need to know it's place.
All you want is to open terminal where your proto file is located and write at terminal
protoc -I=. --cpp_out=. filename.proto
follow this link for documentation