OSG官网——Windows --Compiling with Visual Studio翻译

本文详细介绍了如何在Windows环境下使用Visual Studio .NET 2005及更高版本配合CMake编译OpenSceneGraph源代码。首先,介绍了初始安装步骤,包括源代码和依赖的下载及目录结构。接着,通过CMake生成项目和解决方案文件,然后在Visual Studio中进行构建。文章还提供了关于环境变量设置、无扩展头文件的语法高亮和开始新项目的方法,以及插件、项目管理和环境变量配置的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Follows are details on usage of OpenSceneGraph from source packages. These are useful if you want to always stay up to date with the newest OSG sources from the github repository, or to use an OSG release that has no binaries. If you don't want this, you can probably use precompiled binaries from the Downloads page

以下是一些OSG源代码包的一些使用细节。这些都很有用,如果你一直保持跟随github repository,的最新的源代码,或者使用没有二进制包的OSG 发行版。如果你不想这样,可以直接使用预先编译好的二进制版本。

Subpages:
Help for compiling specific optional plugins
Instructions when using older Visual Studio versions (6.0)

Compiling with Visual Studio .NET用VS.NET 编译

(Note: These instructions are valid for Visual Studio .NET 2005 (8.0) and Visual Studio Express 2005 (see below), but they should also apply to Visual Studio .NET 2003 (7.0) or to Visual Studio .NET 2008 (9.0).)

注意:这些指示对VS.NET 2005 (8.0)是有效的,VS2003,VS2008 也是一样的

Initial setup初始安装

The project files are set up so that if you have the dependencies in the same base directory as the OpenSceneGraph sources, then it will find them. Thus, the suggested directory structure is:

这个项目文件已经建立好了,如果你具有和OSG一样的目录中有依赖,这些目录应该是这样的:

OpenSceneGraph-VERSION (replace VERSION by 2.8 or SVN or whatever) 
OpenSceneGraph-VERSION\3rdParty (put the contents of the 3rdParty zip file in here) 第三方库的zip文件放在这里
OpenSceneGraph-VERSION\OpenSceneGraph

In the OpenSceneGraph directory, see the Downloads page and download the sources for the version you're interested in, or check out the sources from the github repository,

OpenSceneGraph 目录中,请看the Downloads page并下载源代码,或者从github repository检出源代码。

In the 3rdParty directory, see the Dependencies page and download the dependencies for your platform. Place them so that the bin, lib etc. directories are directly under OpenSceneGraph-VERSION\3rdParty .

在第三方库目录下,看the Dependencies page依赖页面)然后下载这些依赖把他们放置起来。放到OpenSceneGraph-VERSION\3rdParty目录下。

Generating Project & Solution files with CMake  CMake使用的一般项目&解决方案文件

Once the sources and dependencies are in place you need to generate the Visual Studio solution and project files. This is done with CMake. Download it at Download | CMake.

一旦源代码和依赖都到位了,就可以创建VS解决方案和项目文件了,这是由CMake来完成的。请你去下载它。

Start the CMake GUI once it's installed, and select root OpenSceneGraph directory in the "Where is the source code" field. The same directory needs to be put into the "Where to build the binaries" field. If you like to do out-of-source builds, you can add \build to the end. Then click Configure.

安装完就开启CMakeGUI,并选择根OSG目录在"Where is the source code"域中。同样的目录需要被放入"Where to build the binaries"域中。如果你喜欢做out-of-source builds,你可以添加\build 到末端,然后点击Configure

You can then customize your build. The red lines in the CMake window indicate new variables that you may want to fill out or change, but most are optional. Some variables will be filled in automatically (such as ACTUAL_3RDPARTY_DIR, if you followed the directory structure above). I typically enable BUILD_OSG_EXAMPLES and set CMAKE_INSTALL_PREFIX to the OpenSceneGraph directory so that the binaries are installed in OpenSceneGraph\bin. You can also set it to somewhere else if you want to keep your source tree clean.

你可以定制化你的build。在CMake窗口中的红色的线表明新变量你需要填充或者改变,但是大部分都是可选的。一些变量会自动填充(例如:ACTUAL_3RDPARTY_DIR,如果你遵循了以上的目录结构),我典型地会使得BUILD_OSG_EXAMPLES可以使用和设置CMAKE_INSTALL_PREFIXOpenSceneGraph 目录,这样二进制包就会被安装在OpenSceneGraph\bin中。你也可以设定它到其他地方去,如果你想保持你的源代码树的干净。

Note: In the permission-restricted environment of Windows Vista and later, do not set CMAKE_INSTALL_PREFIX to be a restricted folder like "Program Files". The make process will not be able to elevate permissions sufficiently to write to the destination, and will fail (typically while installing the first component, OpenThreads, with an error like "file INSTALL cannot copy file OpenThreads.dll").

