如何在Qt Creator中构建osgEarth应用并运行基础示例代码?
Hey there! Let's walk through how to build and run an osgEarth application in Qt Creator step by step. I've tackled this setup multiple times, so I'll share the exact, actionable steps that should get your sample code up and running smoothly.
一、先搞定环境准备
Before diving into Qt Creator, make sure you have these pieces in place:
- Qt Creator & Qt SDK: Grab a version that uses a compiler matching your osgEarth build (e.g., MSVC 2019 for Windows, GCC for Linux). Stick to 32-bit or 64-bit consistently—mixing architectures will cause avoidable headaches.
- osgEarth & Dependencies: You need a pre-compiled osgEarth package (or compile it yourself) that includes all required dependencies: OSG, GDAL, PROJ, etc. Ensure the build mode (Debug/Release) matches what you'll use in Qt Creator.
二、创建Qt项目并配置构建环境
- Create a New Project: Open Qt Creator, select File > New Project > Non-Qt Project > Plain C++ Application (Qt Console Application works too, since we're using osgViewer). Name your project and pick a save location.
- Modify the .pro File: This is the key part to link osgEarth and OSG libraries. Open your project's
.profile and add these configurations (replace the paths with your actual osgEarth/OSG installation directories):
# Add include paths for osgEarth and OSG headers INCLUDEPATH += \ /path/to/osgEarth/include \ /path/to/osg/include \ /path/to/gdal/include # Include this if your osgEarth uses GDAL # Add library paths LIBS += -L"/path/to/osgEarth/lib" \ -L"/path/to/osg/lib" \ -L"/path/to/gdal/lib" # Link core osgEarth and OSG libraries (add a 'd' suffix for Debug builds, e.g., losgEarthd) LIBS += \ -losgEarth \ -losgEarthUtil \ -losgEarthDrivers \ -losgViewer \ -losg \ -losgDB \ -losgGA \ -losgUtil \ -lgdal # Include if required by your osgEarth build
- Verify Build Configuration: Go to Projects > Build & Run and ensure the build mode (Debug/Release) matches the osgEarth libraries you're linking. For Windows, double-check the compiler matches too (e.g., MSVC 2019 x64).
三、调整示例代码并编译
Your sample code is a great start, but we need to add a full main function to make it runnable. Replace your code with this complete version:
#include <osgViewer/Viewer> #include <osgEarth/MapNode> #include <osgEarth/ImageLayer> #include <osgEarth/ElevationLayer> #include <osgEarth/ModelLayer> #include <osgEarth/GeoTransform> #include <osgEarthUtil/EarthManipulator> #include <osgEarthUtil/ExampleResources> #include <osgEarthUtil/AutoScaleCallback> #include <osgEarthDrivers/tms/TMSOptions> #include <osgEarthDrivers/wms/WMSOptions> int main(int argc, char** argv) { // Initialize osg argument parser osg::ArgumentParser arguments(&argc, argv); osgViewer::Viewer viewer(arguments); // Create a basic map instance osgEarth::Map* map = new osgEarth::Map(); // Add an OpenStreetMap TMS layer (swap this with your local layer if needed) osgEarth::TMSOptions tmsOpts; tmsOpts.url() = "http://tile.openstreetmap.org/{z}/{x}/{y}.png"; osgEarth::ImageLayer* osmLayer = new osgEarth::ImageLayer("OpenStreetMap", tmsOpts); map->addLayer(osmLayer); // Wrap the map in a MapNode (the root of your osgEarth scene) osgEarth::MapNode* mapNode = new osgEarth::MapNode(map); // Use osgEarth's EarthManipulator for intuitive camera controls osgEarth::Util::EarthManipulator* earthManip = new osgEarth::Util::EarthManipulator(); viewer.setCameraManipulator(earthManip); // Set the scene root and run the viewer viewer.setSceneData(mapNode); return viewer.run(); }
Now click the Build button (hammer icon) in Qt Creator. If all paths are correct, the project should compile without errors.
四、Run the Application
A quick gotcha here: you need to make sure the osgEarth/OSG dynamic libraries are accessible to your executable:
- Windows: Copy all required
.dllfiles from your osgEarth/OSGbindirectories into the same folder as your project's executable (e.g.,build-YourProject-Desktop_Qt_5_15_2_MSVC2019_64bit-Release/release). - Linux: Add the osgEarth/OSG
libdirectories to yourLD_LIBRARY_PATHenvironment variable, or copy the.sofiles to/usr/local/lib.
Once that's done, click the Run button (play icon). You should see a window pop up with OpenStreetMap tiles loaded, and you can navigate using your mouse (left-click drag to rotate, right-click drag to zoom).
内容的提问来源于stack exchange,提问作者mary.ixarowa

