网上编译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 " (?metaObject@BaseAnnotationDialog@QtGui@osgEarth@@UBEPBUQMetaObject@@XZ) AnnotationDialogs.obj osgEarthQt
解决办法:
主要原因:没能生成moc_xxx.cpp文件。
修改vs配置,右击osgearthQt头文件-----》属性,要对有Q_OBJECT头文件的类都要进行如下配置。
修改项类型“不参与生成”为“自定义生成工具”
配置“自动定义生成工具”,主要配置“命令行”、“输出”
命令行:
"$(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,再次运行程序没有问题。
运行结果如下: