Google 地球在 Ubuntu 上C++ API 库错误

Google Earth C ++ API Library Errors on Ubuntu

本文关键字:API 错误 C++ 地球 Ubuntu Google      更新时间:2023-10-16

我是这个论坛的新手,但我在尝试寻找这个问题的解决方案时已经看过几次了。我正在尝试使用我为 Ubuntu 找到的 C++ API 库(libkml-dev_1.2.0-1ubuntu6_amd64)生成要在 Google 地球中使用的 KML 文件。我使用命令sudo apt-get install libkml-dev安装了软件包,回来成功了。之后,我使用命令行终端导航到 examples 文件夹,尝试使用命令 g++ helloworld.cc -o helloworld 执行程序"helloworld.cc",但随后出现了一系列错误(主要声称 kmldom 是一个未定义的引用)。抱歉,我想附加文本文件,但不知道如何附加,所以我在下面包含一个错误示例。我整个星期都在搜索高低,确保头文件确实包含在下载中,甚至就此问题联系了Google地球开发人员(他们回答说他们与此无关,并将有关此问题的所有问题重定向到StackOverflow)。

有谁知道导致此问题的原因以及我可以做些什么来解决这个问题,以便我可以继续前进?

<小时 />/

tmp/cc5u2JyV.o: In function HelloKml(bool)': helloworld.cc:(.text+0x17): undefined reference to kmldom::KmlFactory::GetFactory()'helloworld.cc:(.text+0x27):对kmldom::KmlFactory::CreateCoordinates() const' helloworld.cc:(.text+0x328): undefined reference to kmldom::AsPoint(boost::intrusive_ptrkmldom::Element)的未定义引用/tmp/cc5u2JyV.o: In function boost::intrusive_ptr<kmldom::Coordinates>::intrusive_ptr(kmldom::Coordinates*, bool)': helloworld.cc:(.text._ZN5boost13intrusive_ptrIN6kmldom11CoordinatesEEC2EPS2_b[_ZN5boost13intrusive_ptrIN6kmldom11CoordinatesEEC5EPS2_b]+0x3d): undefined reference to kmlbase::intrusive_ptr_add_ref(kmlbase::Referent*)'/tmp/cc5u2JyV.o: In function boost::intrusive_ptr<kmldom::Coordinates>::~intrusive_ptr()': helloworld.cc:(.text._ZN5boost13intrusive_ptrIN6kmldom11CoordinatesEED2Ev[_ZN5boost13intrusive_ptrIN6kmldom11CoordinatesEED5Ev]+0x23): undefined reference to kmlbase::intrusive_ptr_release(kmlbase::Referent*)'/tmp/cc5u2JyV.o: In function boost::intrusive_ptr<kmldom::Geometry>::intrusive_ptr(boost::intrusive_ptr<kmldom::Geometry> const&)': helloworld.cc:(.text._ZN5boost13intrusive_ptrIN6kmldom8GeometryEEC2ERKS3_[_ZN5boost13intrusive_ptrIN6kmldom8GeometryEEC5ERKS3_]+0x35): undefined reference to kmlbase::intrusive_ptr_add_ref(kmlbase::Referent*)'收集2:LD 返回 1 个退出状态

编译使用库的应用程序时,需要在应用程序编译时链接它。因此,请尝试使用以下参数:

要指定用于搜索库的目录,请使用 -L:

-L/data[...]/lib

要指定实际的库名称,请使用 -l:

-labc(链接 abc.a 或 abc.so)

指定要搜索包含文件的目录,请使用 -I:

-I/data[...]/lib

我已经找到了解决方案。问题是你正在直接链接 libkml。您应该找到头文件和库文件 (*.so) 的路径。我在这里找到了它们:

/usr/include/kml/(包括带有标题的 dit),
/usr/lib/x86_64-linux-gnu/(图书馆目录)。

我使用 CMake

和 CMakeLists.txt 用于一个项目。使用 libkml 可能如下所示:

`cmake_minimum_required (VERSION 3.2)
# Set language standard
set(CMAKE_CXX_STANDARD "11")
project (test_proj)
add_definitions(-std=c++11)
# Set default build type to RelWithDebInfo if not specified
if (NOT CMAKE_BUILD_TYPE)
    message (STATUS "Default CMAKE_BUILD_TYPE not set using Release with Debug Info")
    set (CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE
        STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel"
        FORCE)
endif()
# linking boost library
find_package(Boost COMPONENTS system filesystem thread REQUIRED)
if(NOT Boost_FOUND)
    message(SEND_ERROR "Failed to find boost.")
    return()
else()
    include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(test_proj main.cpp)
set (LibKML_INCLUDE_DIRS /usr/include/kml/)
set (LibKML_LIBRARIES /usr/lib/x86_64-linux-gnu/libkmlbase.so /usr/lib/x86_64-linux-gnu/libkmlconvenience.so /usr/lib/x86_64-linux-gnu/libkmldom.so /usr/lib/x86_64-linux-gnu/libkmlengine.so /usr/lib/x86_64-linux-gnu/libkmlregionator.so /usr/lib/x86_64-linux-gnu/libkmlxsd.so)
message("LibKML is at: ${LibKML_INCLUDE_DIRS} and ${LibKML_LIBRARIES}")
message("Boost is at: ${Boost_INCLUDE_DIRS} and ${Boost_LIBRARIES}")
target_include_directories(test_proj 
PUBLIC 
${LibKML_INCLUDE_DIRS} 
${Boost_INCLUDE_DIRS} ) 
target_link_libraries(test_proj 
PUBLIC
${LibKML_LIBRARIES} ${Boost_LIBRARIES})
install(TARGETS test_proj EXPORT test_proj_export)
export(EXPORT test_proj_export FILE cmake/test_proj-targets.cmake)`