注意:在严格显示权限的WindowsVista和以后版本的环境中,不要设定CMAKE_INSTALL_PREFIX目录为受限的目录,比如说 "Program Files"Make进程不会提升权限足够到写的权限到目标目录,然后就失败了(典型的就是在安装第一个组件的时候,OpenThreads 会出现一个错误,例如"file INSTALL cannot copy file OpenThreads.dll"

Check if the dependencies you have are detected - if not fill in the variables manually. If you need to fill in some variables manually, you may need to show "Advanced" variables (with the drop-down at the top of the window) and make sure all relevant variables are set correctly for a given dependency. Any plugins or examples for which you don't have the dependencies will just not be part of the generated project files, which is cleaner than it was before (the projects would be there but just refuse to build, which resulted in lots of noise when building for things you knew would not build anyway).

请检查是否依赖已经被检测到了-如果没有手动地填入变量。如果你需要手动填入一些变量,你需要显示“Advanced”变量(在窗口顶部的下拉框处)然后确保所有相关的变量被正确地设置(为了给定的依赖)。任何插件或例子如果你没有依赖将不会成为被创建出来的项目文件的一部分,它还会变得比一前更干净了(项目依然在那,但就是拒绝被构建,这会导致很多在噪音当你构建你想构建的组件时)

Once your build configuration is to your liking, click Configure until the Generate button is enabled, then click that. Once it's done generating the project files, you can close CMake.

一旦你构建配置符合了你的意愿,点击Configure直到Generate按钮变为可用,然后点击它。一旦干完了创建出来了项目文件,你就可以把CMake关闭了。

Building with Visual Studioyong用VS构建

Open the generated OpenSceneGraph.sln file, which will be in the directory you entered under "Where to build the binaries" (eg. OpenSceneGraph\build), with Visual Studio. Select your desired build type (Debug, Release, RelWithDebugInfo, MinSizeRel) and press F7 for "Build Solution". Assuming everything builds correctly, you can then right-click on the INSTALL project and build that, which will copy the compiled files to the correct directories (based on what you set CMAKE_INSTALL_PREFIX to). Before that, the compiled files all reside in the build directory, and I don't recommend you use them from there.

用VS打开构建出来的OpenSceneGraph.sln文件,它在你之前在"Where to build the binaries" 的那个地方填入的那个目录。(例如:OpenSceneGraph\build)。选择你喜欢的构建类型(Debug, Release, RelWithDebugInfo, MinSizeRel)然后按下F7来"Build Solution".假设每一个构建都顺利进行,你可以右键点击INSTALL 项目,然后构建,这将拷贝编译好的文件到正确的目录中(基于你设定的CMAKE_INSTALL_PREFIX),在那之前,编译后的文件都位于这个build目录中,我不建议你们在那里使用他们。

Just to reiterate: Always build the INSTALL project. The locations where the files are copied to as part of the INSTALL target are the correct locations from where you should use them (bin\ for application executables and DLLs, lib\ for libraries, include\ for headers, and share\OpenSceneGraph\bin\ for example executables). See "Environment variables" below for some tips to set up your build environment for your own project to use the files from the correct locations.

反复重申:总是构建这个INSTALL项目。。。后面的没看的太懂,但是应该是和目录有关系,和环境变量有关系。

Notes

COMPILATION TIME编译时间

A word of warning, the first time you compile from source it will take some time. After that, if you update an SVN checkout for example, it will compile incrementally (only what changed) so that's quite a bit faster.

一句叮嘱的话就是第一次从源代码编译的时间会比较长,之后如果你更新了一个SVN 检出,它将会是增量式的编译(只编译变化的部分),就快多了。

ALWAYS REGENERATE AFTER UPDATES更新后总是REGENERATE

You should regenerate the project files with CMake each time you update from SVN or update to a newer snapshot of the source code. That will make sure that a) any new build configuration options added to the CMake build files will be taken into account, and b) any new files will be included in the VS projects and compiled correctly. You can regenerate the build files by just opening the root CMakeLists.txt in the CMake GUI and setting the build directory to the same thing you had before (OpenSceneGraph\build, for example), that way any settings you had made will still be there. Just click Configure then Generate, open the OpenSceneGraph.sln and build.

每一次你从SVN更新了你都应该使用CMake重新创建这个项目文件或者更新到源代码的一个新的快照。这将确保:

  1. 任何新构建的配置选项都添加到了CMake构建文件,然后它会被考虑进去。
  2. 任何新的文件将会被包含在VS项目中并被正确编译。

