前言:使用VS编译时因为第三方库的问题导致前功尽弃,最后还是绕回到vcpkg安装所有库,这里是安装过程记录。
1.安装第三方依赖
其实在安装osg时,第三方依赖会自动下载,但避免再次遇到第三方库问题,建议使用vcpkg install curl后再vcpkg install curl[core,openssl]一下确保一下curl支持openssl。
2.安装osg
vcpkg支持的最新osg版本是3.6.5,在使用vcpkg install osg前,确保开启GL3编译:
[vcpkg安装目录]vcpkg\triplets\x64-windows.cmake中添加 set(osg_OPENGL_PROFILE "GL3")
或[vcpkg安装目录]vcpkg\ports\osg\portfile.cmake 中
if(NOT DEFINED osg_OPENGL_PROFILEGL3)
set(osg_OPENGL_PROFILEGL3 "GL2") //这里的GL2改为GL3
endif()ps:理论上改一种就行,我为了保险两个地方都改了
然后运行vcpkg install osg,如果运行到 -- Configuring x64-windows这一步报错,不要慌,检查错误文档[vcpkg安装目录]vcpkg\buildtrees\osg\install-x64-windows-dbg-out.log
-- Configuring x64-windows
CMake Warning at installed/x64-windows/share/vcpkg-cmake/vcpkg_cmake_configure.cmake:318 (message):
The following variables are not used in CMakeLists.txt:
CMAKE_DISABLE_FIND_PACKAGE_Boost
CMAKE_REQUIRE_FIND_PACKAGE_Boost
Please recheck them and remove the unnecessary options from the
`vcpkg_cmake_configure` call.
If these options should still be passed for whatever reason, please use the
`MAYBE_UNUSED_VARIABLES` argument.
Call Stack (most recent call first):
ports/osg/portfile.cmake:92 (vcpkg_cmake_configure)
scripts/ports.cmake:175 (include)
-- Building x64-windows-dbg
CMake Error at scripts/cmake/vcpkg_execute_build_process.cmake:134 (message):
Command failed: "C:/Program Files/CMake/bin/cmake.exe" --build . --config Debug --target install -- -v -j9
Working Directory: E:/vcpkg/buildtrees/osg/x64-windows-dbg
See logs for more information:
E:\vcpkg\buildtrees\osg\install-x64-windows-dbg-out.log
Call Stack (most recent call first):
installed/x64-windows/share/vcpkg-cmake/vcpkg_cmake_build.cmake:74 (vcpkg_execute_build_process)
installed/x64-windows/share/vcpkg-cmake/vcpkg_cmake_install.cmake:16 (vcpkg_cmake_build)
ports/osg/portfile.cmake:133 (vcpkg_cmake_install)
scripts/ports.cmake:175 (include)
这里摘要部分:
Run Build Command(s): E:/VisualStudio2022/Common7/IDE/CommonExtensions/Microsoft/CMake/Ninja/ninja.exe -v -v -j9 install
[1/1670] C:/PROGRA~1/CMake/bin/cmcldeps.exe RC E:\vcpkg\buildtrees\osg\x64-windows-dbg\PlatformSpecifics\Windows\OpenThreadsVersionInfo.rc src\OpenThreads\win32\CMakeFiles\OpenThreads.dir\__\__\__\PlatformSpecifics\Windows\OpenThreadsVersionInfo.rc.res.d src\OpenThreads\win32\CMakeFiles\OpenThreads.dir\__\__\__\PlatformSpecifics\Windows\OpenThreadsVersionInfo.rc.res "Note: including file: " "E:/VisualStudio2022/VC/Tools/MSVC/14.39.33519/bin/Hostx64/x64/cl.exe" C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\rc.exe -DOPENTHREADS_EXPORTS -DOpenThreads_EXPORTS -DWIN32_LEAN_AND_MEAN -D_CRT_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_WIN32_WINNT=0x0400 -I E:\vcpkg\buildtrees\osg\x64-windows-dbg\include -I E:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\include -c65001 /DWIN32 -D_DEBUG /fo src\OpenThreads\win32\CMakeFiles\OpenThreads.dir\__\__\__\PlatformSpecifics\Windows\OpenThreadsVersionInfo.rc.res E:\vcpkg\buildtrees\osg\x64-windows-dbg\PlatformSpecifics\Windows\OpenThreadsVersionInfo.rc
[2/1670] E:\VisualStudio2022\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x64\cl.exe /TP -DOPENTHREADS_EXPORTS -DOpenThreads_EXPORTS -DWIN32_LEAN_AND_MEAN -D_CRT_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_WIN32_WINNT=0x0400 -IE:\vcpkg\buildtrees\osg\x64-windows-dbg\include -IE:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\include /nologo /DWIN32 /D_WINDOWS /W3 /utf-8 /GR /EHsc /MP /W4 /wd4589 /wd4706 /wd4127 /wd4100 /D_DEBUG /MDd /Z7 /Ob0 /Od /RTC1 /showIncludes /Fosrc\OpenThreads\win32\CMakeFiles\OpenThreads.dir\Win32Mutex.cpp.obj /Fdsrc\OpenThreads\win32\CMakeFiles\OpenThreads.dir\ /FS -c E:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\src\OpenThreads\win32\Win32Mutex.cpp
cl : Command line warning D9025 : overriding '/W3' with '/W4'
[3/1670] E:\VisualStudio2022\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x64\cl.exe /TP -DOPENTHREADS_EXPORTS -DOpenThreads_EXPORTS -DWIN32_LEAN_AND_MEAN -D_CRT_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_WIN32_WINNT=0x0400 -IE:\vcpkg\buildtrees\osg\x64-windows-dbg\include -IE:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\include /nologo /DWIN32 /D_WINDOWS /W3 /utf-8 /GR /EHsc /MP /W4 /wd4589 /wd4706 /wd4127 /wd4100 /D_DEBUG /MDd /Z7 /Ob0 /Od /RTC1 /showIncludes /Fosrc\OpenThreads\win32\CMakeFiles\OpenThreads.dir\Win32ThreadBarrier.cpp.obj /Fdsrc\OpenThreads\win32\CMakeFiles\OpenThreads.dir\ /FS -c E:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\src\OpenThreads\win32\Win32ThreadBarrier.cpp
cl : Command line warning D9025 : overriding '/W3' with '/W4'
[87/1670] E:\VisualStudio2022\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x64\cl.exe /TP -DOSG_LIBRARY -D_CRT_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -Dosg_EXPORTS -IE:\vcpkg\buildtrees\osg\x64-windows-dbg\include -IE:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\include /nologo /DWIN32 /D_WINDOWS /W3 /utf-8 /GR /EHsc /MP /W4 /wd4589 /wd4706 /wd4127 /wd4100 /D_DEBUG /MDd /Z7 /Ob0 /Od /RTC1 /showIncludes /Fosrc\osg\CMakeFiles\osg.dir\Matrixd.cpp.obj /Fdsrc\osg\CMakeFiles\osg.dir\ /FS -c E:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\src\osg\Matrixd.cpp
FAILED: src/osg/CMakeFiles/osg.dir/Matrixd.cpp.obj
E:\VisualStudio2022\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x64\cl.exe /TP -DOSG_LIBRARY -D_CRT_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -Dosg_EXPORTS -IE:\vcpkg\buildtrees\osg\x64-windows-dbg\include -IE:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\include /nologo /DWIN32 /D_WINDOWS /W3 /utf-8 /GR /EHsc /MP /W4 /wd4589 /wd4706 /wd4127 /wd4100 /D_DEBUG /MDd /Z7 /Ob0 /Od /RTC1 /showIncludes /Fosrc\osg\CMakeFiles\osg.dir\Matrixd.cpp.obj /Fdsrc\osg\CMakeFiles\osg.dir\ /FS -c E:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\src\osg\Matrixd.cpp
cl : Command line warning D9025 : overriding '/W3' with '/W4'
E:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\src\osg\Matrix_implementation.cpp(70): warning C4003: not enough arguments for function-like macro invocation 'min'
E:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\src\osg\Matrix_implementation.cpp(70): error C2589: '(': illegal token on right side of '::'
E:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\src\osg\Matrix_implementation.cpp(70): error C2062: type 'unknown-type' unexpected
E:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\src\osg\Matrix_implementation.cpp(70): error C2059: syntax error: ')'
E:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\src\osg\Matrix_implementation.cpp(71): error C2143: syntax error: missing ';' before '{'
E:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\src\osg\Matrix_implementation.cpp(76): error C2181: illegal else without matching if
[88/1670] E:\VisualStudio2022\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x64\cl.exe /TP -DOSG_LIBRARY -D_CRT_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -Dosg_EXPORTS -IE:\vcpkg\buildtrees\osg\x64-windows-dbg\include -IE:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\include /nologo /DWIN32 /D_WINDOWS /W3 /utf-8 /GR /EHsc /MP /W4 /wd4589 /wd4706 /wd4127 /wd4100 /D_DEBUG /MDd /Z7 /Ob0 /Od /RTC1 /showIncludes /Fosrc\osg\CMakeFiles\osg.dir\LogicOp.cpp.obj /Fdsrc\osg\CMakeFiles\osg.dir\ /FS -c E:\vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean\src\osg\LogicOp.cpp
cl : Command line warning D9025 : overriding '/W3' with '/W4'
出现这个问题是函数模板max/min与Visual C++中的全局的宏max/min冲突。
还是[vcpkg安装目录]vcpkg\ports\osg\portfile.cmake 这个文件找到vcpkg_cmake_configure(
vcpkg_cmake_configure(
SOURCE_PATH "${SOURCE_PATH}"
OPTIONS
${FEATURE_OPTIONS}
-DNOMINMAX=ON //添加这一行
-DDYNAMIC_OPENSCENEGRAPH=${OSG_DYNAMIC}
然后再次运行vcpkg install osg,快速进入[vcpkg安装目录]vcpkg\buildtrees\osg\src\raph-3.6.5-c3f8c61ada.clean,raph文件名可能不一样,不用管,打开CMakeLists.txt,在SET(OPENSCENEGRAPH_SOVERSION 161)后一行添加
add_definitions(-DNOMINMAX),之所以要运行命令后在改,是因为每次命令运行前会清除该文件。
解决问题的应该是给CMakeLists.txt文件添加的一行add_definitions(-DNOMINMAX),修改portfile.cmake是为了拖时间(),在vcpkg执行构建前把CMakeLists.txt改了。
根据安装的Visual MSVC版本不同可能不会遇到这个问题,我使用的是143有这个问题,等待一段时间,编译完成。
更新:vcpkg的pull requests#38780已经解决了NOMINMAX宏的问题!感谢开发人员!
3.安装osgearh和osgQt
接下来使用vcpkg install osgearth和vcpkg install osg-qt 安装这两个库,安装过程中我没遇到什么问题。顺便说一下vcpkg支持osgearth3.4,想使用osgearth3.5版本需要用vs从源码编译,也可以参照我的这篇文章(osg要使用本文章的方式安装GL3版本):使用cmake和vcpkg构建最新osgearth3.5_osgearth3.5demo-CSDN博客
4.使用库
安装完成后能在两个地方看到相关库文件:
[vcpkg安装目录]vcpkg\installed\x64-windows
或[vcpkg安装目录]vcpkg\packages\库名
想要一次性导入的可以使用installed\目录,想方便管理建议使用packages目录。