1

I've installed osgearth package with vcpkg with following command:

vcpkg.exe install osgearth:x64-windows

Now in a CMake I want to use it.

cmake_minimum_required (VERSION 3.10.0)

project (osgmap)

add_definitions (-DOSGMAP_EXPORTS)

include_directories (${CMAKE_CURRENT_SOURCE_DIR}/..)
find_package (Osg REQUIRED)
find_package(OsgEarth REQUIRED)

set (PROJECT_SRC
  Dummy.cpp
  )

add_library (${PROJECT_NAME} SHARED ${PROJECT_SRC})
target_link_libraries (${PROJECT_NAME} mapapi)
target_compile_features (${PROJECT_NAME} PUBLIC cxx_std_17)

The problem is that I cannot find osgearth package. I've tried different options.

How can I use OsgEarth installed with vcpkg in a CMake project?

BJ Hargrave
  • 9,324
  • 1
  • 19
  • 27
Jepessen
  • 11,744
  • 14
  • 82
  • 149

2 Answers2

1

The short answer is there are findosg* modules in the cmake distribution and there are not any osgEarth modules to automatically find package paths. Package is either a config or module definition locating both the libs and includes. Vcpkg does not seem to automate package definition and it relies on the modules installed with CMake. What is possible is using find_library and find_path to locate the osgearth files.

find_library(OSGEARTH_LIBRARY osgearth)

assuming you ran vcpkg integrate and use the -DCMAKE_TOOLCHAIN_FILE with cmake. All the libs that were built with vcpkg can be found but not all as packages.

Paul
  • 11
  • 2
1

Once you have properly set up the CMAKE_TOOLCHAIN_FILE you need to provide a FindOsgEarth.cmake file. You could use:

# This module defines

# OSGEARTH_LIBRARY
# OSGEARTH_FOUND, if false, do not try to link to osg
# OSGEARTH_INCLUDE_DIRS, where to find the headers
# OSGEARTH_INCLUDE_DIR, where to find the source headers

# to use this module, set variables to point to the osg build
# directory, and source directory, respectively
# OSGEARTHDIR or OSGEARTH_SOURCE_DIR: osg source directory, typically OpenSceneGraph
# OSGEARTH_DIR or OSGEARTH_BUILD_DIR: osg build directory, place in which you've
#    built osg via cmake

###### headers ######

MACRO( FIND_OSGEARTH_INCLUDE THIS_OSGEARTH_INCLUDE_DIR THIS_OSGEARTH_INCLUDE_FILE )

FIND_PATH( ${THIS_OSGEARTH_INCLUDE_DIR} ${THIS_OSGEARTH_INCLUDE_FILE}
    PATHS
        ${OSGEARTH_DIR}
        $ENV{OSGEARTH_SOURCE_DIR}
        $ENV{OSGEARTHDIR}
        $ENV{OSGEARTH_DIR}
        /usr/local/
        /usr/
        /sw/ # Fink
        /opt/local/ # DarwinPorts
        /opt/csw/ # Blastwave
        /opt/
        [HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session\ Manager\\Environment;OSGEARTH_ROOT]/
        ~/Library/Frameworks
        /Library/Frameworks
    PATH_SUFFIXES
        /include/
)

ENDMACRO( FIND_OSGEARTH_INCLUDE THIS_OSGEARTH_INCLUDE_DIR THIS_OSGEARTH_INCLUDE_FILE )

FIND_OSGEARTH_INCLUDE( OSGEARTH_INCLUDE_DIR       osgEarth/Version )

###### libraries ######

MACRO( FIND_OSGEARTH_LIBRARY MYLIBRARY MYLIBRARYNAME )

FIND_LIBRARY(${MYLIBRARY}
    NAMES
        ${MYLIBRARYNAME}
    PATHS
        ${OSGEARTH_DIR}
        $ENV{OSGEARTH_BUILD_DIR}
        $ENV{OSGEARTH_DIR}
        $ENV{OSGEARTHDIR}
        $ENV{OSG_ROOT}
        ~/Library/Frameworks
        /Library/Frameworks
        /usr/local
        /usr
        /sw
        /opt/local
        /opt/csw
        /opt
        [HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session\ Manager\\Environment;OSGEARTH_ROOT]/lib
        /usr/freeware
    PATH_SUFFIXES
        /lib/
        /lib64/
        /build/lib/
        /build/lib64/
        /Build/lib/
        /Build/lib64/
     )

ENDMACRO(FIND_OSGEARTH_LIBRARY LIBRARY LIBRARYNAME)

FIND_OSGEARTH_LIBRARY( OSGEARTH_LIBRARY osgEarth)
FIND_OSGEARTH_LIBRARY( OSGEARTHFEATURES_LIBRARY osgEarthFeatures)
FIND_OSGEARTH_LIBRARY( OSGEARTHUTIL_LIBRARY osgEarthUtil )
FIND_OSGEARTH_LIBRARY( OSGEARTHQT_LIBRARY osgEarthQt )
FIND_OSGEARTH_LIBRARY( OSGEARTHSYMBOLOGY_LIBRARY osgEarthSymbology )
FIND_OSGEARTH_LIBRARY( OSGEARTHANNOTATION_LIBRARY osgEarthAnnotation )

SET( OSGEARTH_FOUND "NO" )
IF( OSGEARTH_LIBRARY AND OSGEARTH_INCLUDE_DIR )
    SET( OSGEARTH_FOUND "YES" )
    SET( OSGEARTH_INCLUDE_DIRS ${OSGEARTH_INCLUDE_DIR})
    GET_FILENAME_COMPONENT( OSGEARTH_LIBRARIES_DIR ${OSGEARTH_LIBRARY} PATH )
ENDIF( OSGEARTH_LIBRARY AND OSGEARTH_INCLUDE_DIR )

Then add it to your CMakeModules folder and in your CMakeLists.txt:

LIST(APPEND CMAKE_MODULE_PATH "${${PROJECT_NAME}_SOURCE_DIR}/CMakeModules")

FIND_PACKAGE(OsgEarth REQUIRED)
IF(OSGEARTH_FOUND)
    MESSAGE(STATUS "OsgEarth found")
ELSE()
    MESSAGE(STATUS "OsgEarth NOT found")
ENDIF()

INCLUDE_DIRECTORIES(${OSGEARTH_INCLUDE_DIRS})

TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OSGEARTH_LIBRARY} ${OSGEARTHFEATURES_LIBRARY}
    ${OSGEARTHUTIL_LIBRARY} ${OSGEARTHSYMBOLOGY_LIBRARY}
    ${OSGEARTHANNOTATION_LIBRARY} ...)
Luis
  • 678
  • 8
  • 14