This guide is an overview of resources for cross-platform development with F#. To contribute to this guide, log on to GitHub, edit this page and send a pull request.

Table of Contents

Getting Started

Command Line Tools

You can start F# Interactive using

$ fsharpi

> 1+1;;

val it : int = 2

You’re off! Some common commands are:

fsharpi                            (starts F# interactive)
fsharpc file.fs                    (F# compiler)
xbuild                             (builds .fsproj projects and .sln files)
mono file.exe arg1 ... argN        (runs a compiled F# program)
mkbundle --static file.exe -o file (makes a static native image, including the F# runtime)

Editing

Some editors have specific support for F#, either builtin or through addons provided by the F# community:

If running F# Interactive in Emacs or another similar environment, use

> fsharpi --readline- 

to turn off console processing.

Documentation

For most F# documentation, see the documentation pages.

Projects and Build Automation

.fsproj and .sln files

You can use xbuild to build projects and solutions from Visual Studio without change. Xamarin Studio and MonoDevelop can also create and manage .fsproj and .sln files.

Use xbuild to build projects and solutions:

xbuild RocketPart.fsproj
xbuild RocketSolution.sln

Many people doing cross-platform or Mac/Linux development don’t like .sln files. If so, you can also create a .fsproj file that brings together a collection of .fsproj files. Include, for example, root.traversals.targets used in the F# compiler source in a .fsproj like this:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">

  <ItemGroup>
    <ProjectFiles Include="fsharp-proto-build.fsproj"/>
    <ProjectFiles Include="fsharp-library-build.fsproj"/>
    <ProjectFiles Include="fsharp-compiler-build.fsproj"/>
  </ItemGroup>

  <Import Project="root.traversal.targets"/>

</Project>

To create a .fsproj file from scratch yourself, either install Xamarin Studio or MonoDevelop, or find an existing one, copy it and edit it by hand.

Makefiles

The F# command-line compiler (fsharpc) can be used with Makefiles in the usual way.

FAKE

FAKE is a build-and-publish automation utility sometimes used by F# programmers (partly because builds are automated using F# itself, and partly because it is a great tool).

FAKE can be fetched using NuGet.exe, e.g.:

# Get nuget.exe
sudo mozroots --import --sync
curl -L http://nuget.org/nuget.exe -o nuget.exe

# Get FAKE.exe
mono nuget.exe install FAKE -OutputDirectory lib -ExcludeVersion -Prerelease

Package Repositories

nuget

nuget is an open-source, cross-platform package management tool with many thousands of packages available. See the documentation. It is used extensively on Windows ecosystem but is growing in its cross-platform use.

Using nuget from the command line

For those on Mac/Linux, familiarity with the command-line NuGet.exe utility is highly useful. Get the command line utility like this:

# Get nuget.exe
sudo mozroots --import --sync
curl -L http://nuget.org/nuget.exe -o nuget.exe

Before using NuGet.exe on Mac/Linux, be sure to run

 mozroots --import --sync

Typical usage is:

mono nuget.exe install                               -- installs from packages.config
mono nuget.exe install packageId -Version 2.2.2.3    -- installs particular version of particular package

An example packages.config is:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="FsUnit" version="1.2.1.0" targetFramework="net40" />
 <package id="NUnit" version="2.6.2" targetFramework="net40" />
</packages>

See also the documentation.

Using nuget packages as part of a build

F# project files (.fsproj) can be configure to automatically get NuGet packages during a build. The project file should have a line like this (adjust the ‘…’ to reference a copy of NuGet.targets checked into your project).

<Import Project="...\NuGet.targets" Condition="Exists('...\NuGet.targets')" />

A copy of NuGet.exe should be in that directory with executable permissions set. You may also need to set:

export EnableNuGetPackageRestore=true

It is quite common to check a copy of NuGet.exe into a project, e.g. in lib/NuGet/NuGet.exe.

Using nuget in MonoDevelop and Xamarin Studio

You can add NuGet support to these IDEs if not already present. See NuGet for MonoDevelop and Xamarin Studio.

Making nuget packages

See http://nuget.org to learn how to make and publish NuGet packages, or look at examples from other F# community projects.

Other Packaging Mechanisms

Other packaging mechanisms include: * Git sub-modules (especially when building from source) * Traditional Unix packages * Simple .fs files that can be included into projects

Some Examples of Portable, Cross-Platform Packages

Some F# and CLI packages are more cross-platform friendly than others. Many will work with no alteration. Here are some of interest:

In the wider F# exosystem there are many cross-platform and/or portable packages and libraries. Here are some examples:

There are a wide range of cross-platform libraries available as part of the core libraries available to F#, for example:

Compatibility guides, documentation and migration assessment tools are also available.

Portable Libraries

Portable .NET libraries have access to less core functionality, called a “portable profile”, but can be used across multiple platforms and multiple profiles of .NET. For example, a portable library may be usable on Mac, Linux, Android, iOS, Windows and Windows Store apps (depending on the versions of runtime machinery used and other factors).

See Cross-Platform Portable Class Libraries with .NET are Happening for a perspective on cross-platform portable libraries for Visual Studio users.

F# portable libraries reference FSharp.Core versions such as 2.3.5.1, with matching mscorlib versions. A binding redirect may be neeeded to ensure bindings to these libraries redirect correctly, e.g. to FSharp.Core 4.3.1.0.

At the time of writing, creating portable libraries was not yet fully supported in IDES on Mac and Linux, though you can normally build portable libraries successfully using xbuild and command-line tools on these platforms. If you are developing on Windows, or using libraries on Windows, then consider creating portable libraries to ease cross-platfom use.

Unit Testing

A table of .NET unit testing frameworks can be found here.

There are two major ways of writing unit tests in .Net; in a method-per-test style, having the framework use reflection to read the tests and execute them or with the test-is-a-value style, where you pass the framework your test values (much like immutable objects are values).

Fuchu

Fuchu is a test library for .NET, supporting C# and VB.NET but with a special focus on F#. It draws heavily from Haskell’s test-framework and HUnit. You can read about the rationale and underlying concepts in this blog post, or tests should be first-class values so that you can move them around and execute them in any context that you want. Also, if they are first-class values, then you can take extra care with what the test methods return, making integrations with external libraries much cheaper.

Install-Package Fuchu

Since tests are values, it’s easy to extend the framework to integrate with other tooling, e.g. with FsCheck to use a fuzzing/randomization approach to testing (see below)

Install-Package Fuchu.FsCheck

Fuchu also has an integration with PerfUtil which can be used to automate performance testing and trending in a continuous integration environment.

Install-Package Fuchu.PerfUtil

FsCheck

FsCheck is a tool for testing .NET programs automatically. The programmer provides a specification of the program, in the form of properties which functions, methods or objects should satisfy, and FsCheck then tests that the properties hold in a large number of randomly generated cases.

NUnit

NUnit is an open-source, cross-platform unit-testing framework for F# and other .NET languages. It is written in C# and has been completely redesigned to take advantage of many .NET language features, for example custom attributes and other reflection related capabilities. Also xUnit is a good alternative for NUnit.

Some guides to using F# and NUnit together are:

FsUnit

FsUnit is often used by F# programmers as an DSL to access popular unit testing frameworks. An FsUnit NuGet package is available.

Continuous Integration builds

Using Travis and AppVeyor

Perhaps the simplest way to regularly build and test your work across multiple platforms is to use Travis.

You can automate the build and test of all commits and pull requests to GitHub projects on Linux and OSX by using Travis. This is very easy to arrange, just add a .travis.yml file to your project root (example, example ), and register the project in your Travis account.

Setting the language to “objective-c” causes Travis to use an OSX machine for build.

Travis is free for open source projects.

To also automate your build and test on Windows, AppVeyor is a good choice. Here is an example configuration file.

Miscellaneous Notes on Open Source and Cross Platform Development

Testing on multiple platforms

If you are Windows developers, you can set up a Vagrant box in order to test your libraries and tools on Mono (though often a Travis build is simpler, see above) A detailed guide of setting up Vagrant is available here.

Dos and Don’ts

Developing Cross-Platform and Multi-Targeting Type Providers

F# type providers are compile-time components that must execute on your machine during build and development.

A type provider executing on Mac/Linux can expose some small differences in the implementation of .NET, for example in the System.Type implementation. The ProvidedTypes API can normally be adjusted to account for these.

To help isolate the problem, try the following:

Switching to command-line compilations will help localize the problem.

Having trouble?