日常知识点之随手问题整理(vcpkg安装osgearth并进行测试简单整理)
进了一家公司,相关业务与三维和地球显示有关,就想着把osg系列了解了解,尝试在windows安装测试了一下osgearth,稍做总结:
1:简单汇总一下练习流程。
核心还是参考github,根据md文件进行测试,这里第一次尝试,目标是运行起带地球的demo.
2:稍微回顾汇总一下遇到的问题
===》1:vcpkg安装需要网络状态良好,磁盘空间稍微充足(多预留15G以上吧)。
===》2:在没有配置环境变量的前提下,使用网上的简单demo可以运行,但是地球demo一直异常,并且运行界面(demo是全屏)一直没反应蓝屏。
=========》1:首先调整电脑显卡相关,更新驱动,优化显卡选项是独显,依然蓝屏。
=========》2:特别注意,32位/64位,以及debug和release的选择,以及地球的显示除了依赖bin目录下,还以来plugins目录下dll。
=========》3:可以先不关注demo,用自带的工具进行测试,排除问题,验证自己写的.earth文件的正确性。
=========》4:注意环境变量的配置。 这里配置了64位的环境变量,分别支持debug和release,还有注意plugins同样要区分
这是我的相关环境变量路径配置,配置后demo可以正常运行。
===》3:debug版本下运行时一直报崩溃问题(0x00007FFFE306EE2B (libprotobufd.dll)处(位于 CMakeProject1.exe 中)引发的异常: 0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突。)
=========》最终排查是debug的环境变量设置过后,导致有的lib在运行依赖时不匹配吧。
===》4:fontconfig字体显示相关的问题。
=========》虽然不影响测试和运行,但结论好像是windows是不使用这个,建议排除掉,最好的方式是源码安装时排除掉。
3:运行测试demo
3.1 首先运行自带工具,简单测试
E:\opendds\vcpkg-2025.03.19\installed\x64-windows\tools\osgearth>osgearth_version.exe
osgEarth 3.7.1 build 164
主要是osgEarth_viewer.exe 运行对应的earth文件,查看效果
#vcpkg安装时有很多的earth文件,比较乱,我这里是刚好下载过源码,这个目录下都是earth,发现运行正常,可以看到各种效果。
osgEarth_viewer.exe D:\sanwei_project\repo\tests\annotation.earth
比如:osgEarth_viewer.exe D:\sanwei_project\repo\tests\annotation.earth运行如下:
3.2 直接用cmake进行官网测试demo验证。
这个demo是后验证,自己折腾在vs/qt用cmakelist依赖半天,最终还是release版本cmake中拷贝dll,以及环境变量配置完成才正常。
这里直接测试github中md文件中提供的测试。
CMakeLists.txt
这里有一个fontconfig的报错,去对应的E:/opendds/vcpkg-2025.03.19/installed/x64-windows/share/osgearth/osgEarth-targets.cmake 去掉 大约64行;Fontconfig::Fontconfig" 让使用先正常
#问题 这里我虽然配置了环境变量 但是我并没有设置E:/opendds/vcpkg-2025.03.19/scripts/buildsystems/vcpkg.cmake 他是怎么找到的?还是说缓存? 按道理需要配置 find_package才能根据cmake去找到对应模块 这次是先运行,待研究细节
cmake_minimum_required(VERSION 3.20)
project(myApp)
find_package(osgEarth CONFIG REQUIRED)
add_executable(myApp main.cpp)
target_link_libraries(myApp PRIVATE osgEarth::osgEarth)
install(TARGETS myApp RUNTIME DESTINATION bin)
main.cpp
#include <osgEarth/MapNode>
#include <osgEarth/TMS>
#include <osgEarth/EarthManipulator>
#include <osgEarth/GLUtils>
#include <osg/ArgumentParser>
#include <osgViewer/Viewer>int main(int argc, char** argv)
{osgEarth::initialize();osg::ArgumentParser args(&argc, argv);osgViewer::Viewer viewer(args);viewer.setRealizeOperation(new osgEarth::GL3RealizeOperation());viewer.setUpViewInWindow(50, 50, 800, 600); //设置一个大小,方便观察auto imagery = new osgEarth::TMSImageLayer();imagery->setURL("https://readymap.org/readymap/tiles/1.0.0/7/");auto mapNode = new osgEarth::MapNode();mapNode->getMap()->addLayer(imagery);viewer.setSceneData(mapNode);viewer.setCameraManipulator(new osgEarth::EarthManipulator(args));return viewer.run();
}
使用cmake编译后是vs项目,在vs下运行结果如下图:
3.3 真正的第一个测试,在qt上。
是我真正的第一个测试,参考网上资料,瞎折腾cmakelist,以及测试。
CMakeLists.txt
这里球体一直运行不出来,现在出来是因为设置了环境变量,但是cmake并没有对,现在看,是cmake中没有加plugins相关路径吧!,依然没加。
cmake_minimum_required(VERSION 3.20)project(qt_cmake_of_osgearth LANGUAGES CXX)set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# set(CMAKE_TOOLCHAIN_FILE "E:/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "") E:\opendds\vcpkg-2025.03.19\scripts\buildsystems
set(CMAKE_TOOLCHAIN_FILE "E:/opendds/vcpkg-2025.03.19/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
set(CMAKE_PREFIX_PATH "E:/opendds/vcpkg-2025.03.19/installed/x64-windows")# 添加包含目录
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
find_package(osgEarth CONFIG REQUIRED)
# find_package(OpenSceneGraph CONFIG REQUIRED COMPONENTS osg osgDB osgUtil osgGA osgViewer)
# find_package(osg CONFIG REQUIRED)
find_package(unofficial-osg CONFIG REQUIRED)
# find_package(unofficial-osgDB CONFIG REQUIRED)
# find_package(unofficial-osgGA CONFIG REQUIRED)
# find_package(unofficial-osgUtil CONFIG REQUIRED)
# find_package(unofficial-osgViewer CONFIG REQUIRED)
# find_package(unofficial-fontconfig CONFIG REQUIRED)add_executable(qt_cmake_of_osgearth main.cpp)# 包含头文件路径(可选,vcpkg 一般自动处理)
# include_directories(${osg_INCLUDE_DIRS} ${osgDB_INCLUDE_DIRS} ...)# 链接库
# osgEarth::osgEarth 是官方 target,osg 相关用 unofficial:: 前缀
# fontconfig 也用 unofficial::fontconfigtarget_link_libraries(qt_cmake_of_osgearth PRIVATEosgEarth::osgEarthunofficial::osg::osgunofficial::osg::osgDBunofficial::osg::osgUtilunofficial::osg::osgGAunofficial::osg::osgViewerunofficial::osg::osgTextunofficial::osg::OpenThreads
)# 设置运行时库路径
set_target_properties(qt_cmake_of_osgearth PROPERTIESVS_DEBUGGER_ENVIRONMENT "PATH=${CMAKE_PREFIX_PATH}/bin;${CMAKE_PREFIX_PATH}/debug/bin;$ENV{PATH}"
)include(GNUInstallDirs)
install(TARGETS qt_cmake_of_osgearthLIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
# project(myApp)
# find_package(osgEarth CONFIG REQUIRED)
# add_executable(myApp main.cpp)
# target_link_libraries(myApp PRIVATE osgEarth::osgEarth)
# install(TARGETS myApp RUNTIME DESTINATION bin)
# 说明 vcpkg 安装的 fontconfig 包没有导出 fontconfig 这个 target,即 TARGET fontconfig 也不存在。
if(NOT TARGET Fontconfig::Fontconfig)add_library(Fontconfig::Fontconfig INTERFACE IMPORTED)# 你可以根据 fontconfig 的实际安装路径,手动设置 include 路径和库路径set_target_properties(Fontconfig::Fontconfig PROPERTIESINTERFACE_INCLUDE_DIRECTORIES "${CMAKE_PREFIX_PATH}/include"INTERFACE_LINK_LIBRARIES "${CMAKE_PREFIX_PATH}/lib/fontconfig.lib")
endif()
main.cpp
也是参考网上,第一个开始就正常,球体环境变量配置后正常。
#include <osg/Node>
#include <osg/Group>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>int main(int argc, char** argv)
{// 创建一个组节点作为根节点osg::ref_ptr<osg::Group> root = new osg::Group();// 创建一个立方体osg::ref_ptr<osg::Box> box = new osg::Box(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f);osg::ref_ptr<osg::ShapeDrawable> boxDrawable = new osg::ShapeDrawable(box.get());// 创建一个叶节点osg::ref_ptr<osg::Geode> geode = new osg::Geode();geode->addDrawable(boxDrawable.get());// 将叶节点添加到根节点root->addChild(geode.get());// 创建查看器osgViewer::Viewer viewer;// 设置场景数据viewer.setSceneData(root.get());// 设置窗口大小viewer.setUpViewInWindow(100, 100, 800, 600);// 设置相机操作器viewer.setCameraManipulator(new osgGA::TrackballManipulator());// 运行查看器return viewer.run();
}// #include <osgEarth/MapNode>
// #include <osgEarth/TMS>
// #include <osgEarth/EarthManipulator>
// #include <osgEarth/GLUtils>
// #include <osg/ArgumentParser>
// #include <osgViewer/Viewer>// int main(int argc, char** argv)
// {
// osgEarth::initialize();// osg::ArgumentParser args(&argc, argv);
// osgViewer::Viewer viewer(args);
// viewer.setRealizeOperation(new osgEarth::GL3RealizeOperation());
// viewer.setUpViewInWindow(100, 100, 1280, 720); // 添加这行// auto imagery = new osgEarth::TMSImageLayer();
// imagery->setURL("https://readymap.org/readymap/tiles/1.0.0/7/");// auto mapNode = new osgEarth::MapNode();
// mapNode->getMap()->addLayer(imagery);// viewer.setSceneData(mapNode);
// viewer.setCameraManipulator(new osgEarth::EarthManipulator(args));// return viewer.run();
// }
3.4 在vs2022上用cmake进行折腾
核心还是想折腾折腾,去尝试错误。
CMakeLists.txt
release正常后,debug一直不正常,最后才发现是环境变量可能前面有依赖库不一样版本导致的
# CMakeList.txt: CMakeProject1 的 CMake 项目,在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.8)# Enable Hot Reload for MSVC compilers if supported.
if (POLICY CMP0141)cmake_policy(SET CMP0141 NEW)set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()project ("CMakeProject1")# 设置构建类型
if(NOT CMAKE_BUILD_TYPE)set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build." FORCE)
endif()# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)# 设置 vcpkg 工具链文件
set(CMAKE_TOOLCHAIN_FILE "E:/opendds/vcpkg-2025.03.19/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
set(CMAKE_PREFIX_PATH "E:/opendds/vcpkg-2025.03.19/installed/x64-windows")# 查找依赖包
find_package(osgEarth CONFIG REQUIRED)
find_package(unofficial-osg CONFIG REQUIRED)# 添加可执行文件
add_executable (CMakeProject1 "CMakeProject1.cpp" "CMakeProject1.h")# 添加包含目录 这里让他自己通过CMAKE_TOOLCHAIN_FILE 自己去依赖,如果加上下面的会有和vcpkg底层库冲突问题
#[[
target_include_directories(CMakeProject1 PRIVATE${CMAKE_PREFIX_PATH}/include${CMAKE_PREFIX_PATH}/include/osgEarth${CMAKE_PREFIX_PATH}/include/osgEarthDrivers${CMAKE_PREFIX_PATH}/include/osg${CMAKE_PREFIX_PATH}/include/osgDB${CMAKE_PREFIX_PATH}/include/osgUtil${CMAKE_PREFIX_PATH}/include/osgGA${CMAKE_PREFIX_PATH}/include/osgViewer${CMAKE_PREFIX_PATH}/include/osgText${CMAKE_PREFIX_PATH}/include/OpenThreads
)
]]
# 添加编译定义
target_compile_definitions(CMakeProject1 PRIVATE_CRT_SECURE_NO_WARNINGSNOMINMAXWIN32_LEAN_AND_MEAN_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
)# 添加编译选项
if(MSVC)target_compile_options(CMakeProject1 PRIVATE/W3/MP/bigobj)
endif()# 链接库
target_link_libraries(CMakeProject1 PRIVATE osgEarth::osgEarthunofficial::osg::osgunofficial::osg::osgDBunofficial::osg::osgUtilunofficial::osg::osgGAunofficial::osg::osgViewerunofficial::osg::osgTextunofficial::osg::OpenThreads
)# 设置 C++ 标准
#if (CMAKE_VERSION VERSION_GREATER 3.12)
# set_property(TARGET CMakeProject1 PROPERTY CXX_STANDARD 20)
#endif()# 复制 DLL 文件到输出目录
if(WIN32)# 获取所有依赖的 DLL 文件get_target_property(OSG_DLLS unofficial::osg::osg IMPORTED_LOCATION_DEBUG)get_target_property(OSGDB_DLLS unofficial::osg::osgDB IMPORTED_LOCATION_DEBUG)get_target_property(OSGUTIL_DLLS unofficial::osg::osgUtil IMPORTED_LOCATION_DEBUG)get_target_property(OSGGA_DLLS unofficial::osg::osgGA IMPORTED_LOCATION_DEBUG)get_target_property(OSGVIEWER_DLLS unofficial::osg::osgViewer IMPORTED_LOCATION_DEBUG)get_target_property(OSGTEXT_DLLS unofficial::osg::osgText IMPORTED_LOCATION_DEBUG)get_target_property(OPENTHREADS_DLLS osgEarth::osgEarth IMPORTED_LOCATION_DEBUG)get_target_property(OPENTHREADS_DLLS unofficial::osg::OpenThreads IMPORTED_LOCATION_DEBUG)message(STATUS "OSG DLL path: ${OSG_DLLS}")message(STATUS "OSGDB_DLLS DLL path: ${OSGDB_DLLS}")message(STATUS "OSGUTIL_DLLS DLL path: ${OSGUTIL_DLLS}")message(STATUS "OSGGA_DLLS DLL path: ${OSGGA_DLLS}")message(STATUS "OSGVIEWER_DLLS DLL path: ${OSGVIEWER_DLLS}")message(STATUS "OSGTEXT_DLLS DLL path: ${OSGTEXT_DLLS}")message(STATUS "OSGEARTH_DLLS DLL path: ${OSGEARTH_DLLS}")message(STATUS "OPENTHREADS_DLLS DLL path: ${OSGEARTH_DLLS}")set(OSGMANIPULATORD_DEBUG_DLLS "E:/opendds/vcpkg-2025.03.19/installed/x64-windows/debug/bin/osgManipulatord.dll")set(OSGMANIPULATORD_RELEASE_DLLS "E:/opendds/vcpkg-2025.03.19/installed/x64-windows/bin/osgManipulator.dll")set(OSGSHADOW_DEBUG_DLLS "E:/opendds/vcpkg-2025.03.19/installed/x64-windows/debug/bin/osgShadowd.dll")set(OSGSHADOW_ATORD_RELEASE_DLLS "E:/opendds/vcpkg-2025.03.19/installed/x64-windows/bin/osgShadow.dll")set(OSGSIM_DEBUG_DLLS "E:/opendds/vcpkg-2025.03.19/installed/x64-windows/debug/bin/osgSimd.dll")set(OSGSIM_ATORD_RELEASE_DLLS "E:/opendds/vcpkg-2025.03.19/installed/x64-windows/bin/osgSim.dll")# 设置 zlib DLL 路径 # 这里直接吧路径写在add_custom_command ZLIB_DEBUG_DLL中是有问题的 CMake 生成器表达式中的 $ 符号在 Ninja 构建系统中需要特殊处理# 这样处理 让cmake 先处理变量set(ZLIB_DEBUG_DLL "E:/opendds/vcpkg-2025.03.19/installed/x64-windows/debug/bin/zlibd1.dll")set(ZLIB_RELEASE_DLL "E:/opendds/vcpkg-2025.03.19/installed/x64-windows/bin/zlib1.dll")#E:\opendds\vcpkg-2025.03.19\installed\x64-windows\debug\binset(LIBPROTOBUF_DEBUG_DLL "E:/opendds/vcpkg-2025.03.19/installed/x64-windows/debug/bin/libprotobufd.dll")set(LIBPROTOBUF_RELEASE_DLL "E:/opendds/vcpkg-2025.03.19/packages/protobuf_x64-windows/bin/libprotobuf.dll")set(GDALD_DEBUG_DLL "E:/opendds/vcpkg-2025.03.19/packages/gdal_x64-windows/debug/bin/gdald.dll")set(GDALD_RELEASE_DLL "E:/opendds/vcpkg-2025.03.19/packages/gdal_x64-windows/bin/gdal.dll")set(LIBCURL_DEBUG_DLL "E:/opendds/vcpkg-2025.03.19/packages/curl_x64-windows/debug/bin/libcurl-d.dll")set(LIBCURL_RELEASE_DLL "E:/opendds/vcpkg-2025.03.19/packages/curl_x64-windows/bin/libcurl.dll")set(QHULL_DEBUG_DLL "E:/opendds/vcpkg-2025.03.19/packages/qhull_x64-windows/debug/bin/qhull_rd.dll")set(QHULL_RELEASE_DLL "E:/opendds/vcpkg-2025.03.19/packages/qhull_x64-windows/bin/qhull_r.dll")set(TIFF_DEBUG_DLL "E:/opendds/vcpkg-2025.03.19/packages/tiff_x64-windows/debug/bin/tiffd.dll")set(TIFF_RELEASE_DLL "E:/opendds/vcpkg-2025.03.19/packages/tiff_x64-windows/bin/tiff.dll")set(GEOTIFF_DEBUG_DLL "E:/opendds/vcpkg-2025.03.19/packages/libgeotiff_x64-windows/debug/bin/geotiff_d.dll")set(GEOTIFF_RELEASE_DLL "E:/opendds/vcpkg-2025.03.19/packages/libgeotiff_x64-windows/bin/geotiff.dll")set(PNG16_DEBUG_DLL "E:/opendds/vcpkg-2025.03.19/packages/libpng_x64-windows/debug/bin/libpng16d.dll")set(PNG16_RELEASE_DLL "E:/opendds/vcpkg-2025.03.19/packages/libpng_x64-windows/bin/libpng16.dll")set(PCRE2_DEBUG_DLL "E:/opendds/vcpkg-2025.03.19/packages/pcre2_x64-windows/debug/bin/pcre2-8d.dll")set(PCRE2_RELEASE_DLL "E:/opendds/vcpkg-2025.03.19/packages/pcre2_x64-windows/bin/pcre2-8.dll")set(LIBEXPATD_DEBUG_DLL "E:/opendds/vcpkg-2025.03.19/packages/expat_x64-windows/debug/bin/libexpatd.dll")set(LIBEXPATD_RELEASE_DLL "E:/opendds/vcpkg-2025.03.19/packages/expat_x64-windows/bin/libexpat.dll")set(PROJ_9_DEBUG_DLL "E:/opendds/vcpkg-2025.03.19/packages/proj_x64-windows/debug/bin/proj_9_d.dll")set(PROJ_9_RELEASE_DLL "E:/opendds/vcpkg-2025.03.19/packages/proj_x64-windows/bin/proj_9.dll")set(HDF5_DEBUG_DLL "E:/opendds/vcpkg-2025.03.19/packages/hdf5_x64-windows/debug/bin/hdf5_D.dll")set(HDF5_RELEASE_DLL "E:/opendds/vcpkg-2025.03.19/packages/hdf5_x64-windows/bin/hdf5.dll")# Debug 模式下的 DLL 复制add_custom_command(TARGET CMakeProject1 POST_BUILDCOMMAND ${CMAKE_COMMAND} -E copy_if_different$<IF:$<CONFIG:Debug>,${OSG_DLLS},$<TARGET_FILE:unofficial::osg::osg>>$<IF:$<CONFIG:Debug>,${OSGDB_DLLS},$<TARGET_FILE:unofficial::osg::osgDB>>$<IF:$<CONFIG:Debug>,${OSGUTIL_DLLS},$<TARGET_FILE:unofficial::osg::osgUtil>>$<IF:$<CONFIG:Debug>,${OSGGA_DLLS},$<TARGET_FILE:unofficial::osg::osgGA>>$<IF:$<CONFIG:Debug>,${OSGVIEWER_DLLS},$<TARGET_FILE:unofficial::osg::osgViewer>>$<IF:$<CONFIG:Debug>,${OSGTEXT_DLLS},$<TARGET_FILE:unofficial::osg::osgText>>$<IF:$<CONFIG:Debug>,${OSGEARTH_DLLS},$<TARGET_FILE:osgEarth::osgEarth>>$<IF:$<CONFIG:Debug>,${OPENTHREADS_DLLS},$<TARGET_FILE:unofficial::osg::OpenThreads>>$<IF:$<CONFIG:Debug>,${OSGMANIPULATORD_DEBUG_DLLS},${OSGMANIPULATORD_RELEASE_DLLS}>$<IF:$<CONFIG:Debug>,${OSGSHADOW_DEBUG_DLLS},${OSGSHADOW_ATORD_RELEASE_DLLS}>$<IF:$<CONFIG:Debug>,${OSGSIM_DEBUG_DLLS},${OSGSIM_ATORD_RELEASE_DLLS}>$<IF:$<CONFIG:Debug>,${ZLIB_DEBUG_DLL},${ZLIB_RELEASE_DLL}>$<IF:$<CONFIG:Debug>,${LIBPROTOBUF_DEBUG_DLL},${LIBPROTOBUF_RELEASE_DLL}>$<IF:$<CONFIG:Debug>,${GDALD_DEBUG_DLL},${GDALD_RELEASE_DLL}>$<IF:$<CONFIG:Debug>,${LIBCURL_DEBUG_DLL},${LIBCURL_RELEASE_DLL}>$<IF:$<CONFIG:Debug>,${QHULL_DEBUG_DLL},${QHULL_RELEASE_DLL}>$<IF:$<CONFIG:Debug>,${TIFF_DEBUG_DLL},${TIFF_RELEASE_DLL}>$<IF:$<CONFIG:Debug>,${GEOTIFF_DEBUG_DLL},${GEOTIFF_RELEASE_DLL}>$<IF:$<CONFIG:Debug>,${PNG16_DEBUG_DLL},${PNG16_RELEASE_DLL}>$<IF:$<CONFIG:Debug>,${PCRE2_DEBUG_DLL},${PCRE2_RELEASE_DLL}>$<IF:$<CONFIG:Debug>,${LIBEXPATD_DEBUG_DLL},${LIBEXPATD_RELEASE_DLL}>$<IF:$<CONFIG:Debug>,${PROJ_9_DEBUG_DLL},${PROJ_9_RELEASE_DLL}>$<IF:$<CONFIG:Debug>,${HDF5_DEBUG_DLL},${HDF5_RELEASE_DLL}>$<TARGET_FILE_DIR:CMakeProject1>)
endif()# 设置输出目录
set_target_properties(CMakeProject1 PROPERTIESRUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/Debug"RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/Release"LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/Debug"LIBRARY_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/Release"ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/Debug"ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/Release"
)# TODO: 如有需要,请添加测试并安装目标。
CMakeProject1.cpp
#include <osgViewer/Viewer>
#include <osgEarth/MapNode>
#include <osgEarth/EarthManipulator>
#include <osgDB/ReadFile>
#include <osgText/Text>
#include <osg/Geode>
#include <osg/PositionAttitudeTransform>osg::ref_ptr<osg::Node> createText()
{osg::ref_ptr<osgText::Text> text = new osgText::Text();text->setFont("C:/Windows/Fonts/arial.ttf"); // 避免用 CascadiaCodetext->setCharacterSize(40.0f);text->setText("Hello osgEarth!");text->setPosition(osg::Vec3(0, 0, 1000000.0)); // 地球表面上方osg::ref_ptr<osg::Geode> geode = new osg::Geode();geode->addDrawable(text);osg::ref_ptr<osg::PositionAttitudeTransform> pat = new osg::PositionAttitudeTransform();pat->addChild(geode);return pat;
}int main(int argc, char** argv)
{std::cout << "OSG_LIBRARY_PATH: " << getenv("path") << std::endl;osgViewer::Viewer viewer;viewer.setUpViewInWindow(50, 50, 800, 600);osg::ref_ptr<osg::Node> earthNode = osgDB::readNodeFile("D:/VS_project/CMakeProject1/minimal.earth");if (!earthNode){std::cerr << "Failed to load earth file!" << std::endl;return 1;}osg::Group* root = new osg::Group();root->addChild(earthNode.get());root->addChild(createText().get());viewer.setSceneData(root);viewer.setCameraManipulator(new osgEarth::Util::EarthManipulator());return viewer.run();
}
先不关注内部使用报错,至少结果有了:
4:简单思考整理一下cmake
在做完上面的练习后,刚好同事在做一个boost库的cmake依赖遇到问题,就问了问chatgpt,简单整理一下。
CMakeLists.txt
这里主要想记录cmake的两种方式1:使用cmake,通过find_package查找, 2:直接设置路径进行链接
cmake_minimum_required(VERSION 3.15)
project(MyBoostApp)set(CMAKE_CXX_STANDARD 17)# 可选:显式设置 Boost 路径
set(BOOST_ROOT "D:/software/boost_1_78_0")# 如果用静态库,打开这一行
# set(Boost_USE_STATIC_LIBS ON)
set(Boost_NO_BOOST_CMAKE ON) #必须这一行 find_package是从cmake找,只有确定这行才能使下面find_package正确。
# 查找 Boost
find_package(Boost REQUIRED COMPONENTS filesystem system)if (Boost_FOUND)add_executable(MyBoostApp main.cpp)target_include_directories(MyBoostApp PRIVATE ${Boost_INCLUDE_DIRS})target_link_libraries(MyBoostApp PRIVATE ${Boost_LIBRARIES})
else()message(FATAL_ERROR "Boost not found")
endif()#还有一种方式 直接设置头文件和路径
#include_directories("D:/software/boost_1_78_0")
#link_directories("D:/software/boost_1_78_0/stage/lib")#add_executable(MyBoostDemo main.cpp)
#target_link_libraries(MyBoostDemo boost_filesystem boost_system)
main.cpp
#include <iostream>
#include <boost/filesystem.hpp>int main() {boost::filesystem::path current_dir = boost::filesystem::current_path();std::cout << "Current path is: " << current_dir << std::endl;for (auto& entry : boost::filesystem::directory_iterator(current_dir)) {std::cout << entry.path().string() << std::endl;}return 0;
}