你可以用CMakeGUI重新生成这个build 文件通过打开根目录的CMakeLists.txt 文件 ,并设定build目录就像你以前做的那样(例如OpenSceneGraph\build),以前的设定还会在那里。仅仅需要点击Configure然后Generate打开OpenSceneGraph.sln然后build。

FREETYPE PLUGIN自由类型的插件

If have the 3rdparty libraries installed, the BUILD_OSG_PLUGINS is ON in your CMake file and the FREETYPE_LIBRARY_DEBUG path is found it might still happen that the project files for Freetype plugins or not built. Select "Show Advanced Values" in CMake and copy the directory found in FREETYPE_INCLUDE_DIR to FREETYPE_INCLUDE_DIR_freetype2 and FREETYPE_INCLUDE_DIR_ft2build.

如果你安装了第三方的插件, BUILD_OSG_PLUGINS在你的CMake文件中就是ON,FREETYPE_LIBRARY_DEBUG路径会被发现它可能还是会发生的,这个项目文件是为了Freetype插件,而不是为了构建。CMake中选择“Show Advanced Values”,拷贝FREETYPE_INCLUDE_DIR目录中的文件到FREETYPE_INCLUDE_DIR_freetype2中和FREETYPE_INCLUDE_DIR_ft2构建。

CLEANUP THE CMAKE CACHE清理CMAKE缓存

Finally, note that from time to time, to see new settings, you will need to delete the CMakeCache.txt file in the build directory, which will also erase your settings... This should be rare though.

最后注意随着时间的增长,为了看到最新的设置,你需要删除build目录中的 CMakeCache.txt文件,这也会删除你的设置。。。这应该是很少见的。

Extensionless headers and syntax highlighting on Visual Studio 2003/2005/2008VS 2003、2005、2008中无扩展的头文件和语法高亮

In the OSG sources, in the PlatformSpecifics\Windows directory, there is a text file VisualStudio_Syntax_Highlighting.txt in which you can find instructions to get syntax highlighting on extensionless header files. It basically says:

在OSG源码中,在PlatformSpecifics\Windows目录下有一个文件叫VisualStudio_Syntax_Highlighting.txt你可以得到指示来得到语法高亮在那些无扩展的头文件上。它基本上是这样说的:

Go to Tools-Options, then Text Editor-File Extension, check "Map Extensionless Files To" at the bottom and select "Microsoft Visual C++" in the list to the right.

中文略。

Starting a new project开始一个新项目

There are two basic ways to start a new project using the OSG.

有两个基本的方式来使用OSG开始一个新项目。

Create a new solution and project, and add the OSG libs as dependencies

Pros:创建一个新的解决方案和项目,然后添加OSGlibs作为依赖

  • Keeps everything separate, you can keep a tight control over the files in your project.保持每个事物是独立的,你可以紧密地控制项目中的文件。
  • If your own project is in SVN, it won't conflict with the OSG SVN.如果你自己的项目在SVN中,它不会和OSG SVN冲突。
  • It's easy to distribute your project.分发你的项目是容易的。

Cons:

  • If you need to modify the OSG or look something up, you need to open the OSG's workspace/solution separately.如果你需要修改OSG或者查找一些东西,你需要分别打开OSG的工作空间/解决方案。

I normally start the project by copying OpenSceneGraph\applications\osgViewer\osgViewer.cpp into my new project's src directory, and compiling that. If that doesn't compile and run, there's a problem. See "Environment Variables" below for how to set up the compiler's include and library search paths easily.

我一般是开始项目通过拷贝OpenSceneGraph\applications\osgViewer\osgViewer.cpp到我的新的项目的src 目录下,然后编译它。如果它不能编译和运行,就是有问题了。看下面的“环境变量”去查找如何安装编译器的include and library search 路径。

Create a project directly in the OSG workspace / solution直接在OSG的工作空间/解决方案中创建一个项目

Pros:优点

  • If you have to modify OSG itself for your project, the Visual Studio dependency check will recompile what is needed automatically.如果你不得不修改OSG,VS的依赖检查将会自动检查已经自动添加了什么。
  • You can quickly switch from the project on which you're working to the OSG code itself.可以很快地在你的项目和OSG code之间切换
  • You can examine code for any src/example easily.可以快速地查看任何的src下面的例子代码。

