matplotlib-cpp
matplotlib-cpp copied to clipboard
Cannot link nor find matplotlib in cmake
Hey guys, Iam currently a bit frustrated trying to link the matplotlib in my ros project via cmake and catkin. Whenever i run catkin_make, which basically run cmakefiles in multiple directories, i get an error message that Python(lib) cannot be found. Im trying to solve this for hours now and got multiple errors, so Im showiung you the current state. Would be awesome if someone could help me out!
The important parts of the CMakeList which is run by catkin:
`cmake_minimum_required(VERSION 2.8.3)
project(vector_thruster)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
tf
Eigen3
PythonLibs 2.7
#Python3 COMPONENTS Development NumPy
#Python ${PY_VERSION}
)
include_directories(
${PYTHON_INCLUDE_DIRS}
${catkin_INCLUDE_DIRS}
${EIGEN3_INCLUDE_DIRS}
"/usr/include/python2.7"
)
add_executable(plotter src/plotter.cpp)
target_include_directories(plotter PRIVATE ${PYTHON_INCLUDE_DIRS})
target_link_libraries(plotter ${PYTHON_LIBRARIES})
#target_link_libraries(plotter ${catkin_LIBRARIES} #${PYTHON_LIBRARIES})
add_dependencies(plotter vector_thruster_gencpp)
`
This is the error message i get momentarily: -- Could NOT find PythonLibs (missing: PythonLibs_DIR) -- Could not find the required component 'PythonLibs'. The following CMake error indicates that you either need to install the package with the same name or change your environment so that it can be found. CMake Error at /opt/ros/melodic/share/catkin/cmake/catkinConfig.cmake:83 (find_package): Could not find a package configuration file provided by "PythonLibs" with any of the following names:
PythonLibsConfig.cmake
pythonlibs-config.cmake
Add the installation prefix of "PythonLibs" to CMAKE_PREFIX_PATH or set "PythonLibs_DIR" to a directory containing one of the above files. If "PythonLibs" provides a separate development package or SDK, be sure it has been installed. Call Stack (most recent call first): vector_thruster/CMakeLists.txt:10 (find_package)
So I get that the error message is giving a hint, thing is i cant locate PythonLibsConfig.cmake or something similar on my system. There are indeed those FindPython files which are obviously not called in this cmake file. I also tried to set the PythonLib_DIR in the .bashrc like this: export PythonLib_DIR=/usr/lib/x86_64-linux-gnu this is the directory where i found one copy of libpython2.7.so
As you can maybe tell im using Ubuntu.
Thanks in advance
Here is my minimal example for matplotlibcpp usage with cmake
# CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project("example" LANGUAGES CXX)
# guard against in-source builds
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt. ")
endif()
# find python libraries
find_package(Python3 COMPONENTS Interpreter Development NumPy REQUIRED)
find_package(PythonLibs 3.0 REQUIRED)
include_directories(${PYTHON3_INCLUDE_DIRS} ${NumPy_INCLUDE_DIRS})
# populate matplotlib repository
include(FetchContent)
FetchContent_Declare(
matplotlib
GIT_REPOSITORY https://github.com/lava/matplotlib-cpp.git
GIT_TAG f23347fca25219d1c42cbb91608b5556814bf572
)
FetchContent_GetProperties(matplotlib)
if(NOT matplotlib_POPULATED)
FetchContent_Populate(matplotlib)
endif()
include_directories(SYSTEM ${matplotlib_SOURCE_DIR})
# add executable
add_executable(example example.cpp)
# link python and numpy
target_link_libraries(example
PRIVATE
${PYTHON_LIBRARIES}
Python3::NumPy
)
and the minimal example
// example.cpp
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
plt::plot({1,3,2,4});
plt::show();
}
I hope this helps ;)
@JonasHarsch Hi Jonas, i was able to compile your example. Do you know if it's possible to link the Python and Matplotlib libraries statically instead of dynamically?
@bolu-atx, sorry I'm not aware of that. But after a little research I'm of the opinion that it is not a good idea to try this. All python libraries are linked dynamicall only the python Base can bei used as static library.
Hello, in spite of having numpy and matplotlib, I am getting this error and cannot get the minimal code to work. I checked running a python file and it does not throw errors for numpy and matplotlib imports. I am using Ubuntu 22.04 and Python 3.9 (with Anaconda). The build is successful but executing the executable throws this error-
ModuleNotFoundError: No module named 'numpy' ModuleNotFoundError: No module named 'matplotlib' terminate called after throwing an instance of 'std::runtime_error' what(): Error loading module matplotlib! Aborted (core dumped)
I figure out that the conda python3.9 was not the Ubuntu python, instead I installed numpy and matplotlib for the Ubuntu python3.10 and it looks working now.
@JonasHarsch Why exactly isn't FetchContent_MakeAvailable used, since that is the recommended approach to use FetchContent? Also, why the need to specify the include directories - wouldn't just linking the target be enough? (I'm not a CMake expert, but this package CMake configuration seems a bit of a mess currently).
@joaocandre: Sure, you can replace
FetchContent_GetProperties(matplotlib)
if(NOT matplotlib_POPULATED)
FetchContent_Populate(matplotlib)
endif()
FetchContent_MakeAvailable(matplotlib)
But adding the include directories is mandatory.