Frequently Asked Questions

How do I create a Visual Studio project for Ice?

The easiest way to use Ice with Visual Studio is to use our Visual Studio plug-in. The plug-in automates the chores of adding Slice files to a project, generating the code, tracking dependencies among Slice files, and other things. See the Visual Studio Extension page for more information. The instructions that follow describe how to create Visual Studio projects from scratch, without the assistance of the plug-in.

The steps you have to go through to create a Visual Studio project depend on whether you are building a C++ application, C# application, or an application with another .NET language (such as Managed C++). Please use the links below to jump to the relevant topic.

Configuring Visual C++ directory paths

To build Ice applications with Visual C++, you must configure Visual C++'s directory paths. In the Tools menu, choose Options, and select Projects and Solutions. You can change the directories that Visual C++ searches in the VC++ Directories category. (You need to perform this step only once.)

You need to add three directories to the configuration:

  • In the Show directories for drop-down, select Executable files. Add the Ice bin directory to the list of directories. For example, if you have installed Ice in C:\Ice-3.3.1-VC90, add C:\Ice-3.3.1-VC90\bin to the list.
  • In the Show directories for drop-down, select Header files. Add the Ice include directory to the list of directories. For example, if you have installed Ice in C:\Ice-3.3.1-VC90, add C:\Ice-3.3.1-VC90\include to the list.
  • In the Show directories for drop-down, select Library files. Add the Ice lib directory to the list of directories. For example, if you have installed Ice in C:\Ice-3.3.1-VC90, add C:\Ice-3.3.1-VC90\lib to the list.

If you want to create 64-bit binaries, select x64 as the platform and set the directory for executables and libraries to bin\x64 and lib\x64, respectively; the include directory is the same as for 32-bit builds (include).

Creating a Visual C++ project

This section describes how to create a new Visual C++ project.

Create a new project

Select New in the File menu and choose New Project. The project template will normally be Win32 Console Application. However, you are free to choose a different project template for your application.

In the Application Settings dialog that follows, you can change settings to suit your needs. You can start with an empty project or, if you prefer, use precompiled headers.

Add Slice files and custom build steps

In your new project, right-click on Resource Files and select Add Existing Item. Select the Slice file you want to add to the project. Next, right-click on the newly-added file and select Properties. Set Configuration to All Configurations and provide the command to run slice2cpp and set the output file names produced when you compile the Slice file. Note the use of macros to specify the input and output file names.

If your Slice file includes any of the Slice files that ship with Ice, you must add the slice directory with the -I option to the list of directories that are searched for included files. For example, if Ice is installed in C:\Ice-3.3.1-VC90, add -IC:\Ice-3.3.1-VC90\slice to the command line.

If your Slice files include other Slice files, you must inform Visual C++ of the dependency. For example, if Demo.ice includes Included.ice, you must add Included.ice to the Additional Dependencies item for Demo.ice; otherwise, Visual C++ will not recompile Demo.ice when you make a change to Included.ice. Note that you need to specify the directory in which Included.ice is located with the -I option for the custom build step for Demo.ice:

Add C++ source and header files

Once you have added your Slice files to the project and specified the custom build steps, right-click on each Slice file and select Compile to generate the header and source files. You can now add the generated files to their respective project folders:

You also need to add the header and source files used by your application. In this case, we added a single source file Client.cpp. Note that you must add . (the current directory) to the include path of Client.cpp for both release and debug configurations because that is where slice2cpp places the generated files, and Client.cpp includes Demo.h.

Set project properties

You must ensure that the project property settings when you compile match those that were used to compile the Ice libraries. In particular, you must set Enable C++ Exceptions to Yes (/EHsc). For Runtime Library, select Multi-threaded Debug DLL (/MDd) for a debug build, and Multi-threaded DLL (/MD) for a release build. (The screen shot below shows the setting for a debug build.)

For the Language settings, ensure that Enable Run-Time Type Info is set to Yes.

