Skip to content

Commit

Permalink
Creating shared library (#9)
Browse files Browse the repository at this point in the history
* Fixing errors in documentation

* Initial commit creating shared library

* Adding pkg-config file

* Adding shared library example

* Removing fprofile-arcs from shared library

* Expanding README.md

* Setting LD LIBRARY PATH

* Incrementing CMake version
  • Loading branch information
ifilot authored Apr 10, 2024
1 parent 9c1c19f commit 252a60e
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 12 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,24 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./build/coverage.xml

test-shared:

runs-on: ubuntu-latest
needs: build

steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: sudo apt install -y build-essential cmake libglm-dev libtclap-dev libboost-all-dev libopenvdb-dev libtbb-dev libcppunit-dev libeigen3-dev liblzma-dev zlib1g-dev libbz2-dev gcovr
- name: Configure CMake
run: mkdir build && cd build && cmake ../src
- name: Build
run: cd build && make -j && sudo make install
- name: Produce compilation using shared library
run: |
cd examples && mkdir build && cd build
cmake ../shared
make -j
ldd ./den2obj-shared-example
LD_LIBRARY_PATH=/usr/local/lib ./den2obj-shared-example
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ build/*
CHGCAR*
*.png
src/config.h
src/den2obj.pc

# do not save doc build
docs/_build/
Expand Down
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ Gaussian .cube files as well as its own .d2o format.
### Debian Latest

Getting the dependencies
```

```bash
sudo apt install build-essential cmake libtclap-dev libboost-all-dev libopenvdb-dev libtbb-dev \
pkg-config libcppunit-dev libeigen3-dev liblzma-dev zlib1g-dev
pkg-config libcppunit-dev libeigen3-dev liblzma-dev zlib1g-dev libbz2-dev libssl-dev
```

To compile, run the following commands:
```

```bash
git clone https://github.com/ifilot/den2obj.git
cd den2obj
mkdir build
Expand All @@ -37,9 +39,20 @@ make -j5

### Ubuntu Latest

The stable OpenVDB library (`libopenvdb`) under Ubuntu is incompatible with the Threading Building Blocks (`libtbb`) library. To solve this, manually compile and install OpenVDB 8.2 using the following instructions.
The stable OpenVDB library (`libopenvdb`) under Ubuntu is incompatible with the
Threading Building Blocks (`libtbb`) library. To solve this, manually compile
and install OpenVDB 8.2 using the following instructions.

Getting the dependencies

```bash
sudo apt install build-essential cmake libtclap-dev libboost-all-dev libopenvdb-dev libtbb-dev \
pkg-config libcppunit-dev libeigen3-dev liblzma-dev zlib1g-dev libbz2-dev libssl-dev
```

Next download and install OpenVDB.

```bash
get https://github.com/AcademySoftwareFoundation/openvdb/archive/refs/tags/v8.2.0.tar.gz
tar -xvzf v8.2.0.tar.gz
mkdir openvdb-build && cd openvdb-build && cmake ../openvdb-8.2.0 -DCMAKE_INSTALL_PREFIX=/opt/openvdb
Expand All @@ -48,7 +61,7 @@ make -j9 && sudo make install

Thereafter, clone, configure and compile Den2Obj and link against OpenVDB 8.2.

```
```bash
git clone https://github.com/ifilot/den2obj.git
cd den2obj
mkdir build
Expand Down Expand Up @@ -108,3 +121,8 @@ The D2O file format is native to `Den2Obj`. This file format stores the scalarfi
in binary format and uses compression to generate small files which are fast to
read from. More information on the file format can be found in the
[documentation](https://den2obj.imc-tue.nl).

## Shared library

`Den2Obj` can also be used as a shared library in your own code. See the
[examples/shared](examples/shared) for an example.
4 changes: 3 additions & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ libraries are available to you:
* `GZIP <https://www.gnu.org/software/gzip/>`_ (gzip data compression)
* `LZMA <https://7-zip.org/>`_ (lzma data compression)
* `CPPUnit <https://sourceforge.net/projects/cppunit/>`_ (unit testing)
* `OpenSSL <https://www.openssl.org/>`_ (unit testing; MD5 checksums)

.. note::
* The instructions covered in this guide assume you are running a
Expand All @@ -32,7 +33,8 @@ libraries are available to you:
To ensure that all the packages are installed, one can run the following::

sudo apt install build-essential cmake libtclap-dev libboost-all-dev \
pkg-config libcppunit-dev libeigen3-dev liblzma-dev zlib1g-dev libbz2-dev
pkg-config libcppunit-dev libeigen3-dev liblzma-dev zlib1g-dev libbz2-dev \
libssl-dev

Standard compilation
--------------------
Expand Down
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/*
71 changes: 71 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Examples

This folder contains a number of scripts how to use Den2Obj as a (shared)
library in your own programs and applications.

## Requirements

To use Den2Obj as a shared library in your applications, you need to dynamically
link against `libden2obj.so` as well as against a number of required libraries:

* Boost (regex, iostreams and filesystem)
* GZIP
* LZMA
* BZ2

Besides these libraries, there is also a header-only dependency on the Eigen3
library.

A convenient way to ensure this in your application is by making use of
`CMake` and using the following settings in your `CmakeLists.txt file`

```cmake
# use Boost
SET(BOOST_INCLUDEDIR "/usr/include")
SET(BOOST_LIBRARYDIR "/usr/lib/x86_64-linux-gnu")
# set Boost
set (Boost_NO_SYSTEM_PATHS ON)
set (Boost_USE_MULTITHREADED ON)
set (Boost_USE_STATIC_LIBS OFF)
set (Boost_USE_STATIC_RUNTIME OFF)
set (BOOST_ALL_DYN_LINK OFF)
# use OpenMP
find_package(OpenMP)
if (OPENMP_FOUND)
option(HAS_OPENMP "OpenMP enabled" ON)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
# include libraries
find_package(PkgConfig REQUIRED)
find_package(Boost COMPONENTS regex iostreams filesystem REQUIRED)
find_package(LibLZMA REQUIRED)
find_package(ZLIB REQUIRED)
find_package(BZip2 REQUIRED)
pkg_check_modules(DEN2OBJ den2obj REQUIRED)
pkg_check_modules(EIGEN eigen3 REQUIRED)
```

and finally add the required libraries to your executable

```cmake
target_link_libraries(den2obj-shared-example
${DEN2OBJ_LIBRARIES}
${Boost_LIBRARIES}
${LIBLZMA_LIBRARIES}
${ZLIB_LIBRARIES}
${BZIP2_LIBRARIES})
```

An example of this is provided in [shared/CMakeLists.txt](shared/CMakeLists.txt).

## Compilation instructions

```bash
mkdir build && cd build
cmake ../shared
make -j
```
84 changes: 84 additions & 0 deletions examples/shared/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#*************************************************************************
# CMakeLists.txt -- This file is part of DEN2OBJ. *
# *
# Author: Ivo Filot <i.a.w.filot@tue.nl> *
# *
# DEN2OBJ is free software: you can redistribute it and/or modify *
# it under the terms of the GNU General Public License as published *
# by the Free Software Foundation, either version 3 of the License, *
# or (at your option) any later version. *
# *
# DEN2OBJ is distributed in the hope that it will be useful, *
# but WITHOUT ANY WARRANTY; without even the implied warranty *
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
# See the GNU General Public License for more details. *
# *
# You should have received a copy of the GNU General Public License *
# along with this program. If not, see http://www.gnu.org/licenses/. *
# *
#*************************************************************************/

# set minimum cmake requirements
cmake_minimum_required(VERSION 3.5)
project (den2obj-shared-example)

# add custom directory to look for .cmake files
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake/modules )

# Enable release build
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

# add OS specific
SET(BOOST_INCLUDEDIR "/usr/include")
SET(BOOST_LIBRARYDIR "/usr/lib/x86_64-linux-gnu")

# set Boost
set (Boost_NO_SYSTEM_PATHS ON)
set (Boost_USE_MULTITHREADED ON)
set (Boost_USE_STATIC_LIBS OFF)
set (Boost_USE_STATIC_RUNTIME OFF)
set (BOOST_ALL_DYN_LINK OFF)

# use OpenMP
find_package(OpenMP)
if (OPENMP_FOUND)
option(HAS_OPENMP "OpenMP enabled" ON)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

# include libraries
find_package(PkgConfig REQUIRED)
find_package(Boost COMPONENTS regex iostreams filesystem REQUIRED)
find_package(LibLZMA REQUIRED)
find_package(ZLIB REQUIRED)
find_package(BZip2 REQUIRED)
pkg_check_modules(DEN2OBJ den2obj REQUIRED)
pkg_check_modules(EIGEN eigen3 REQUIRED)

# Set include folders
include_directories(${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_BINARY_DIR}
${EIGEN_INCLUDE_DIRS}
${DEN2OBJ_INCLUDE_DIR}
${Boost_INCLUDE_DIRS})

# Add sources
file(GLOB SOURCES "*.cpp")
add_executable(den2obj-shared-example ${SOURCES})

# Set C++17
add_definitions(-std=c++17 -march=native)

# Link libraries
target_link_libraries(den2obj-shared-example
${DEN2OBJ_LIBRARIES}
${Boost_LIBRARIES}
${LIBLZMA_LIBRARIES}
${ZLIB_LIBRARIES}
${BZIP2_LIBRARIES})
16 changes: 16 additions & 0 deletions examples/shared/test_shared.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <den2obj/scalar_field.h>
#include <den2obj/generator.h>
#include <memory>
#include <string>

int main() {
// construct scalar field
Generator gen;
const std::string filename = "genus2.d2o";
gen.build_dataset("genus2", filename, D2OFormat::CompressionAlgo::BZIP2);

auto sf = std::make_unique<ScalarField>(filename,
ScalarFieldInputFileType::SFF_D2O);

return 0;
}
26 changes: 20 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#*************************************************************************/

# set minimum cmake requirements
cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.5)
project (den2obj)

# change compiler directives when code coverage is required
Expand All @@ -34,7 +34,7 @@ if(USE_GCOV)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lgcov -fprofile-arcs -ftest-coverage")

# Link flags used for creating shared libraries
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lgcov -profile-arcs -ftest-coverage")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lgcov -ftest-coverage")
endif()

# add custom directory to look for .cmake files
Expand All @@ -51,10 +51,11 @@ execute_process(
# prepare configuration file
SET(PROGNAME "DEN2OBJ")
SET(VERSION_MAJOR "1")
SET(VERSION_MINOR "1")
SET(VERSION_MINOR "2")
SET(VERSION_MICRO "0")
message(STATUS "Writing configuration file in: ${CMAKE_CURRENT_SOURCE_DIR}/config.h")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/config.h @ONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/den2obj.pc.in ${CMAKE_BINARY_DIR}/den2obj.pc @ONLY)

# Enable release build
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
Expand Down Expand Up @@ -112,14 +113,18 @@ endif()
file(GLOB SOURCES "*.cpp")
list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/den2obj.cpp)
add_library(den2objsources STATIC ${SOURCES})
add_executable(den2obj ${CMAKE_CURRENT_SOURCE_DIR}/den2obj.cpp)
add_library(den2obj SHARED ${SOURCES})
add_executable(den2obj_exec ${CMAKE_CURRENT_SOURCE_DIR}/den2obj.cpp)
set_property(TARGET den2obj_exec PROPERTY OUTPUT_NAME den2obj)
file(GLOB DEN2OBJ_HEADERS "*.h")
set_target_properties(den2obj PROPERTIES PUBLIC_HEADER "${DEN2OBJ_HEADERS}")

# Set C++17
add_definitions(-std=c++17 -march=native)

# Link libraries
#SET(CMAKE_EXE_LINKER_FLAGS "-Wl,-rpath=\$ORIGIN/lib")
target_link_libraries(den2obj
target_link_libraries(den2obj_exec
den2objsources
${OPENVDB_LIBS}
${Boost_LIBRARIES}
Expand All @@ -138,4 +143,13 @@ endif()
###
# Installing
##
install (TARGETS den2obj DESTINATION bin)
include(GNUInstallDirs)

# install shared library and header files
install (TARGETS den2obj
DESTINATION bin
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/den2obj)

# install pkg-config files
install(FILES ${CMAKE_BINARY_DIR}/den2obj.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
12 changes: 12 additions & 0 deletions src/den2obj.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=@CMAKE_INSTALL_PREFIX@
libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@

Name: @PROJECT_NAME@
Description: @PROJECT_DESCRIPTION@
Version: @VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_MICRO@

Requires:
Libs: -L${libdir} -lden2obj
Cflags: -I${includedir}

0 comments on commit 252a60e

Please sign in to comment.