Skip to content

Commit

Permalink
Adding shared library example
Browse files Browse the repository at this point in the history
  • Loading branch information
ifilot committed Dec 25, 2023
1 parent 988005a commit 65bd917
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 5 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,25 @@ 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
- name: Perform unit tests
run: cd build && sudo make install
- name: Produce compilation using shared library
run: |
cd examples && mkdir build && cd build
cmake ../shared
make -j
./den2obj-shared-example
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 2.8.12)
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;
}
6 changes: 1 addition & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,7 @@ add_library(den2objsources STATIC ${SOURCES})
add_library(den2obj SHARED ${SOURCES})
add_executable(den2obj_exec ${CMAKE_CURRENT_SOURCE_DIR}/den2obj.cpp)
set_property(TARGET den2obj_exec PROPERTY OUTPUT_NAME den2obj)
set(DEN2OBJ_HEADERS
isosurface_mesh.h
isosurface.h
scalar_field.h
)
file(GLOB DEN2OBJ_HEADERS "*.h")
set_target_properties(den2obj PROPERTIES PUBLIC_HEADER "${DEN2OBJ_HEADERS}")

# Set C++17
Expand Down

0 comments on commit 65bd917

Please sign in to comment.