Your project must link (at a minimum) with the IceUtil and Ice libraries. If you use other parts of Ice, such as Glacier2 or Freeze, you must link with the libraries for these as well. For a debug build, the libraries have d-suffix, so the libraries are iceutild.lib and iced.lib. for a release build, the d-suffix is missing, so the libraries are iceutil.lib and ice.lib.

Make sure that debug and release builds link with the correct library; otherwise, even though things may compile, the program will crash at run time (see this FAQ for more details).

For 64-bit builds, use Configuration Manager in the project properties to add x64 as a new platform and

Then set x64 as the active configuration in the project properties.

Creating a Visual C# project

This section describes how to create a new Visual C# project.

Create a new project

Select New in the File menu and choose New Project. The project template will normally be Console Application. However, you are free to use other templates.

Add targets to compile Slice files

Once you have created the project, save it and use a text editor to edit the project file (Demo.vcproj for this example). Near the end of the project file (before the </Project> tag), add the following text (modified for the name of your Slice file):

<Target Name="CompileSlice" Inputs="Demo.ice" Outputs="Demo.cs">
  <Exec Command="slice2cs Demo.ice"/>
</Target>
<Target Name="AfterClean">
  <Delete Files="Demo.cs"/>
</Target>
<PropertyGroup>
  <CompileDependsOn>
    CompileSlice;$(CompileDependsOn)
  </CompileDependsOn>
</PropertyGroup>

Note that you must have the Ice bin directory in your PATH for this to work.

If you have several Slice files, you can list them separated by semicolons, for example:

<Target Name="CompileSlice" Inputs="Demo.ice; Demo2.ice" Outputs="Demo.cs; Demo2.cs"> 

This also works for the AfterClean target.

If you update the project settings with Visual Studio, the targets in the project file for compile your Slice files will not be overwritten.

If your Slice files include other Slice files, you must tell Visual C# about the dependencies between them. For example, if Demo.ice includes Included.ice, set up your project as follows:

<Target Name="Included" Inputs="Included.ice" Outputs="Included.cs">
<Exec Command="slice2cs Included.ice"/>
</Target>
<Target Name="Demo" DependsOnTargets="Included" Inputs="Demo.ice" Outputs="Demo.cs">
<Exec Command="slice2cs Demo.ice"/>
</Target>
<Target Name="AfterClean">
<Delete Files="Demo.cs; Included.cs"/>
</Target>
<PropertyGroup>
<CompileDependsOn>
Demo;$(CompileDependsOn)
</CompileDependsOn>
</PropertyGroup>

The DependsOnTarget attribute ensures that, if you edit Included.Ice, Visual C# will recompile both Demo.ice and Included.ice.

Add source files

After adding the targets to your project, build your project. This runs slice2cs and generates your C# source files. You can now add the generated source files to your project, along with any other source files for your application. In this example, we have a single application source file Client.cs:

Add assembly references

At a mimimum, your application must reference the Ice.dll assembly, which contains the Ice run time. If your application uses other Ice features, such as Glacier2 or IceGrid, you must add references for these assemblies as well. To add the references, right-click on the References folder of the project and select Add Reference. In the dialog, select the Browse tab and navigate to the bin directory of your Ice installation. Add Ice.dll and whatever other assemblies your application needs.

Creating a Visual Studio project for another .NET language

You can use the Ice for .NET run time for any CLR-compliant programming language. For example, you can create an Ice application with Visual Basic. However, Ice for .NET generates C# source code from Slice files, which you cannot directly use in a Visual Basic project. The solution is to compile the C# source files that are generated by slice2cs into a separate assembly. Your Visual Basic application then links against the Ice run time assembly (Ice.dll) and the assembly containing the compiled Slice files.

To create an assembly for your Slice files, follow the steps for creating a Visual C# project, but select Class Library as your project template. Then reference that assembly in your Visual Basic project.

Terms of Use | Privacy © 2010 ZeroC, Inc.