Cons:缺点

  • Your OSG workspace/solution will quickly become crowded once you start adding lots of your projects to it (as if it wasn't enough already...).你的OSG workspace/solution将快速地变得拥挤,如果你添加了很多个项目进去(甚至不够用)
  • When you regenerate the build files with CMake, you will have to re-add your project(s) to it.当你用CMake重新创建构建文件的时候,你不得不重新添加你的项目进去。
  • It's hard to separate your project from the OSG for distribution, so the other way is "cleaner".在分发的时候很难将你的项目和OSG分开,所以另外的办法就是“cleaner”
  • You can't keep your project in SVN if your OSG directories are also from SVN, unless you copy your project's files somewhere else, in which case why don't you just use the first method instead of having to do both?

你不可以在SVN中保持你的项目如果你的OSG目录也是从OSG来的,除非你拷贝你是从其他的地方拷贝的你的项目文件。

I guess you can see that I don't recommend this way of doing. The OSG workspace/solution is already so big as it is, I think it's best to keep your mind uncluttered by creating a separate project for your own stuff. The rest of this guide is based on using the first option.我并不建议这么做。OSG workspace/solution 已经很大了,我认为最好牢记不要搞乱,自己去建立一个单独的工程比什么都强。

Environment variables环境变量

If you select the first method, a way to simplify things and to make your project files work on multiple different machines is to use some standard environment variables. I use the following:

如果你选择了第一种方法,一个方法可以令事情变得简单可以让你的项目在各个不同的机器上工作,这就是使用标准的环境变量,我使用如下的:

OSG_ROOT points to the base of the OSG file structure (the directory that contains includesrc etc. subdirectories)
OSG_BIN_PATH = %OSG_ROOT%\bin
OSG_INCLUDE_PATH = %OSG_ROOT%\include
OSG_LIB_PATH = %OSG_ROOT%\lib
OSG_SAMPLES_PATH = %OSG_ROOT%\share\OpenSceneGraph\bin
OSG_FILE_PATH = ???\OpenSceneGraph-Data-X.X

Then, add %OSG_BIN_PATH% and %OSG_SAMPLES_PATH% to your PATH environment variable. That way, not only can you run examples easily, but the latest DLLs will always be found. When starting an application, Windows looks for the required DLLs first in the executable's directory, then in the PATH.

Make sure you restart Visual Studio if it was open when you added/changed these environment variables, so that it picks up te changes.

In your project's properties, use those environment variables to get Visual Studio to find the OSG libraries. Here are the settings I use:

Properties - C/C++ - General - Additional Include Directories = $(OSG_INCLUDE_PATH)
Properties - C/C++ - Preprocessor - Preprocessor Definitions = WIN32;_WIN32;NDEBUG
Properties - Linker - General - Additional Library Directories = $(OSG_LIB_PATH)
Properties - Linker - Input - Additional Dependencies = (any OSG library your project needs - for example: osg.lib osgGA.lib osgDB.lib osgViewer.lib osgText.lib osgUtil.lib OpenThreads.lib)

Remember to do the same thing in your project's Debug configuration, but add a d to the end of the OSG and OpenThreads library names (osg.lib becomes osgd.lib and so on). Also change NDEBUG to _DEBUG in the Preprocessor Definitions.

Once that's done, your project should be able to compile, link and run. If not, you can test your paths in a console (Start-Run, type cmd, press enter), by for example typing echo %OSG_LIB_PATH% or dir %OSG_LIB_PATH%. With those commands you should be able to see what is wrong.

Important note about plugins关于插件的重要说明

Once in a while, we get a message on the mailing list asking why the version number is added to the bin\osgPlugins directory name. Normally, the reason they ask is that after updating their copy of OSG, they had to modify their project files or their PATH to change the version number to point to the new directory.

There is never a need to add that directory to your library search paths or PATH environment variable. The OSG knows how to find the plugins by itself. In general, it will try to add osgPlugins-<version> to the directories in your PATH until it finds the right one. And since it knows its own version, it will find the right plugins directory.

没有必要去添加目录到你的库搜索路径或者PATH环境变量。OSG知道如何查找插件。一般来说,它会尝试添加osgPlugins-<version>到PATH追踪,直到它发现了正确的。由于它直到它自己的版本,它将会寻找正确的目录。

If you had to add that directory to your PATH, for example in order to get examples to load the .osg files (cow.osg for example), that means that you didn't build the INSTALL target after compiling OSG. In that case, the plugins will reside in directories like ...\OpenSceneGraph\build\lib\osgPlugins-<version>\release. So the problem is not the version added to the directory name, it's the lib instead of bin, and the release subdirectory. That's because CMake builds DLLs in the lib tree, and then the INSTALL target copies them to the bin tree, more specifically to ...\OpenSceneGraph\bin\osgPlugins-<version>. And since ...\OpenSceneGraph\bin is on your PATH, OSG will find them fine.

More generally, you should never have to put a reference to a subdirectory of the build directory into your environment variables. That is just a temporary location where things get built. Once the INSTALL target is run, the files will be in their final locations, which are bin\ for application executables and DLLs, lib\ for libraries, include\ for headers, and share\OpenSceneGraph\bin\ for the example executables.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值