Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statically linked g2o command line application #343

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.12)
set(CMAKE_LEGACY_CYGWIN_WIN32 0)

project(g2o)
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
# g2o - static
### Static executable
This fork adds cmake build targets to compile a static version of the g2o command line
executable (cli). The static binary can be useful in situations where a installation/compilation
is not possible on a target system. The static binary includes all dependencies and therefore does
not require any dynamic libraries at runtime.

### Dynamic library without dependencies
Furthermore we provide a dynamic g2o library that mimics the command line client interface.
Dependencies are also linked into the library to provide a maximum of encapsulation.

### Building
Setting up cmake
```
mkdir build
cd build
cmake -DG2O_BUILD_LINKED_APPS=ON \
-DBUILD_CSPARSE=ON \
-DG2O_USE_OPENGL=OFF \
-DG2O_USE_OPENMP=OFF \
-DBUILD_SHARED_LIBS=OFF \
-DBUILD_LGPL_SHARED_LIBS=OFF \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
..
```
To build the library run

`make g2o_lib`

To build the static g2o cli executable

`make g2o_cli_application_static`

The static executable should then be in `bin/g2o_static` while the library is located in `lib/libg2o_lib.so`

***************************************
g2o - General Graph Optimization
================================

Expand Down
2 changes: 2 additions & 0 deletions g2o/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ endif()

if(G2O_BUILD_LINKED_APPS)
add_subdirectory(linked_binaries)
add_subdirectory(g2o_cli_static)
add_subdirectory(g2o_lib)
endif()
26 changes: 23 additions & 3 deletions g2o/apps/g2o_cli/g2o.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
#include <algorithm>
#include <cassert>

#include "dl_wrapper.h"
#include "output_helper.h"
#include "g2o_common.h"
#include "g2o/apps/g2o_cli/dl_wrapper.h"
#include "g2o/apps/g2o_cli/output_helper.h"
#include "g2o/apps/g2o_cli/g2o_common.h"

#include "g2o/config.h"
#include "g2o/core/estimate_propagator.h"
Expand All @@ -57,6 +57,25 @@
#include "g2o/stuff/string_tools.h"
#include "g2o/stuff/timeutil.h"

// include dependencies into the g2o cli
#ifdef G2O_STATIC_LINK_LIBRARIES
G2O_USE_TYPE_GROUP(slam2d);
G2O_USE_TYPE_GROUP(slam2d_segment);
G2O_USE_TYPE_GROUP(slam3d);
G2O_USE_TYPE_GROUP(slam3d_addons);
G2O_USE_TYPE_GROUP(sba);

// static linking of cholmod does not work due to OpenMP
G2O_USE_OPTIMIZATION_LIBRARY(eigen);
G2O_USE_OPTIMIZATION_LIBRARY(csparse);
G2O_USE_OPTIMIZATION_LIBRARY(pcg);
G2O_USE_OPTIMIZATION_LIBRARY(dense);
G2O_USE_OPTIMIZATION_LIBRARY(slam2d_linear);
G2O_USE_OPTIMIZATION_LIBRARY(structure_only);

G2O_USE_ROBUST_KERNEL(RobustKernelHuber);
#endif // G2O_STATIC_LINK_LIBRARIES

static bool hasToStop=false;

using namespace std;
Expand Down Expand Up @@ -188,6 +207,7 @@ int main(int argc, char** argv)

OptimizationAlgorithmFactory* solverFactory = OptimizationAlgorithmFactory::instance();
if (listSolvers) {
cout << "Listing solvers" << endl;
solverFactory->listSolvers(cout);
}

Expand Down
62 changes: 62 additions & 0 deletions g2o/apps/g2o_cli_static/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Provide a build target to statically link the
# g2o commandline application. The resulting binary
# includes all dependencies and can work as a
# standalone application.
#
# Static linking does not work with opengl or shared libs.
# Use the following cmake configuration:
#
# -DG2O_USE_OPENGL=OFF
# -DBUILD_LGPL_SHARED_LIBS=OFF
# -DBUILD_SHARED_LIBS=OFF
# -DG2O_BUILD_LINKED_APPS=ON
#
if (NOT (G2O_USE_OPENGL OR BUILD_LGPL_SHARED_LIBS OR BUILD_SHARED_LIBS))
# disable dynamic loading of libraries in the standard apps
add_compile_definitions(G2O_DISABLE_DYNAMIC_LOADING_OF_LIBRARIES)
add_compile_definitions(G2O_STATIC_LINK_LIBRARIES)

# statically linked cli executable
add_executable(g2o_cli_application_static
../g2o_cli/g2o.cpp)

include_directories(${CSPARSE_INCLUDE_DIR})

# enable static linking of the g2o_cli application
# (MacOS does not support static binaries)
if (NOT APPLE)
target_link_libraries(g2o_cli_application_static -static)
endif ()
target_link_libraries(g2o_cli_application_static g2o_cli_library)
set_target_properties(g2o_cli_application_static PROPERTIES OUTPUT_NAME g2o_static${EXE_POSTFIX})

# linking solvers
target_link_libraries(g2o_cli_application_static
solver_csparse
solver_pcg
solver_dense
solver_slam2d_linear
solver_structure_only
solver_eigen)

# linking types
target_link_libraries(g2o_cli_application_static
types_slam2d
types_slam2d_addons
types_slam3d
types_slam3d_addons
types_sba
types_sclam2d
)

install(TARGETS g2o_cli_application_static
RUNTIME DESTINATION ${RUNTIME_DESTINATION}
LIBRARY DESTINATION ${LIBRARY_DESTINATION}
ARCHIVE DESTINATION ${ARCHIVE_DESTINATION}
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)

file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/apps/linked_binaries)

endif ()
53 changes: 53 additions & 0 deletions g2o/apps/g2o_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Dynamic g2o library without dependencies.
#
# Create a g2o library that mimics the g2o cli executable and
# includes all solvers, types, and kernels.
#
# -DG2O_USE_OPENGL=OFF
# -DBUILD_LGPL_SHARED_LIBS=OFF
# -DBUILD_SHARED_LIBS=OFF
# -DCMAKE_POSITION_INDENPENDENT_CODE=ON
#
if (NOT (G2O_USE_OPENGL OR BUILD_LGPL_SHARED_LIBS OR BUILD_SHARED_LIBS OR NOT CMAKE_POSITION_INDEPENDENT_CODE))
# disable dynamic loading of libraries in the standard apps
add_definitions(-DG2O_DISABLE_DYNAMIC_LOADING_OF_LIBRARIES)

# g2o library without dependencies
add_library(g2o_lib SHARED
g2o_lib.cpp
)

include_directories(${CSPARSE_INCLUDE_DIR})

target_link_libraries(g2o_lib g2o_cli_library)

# linking solvers
target_link_libraries(g2o_lib
solver_csparse
solver_pcg
solver_dense
solver_slam2d_linear
solver_structure_only
solver_eigen)

# linking types
target_link_libraries(g2o_lib
types_slam2d
types_slam2d_addons
types_slam3d
types_slam3d_addons
types_sba
types_sclam2d
)

install(TARGETS g2o_lib
RUNTIME DESTINATION ${RUNTIME_DESTINATION}
LIBRARY DESTINATION ${LIBRARY_DESTINATION}
ARCHIVE DESTINATION ${ARCHIVE_DESTINATION}
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)

file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/apps/linked_binaries)

endif ()
Loading