8000 made it a library · lava/matplotlib-cpp@0dd80dd · GitHub
[go: up one dir, main page]

Skip to content

Commit 0dd80dd

Browse files
committed
made it a library
1 parent d6fd842 commit 0dd80dd

12 files changed

+6321
-15
lines changed

CMakeLists.txt

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,64 @@
11
# CMakeLists.txt
22
cmake_minimum_required(VERSION 3.15)
3-
project("example" LANGUAGES CXX)
3+
project(matplotlibcpp LANGUAGES CXX)
44

5+
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install) # comment this if you want to install in /usr/local
6+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") # neede for include(...)
57

68
set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS 1 CACHE INTERNAL "No dev warnings") # bypass dev warnings
79

10+
# -------------------------------------------------------------------------- #
11+
# Build C++ library #
12+
# -------------------------------------------------------------------------- #
13+
14+
set(LIBRARY_NAME matplotlibcpp_lib)
15+
816
# find python libraries
917
find_package(Python3 COMPONENTS Interpreter Development NumPy REQUIRED)
1018
find_package(PythonLibs 3.0 REQUIRED)
1119
include_directories(${PYTHON3_INCLUDE_DIRS} ${NumPy_INCLUDE_DIRS})
1220

13-
# populate matplotlib repository
14-
include_directories(include/)
21+
# add library
22+
add_library(${LIBRARY_NAME} SHARED
23+
src/matplotlibcpp.cpp
24+
)
1525

16-
# add executable
17-
add_executable(minimal examples/minimal.cpp)
18-
add_executable(animated_3d examples/animated_3d.cpp)
26+
target_include_directories(${LIBRARY_NAME} PUBLIC
27+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
28+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
29+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>
30+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/src>
31+
)
1932

20-
# link python and numpy
21-
target_link_libraries(minimal
22-
PRIVATE
33+
target_link_libraries(${LIBRARY_NAME}
2334
${PYTHON_LIBRARIES}
2435
Python3::NumPy
2536
)
26-
target_link_libraries(animated_3d
27-
PRIVATE
28-
${PYTHON_LIBRARIES}
29-
Python3::NumPy
30-
)
37+
38+
set_target_properties(
39+
${LIBRARY_NAME}
40+
PROPERTIES
41+
CXX_STANDARD 17
42+
CXX_STANDARD_REQUIRED YES
43+
CXX_EXTENSIONS NO
44+
)
45+
46+
# -------------------------------------------------------------------------- #
47+
# 8000 Install #
48+
# -------------------------------------------------------------------------- #
49+
install(TARGETS ${LIBRARY_NAME}
50+
EXPORT ${LIBRARY_NAME}
51+
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
52+
53+
install(EXPORT ${LIBRARY_NAME}
54+
FILE ${LIBRARY_NAME}.cmake
55+
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake)
56+
57+
install(FILES cmake/${LIBRARY_NAME}-config.cmake
58+
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake)
59+
60+
install(DIRECTORY include/
61+
DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
62+
63+
install(DIRECTORY src/
64+
DESTINATION ${CMAKE_INSTALL_PREFIX}/src)

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,33 @@
33
This is a fork of the original [matplotlib-cpp](https://github.com/lava/matplotlib-cpp). Its characterized for being:
44

55
- minimal: Just the necessary files.
6-
- cmake-friendly: Compile from cmake!
6+
- library: Install it as a library for usage in other projects.
77
- extended 3D rendering: Copied from [this video](https://www.youtube.com/watch?v=NOZDyFmWDtw)
8+
9+
## Installation
10+
11+
Build and install the library with `install.sh` as follows:
12+
13+
```bash
14+
source install_cpp.sh
15+
```
16+
17+
## Usage
18+
19+
If you have successfully built the project, you are ready to start using the installed library. By default matplotlibcpp will be installed as a shared library in `/path/to/matplotlibcpp/install/`, and thereby, it can easily be linked to other projects using CMakeLists.txt as follows:
20+
21+
```cmake
22+
set(CMAKE_PREFIX_PATH ${CMAKE_SOURCE_DIR}/../install/lib/cmake)
23+
24+
find_package(matplotlibcpp_lib REQUIRED)
25+
26+
add_executable(my_excutable my_executable.cpp)
27+
target_link_libraries(my_executable matplotlibcpp_lib)
28+
29+
```
30+
31+
For exemplary files that showcase how to use the library can be found in [the examples folder](examples/). You can run an example by running:
32+
33+
```bash
34+
bash examples/run_examples.sh
35+
```

cmake/matplotlibcpp_lib-config.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include(CMakeFindDependencyMacro)
2+
3+
find_dependency(Python3 COMPONENTS Interpreter Development NumPy REQUIRED)
4+
find_dependency(PythonLibs 3.0 REQUIRED)
5+
6+
get_filename_component(SELF_DIR ${CMAKE_CURRENT_LIST_DIR} PATH)
7+
include(${SELF_DIR}/cmake/matplotlibcpp_lib.cmake)

examples/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(matplotlibcpp_examples)
3+
4+
# set path to motplit_lib installation( comment if installed in /usr/local)
5+
set(CMAKE_PREFIX_PATH ${CMAKE_SOURCE_DIR}/../install/lib/cmake)
6+
7+
# to avoid PCL warnings
8+
set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS 1 CACHE INTERNAL "No dev warnings") # bypass PCL warnings
9+
10+
# find motplit_lib package
11+
find_package(matplotlibcpp_lib REQUIRED)
12+
13+
# add executable and link libraries
14+
add_executable(animated_3d animated_3d.cpp)
15+
target_link_libraries(animated_3d matplotlibcpp_lib)
16+
17+
add_executable(minimal minimal.cpp)
18+
target_link_libraries(minimal matplotlibcpp_lib)
File renamed without changes.

install.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Check if the build directory exists
2+
if [ -d "build" ]; then
3+
echo "Removing existing build directory..."
4+
rm -rf build
5+
fi
6+
7+
# Create a new build directory and navigate into it
8+
mkdir build && cd build
9+
10+
# Run CMake and make based on the selected build type
11+
cmake -DPython3_EXECUTABLE=$(which python) -DCMAKE_INSTALL_PREFIX=$pwd/install ..
12+
make
13+
14+
# Install the executable
15+
make install
16+
17+
# # Run the executable
18+
# ./minimal
19+
# ./animated_3d

0 commit comments

Comments
 (0)
0