網上編譯osgearth的過程很多,這裡就不再做記錄了,隻把編譯過程中遇到的問題記錄下來,以便以後有人再次遇到同樣的問題,不用再走彎路。
一、編譯環境
Vs2013、Qt5.7、osg3.2、osgearth2.7
二、cmake生成vs時的警告
CMake Warning (dev) in src/osgEarthQt/CMakeLists.txt:
Policy CMP0043 is not set: Ignore COMPILE_DEFINITIONS_<Config> properties.
Run "cmake --help-policy CMP0043" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.
This warning is for project developers. Use -Wno-dev to suppress it.
CMake Warning (dev) in src/osgEarthQt/CMakeLists.txt:
Policy CMP0043 is not set: Ignore COMPILE_DEFINITIONS_<Config> properties.
Run "cmake --help-policy CMP0043" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.
This warning is for project developers. Use -Wno-dev to suppress it.
CMake Warning (dev) in src/osgEarthQt/CMakeLists.txt:
Policy CMP0043 is not set: Ignore COMPILE_DEFINITIONS_<Config> properties.
Run "cmake --help-policy CMP0043" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.
This warning is for project developers. Use -Wno-dev to suppress it.
解決辦法:
在osgEarthQt/CMakeList.txt檔案中添加如下代碼即可消除警告
if(COMMAND cmake_policy)
# Works around warnings libraries linked against that don't
# have absolute paths (e.g. -lpthreads)
cmake_policy(SET CMP0003 NEW)
# Works around warnings about escaped quotes in ADD_DEFINITIONS
# statements.
cmake_policy(SET CMP0005 OLD)
IF(COMMAND cmake_policy)
IF(${CMAKE_MAJOR_VERSION} GREATER 2)
# Qt5 qt5_use_modules usage was causing "Policy CMP0043 is not set: Ignore COMPILE_DEFINITIONS_<Config> properties." warnings
cmake_policy(SET CMP0043 NEW)
ENDIF()
ENDIF()
# disable autolinking to qtmain as we have our own main() functions (new in Qt 5.1)
if(NOT "${CMAKE_VERSION}" VERSION_LESS 3.6.0)
cmake_policy(SET CMP0020 OLD)
endif(NOT "${CMAKE_VERSION}" VERSION_LESS 3.6.0)
endif(COMMAND cmake_policy)
三、編譯osgearthQt庫時在moc_XXX.cpp中缺少osgEarth、QtGui命名空間
主要原因在于cmake生成Qt時自動生成了moc_xxx.cpp檔案,但是沒有将工程的.h頭檔案添加進去,導緻程式在編譯時無法找到命名空間以及相應的成員方法。
解決辦法
方法1、 在moc_xxx.cpp中添加osgearthQt庫工程的對應頭檔案,這個辦法比較笨,但是友善。
方法2、 在進行cmake生成vs工程之前注釋掉osgearthQt工程CMakeLists.txt檔案如下幾行,不讓在cmake時生成moc_xxx.cpp檔案,因為moc_xxx.cpp檔案會在編譯工程時自動生成。
# Header files that need moc'd
#set(LIB_MOC_HDRS
# AnnotationDialogs
# AnnotationListWidget
# AnnotationToolbar
# CollapsiblePairWidget
# DataManager
# LayerManagerWidget
# LOSControlWidget
# LOSCreationDialog
# MapCatalogWidget
# TerrainProfileGraph
# TerrainProfileWidget
# ViewerWidget
#)
其次在注釋掉如上代碼後再進行cmake生成vs工程,再次編譯osgearthQt工程還是不能通過。
錯誤如下:
錯誤 1 error LNK2001: 無法解析的外部符号 "public: virtual struct QMetaObject const * __thiscall osgEarth::QtGui::BaseAnnotationDialog::metaObject(void)const " ([email protected]@[email protected]@@[email protected]@XZ) AnnotationDialogs.obj osgEarthQt
解決辦法:
主要原因:沒能生成moc_xxx.cpp檔案。
修改vs配置,右擊osgearthQt頭檔案-----》屬性,要對有Q_OBJECT頭檔案的類都要進行如下配置。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TO5MDNyYTNxIjMyATM2EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
修改項類型“不參與生成”為“自定義生成工具”
配置“自動定義生成工具”,主要配置“指令行”、“輸出”
指令行:
"$(QTDIR)\moc.exe" "%(FullPath)" -o "$(ConfigurationName)\moc_%(Filename).cpp" -D_WINDOWS -DUNICODE -DWIN32 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DNOMINMAX -D_UNICODE "-I$(QTDIR)\include" "-I$(QTDIR)\include\QtWidgets" "-I$(QTDIR)\include\QtGui" "-I$(QTDIR)\include\QtCore" "-I.\debug" "-I." "-I$(QTDIR)\." "-I$(QTDIR)\include\QtOpenGL"
輸出:
Debug\moc_ViewWidget.cpp;%(Outputs)
這裡注意:首先要配置環境變量:QTDIR
四、運作osgearth_qtd.exe時報錯如下:
Error: glGenBuffers not supported by OpenGL driver
Error: glBindBuffer not supported by OpenGL driver
Error: glBufferData not supported by OpenGL driver
Error: glBufferData not supported by OpenGL driver
Error: glBufferData not supported by OpenGL driver
Error: glBufferData not supported by OpenGL driver
Error: glBufferData not supported by OpenGL driver
Error: glBufferData not supported by OpenGL driver
Error: glBufferData not supported by OpenGL driver
Error: glGenBuffers not supported by OpenGL driver
Error: glBindBuffer not supported by OpenGL driver
Error: glBufferData not supported by OpenGL driver
Error: glBufferData not supported by OpenGL driver
Error: glBindBuffer not supported by OpenGL driver
Error: glBindBuffer not supported by OpenGL driver
主要原因:電腦顯示卡驅動的opengl版本太低。Intel(R) HD Graphics 300 內建顯示卡的opengl版本比較低,導緻如上錯誤。禁止內建顯示卡,電腦會自動切換到獨立顯示卡NVIDIA GreForce GT 520M,再次運作程式沒有問題。
運作結果如下: