none
Create DLL with F# RRS feed

  • Question

  • Hello,

    I am new to F# and coding in general.  I am having trouble understanding how to create a DLL with F#.  I've read all the posts, help, support, and tutorials, but nothing is specific to F#, just C++/#.  

    It seems F# requires two files, one with the functions and one to somehow make it available to other programs (in this case, Excel).  I cannot determine what file types to use (.fs, .fi) and what headers are needed.  

    Can someone help with a set of instructions on how to create this solution in VSTO?  I only need to know the file types and verbiage to create the DLL.  Hopefully, this is a small request...  :)

    http://msdn.microsoft.com/en-us/library/ee241127.aspx

    http://msdn.microsoft.com/en-us/library/ms682507(v=VS.85).aspx

    http://msdn.microsoft.com/en-us/library/ms682592(v=VS.85).aspx

    Monday, November 26, 2012 2:09 AM

All replies

  • At the very simplest you just need a .fs file with your code in it, and build it like

    fsc --target:library myfile.fs

    An F# library project in Visual Studio will do essentially this (just with a bunch more flags that get in the way as this example from MSBuild output of an F# library project shows

    C:\Program Files\Microsoft F#\v4.0\fsc.exe -o:E:\hg\tinesware-tools\Infrastructure\_Intermediate\Tinesware.Infrastruc
    ture\Debug+AnyCPU\Tinesware.Infrastructure.dll -g --debug:full --noframework --define:TRACE --define:DEBUG --define:C
    ODE_ANALYSIS --doc:E:\hg\tinesware-tools\Infrastructure\_Binaries\Tinesware.Infrastructure\Debug+AnyCPU\Tinesware.Inf
    rastructure.XML --optimize- --tailcalls- -r:C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll -r:"C:\Program
     Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll" -r:C:\Windows\Microsoft.NET\Framework\v2.0.5072
    7\System.dll --target:library --warn:4 --warnaserror --warnaserror:76 --fullpaths --flaterrors --keyfile:E:\hg\tinesw
    are-tools\Infrastructure\_Tools\Infrastructure.snk AssemblyInfo.fs ..\..\_Generated\AssemblyVersion.fs CoverageExempt
    ion.fs CustomDictionary.fs Extensions.fs Require.fs Trace.fs

    The .fsi interface file is optional; its purpose is to define the visibility of types outside the assembly, if the default behaviour is not what you want.

    A .fs source file looks like

    #light                         // optional these days
    namespace My.Namespace.Here
    open System
    open Other.Dependency
    // other namespace references
    
    module MyModule =  // or type MyType =
     // code 
    
    // more types or modules
    The thing that differs from C# or VB is that file order in the project is important -- code or interfaces in one file can only refer to definitions earlier in the list.

    Monday, November 26, 2012 8:09 AM
  • Hello again.  Thank you for your prompt response.

    I tried to follow your directions, but am having trouble.  Below, I've listed the steps I took.  Will you please point out my error?

    1. I created a new 'F# Application' project in VSTO 2012, which started with References: (FSharp.Core, mscorlib, System, System.Core, System Numerics), Program.fs, and App.config.

    2. I deleted the code:

    // Learn more about F# at http://fsharp.net
    // See the 'F# Tutorial' project for more help.
    
    [<EntryPoint>]
    let main argv = 
        printfn "%A" argv
        0 // return an integer exit code

    and replaced it with your code, albeit modified:

    #light                         // optional these days
    namespace My.Namespace.Here
    open System
    open System.Numerics 
    
    module MyModule =  
        let testIt x = x + x

    3.  I didn't know what the following meant.  I tried it in a few places with no luck...

    fsc --target:library myfile.fs
     

    4. When compiled I get the error: "Main module of program is empty: nothin wll happen when it is run.

    5. Despite the error a dll is created and when I link it to Excel the function "testIt" shows, but produces an error and sometimes crashes Excel.

    I really appreciate your help.  Once I get this I will be able to make some real headway!

    Is it possible for you to post a file somewhere that I can download?  :)



    • Edited by Uziel9999 Tuesday, November 27, 2012 5:04 AM
    Tuesday, November 27, 2012 5:02 AM
  • When it says "F# Application", that's a project to build an executable; you want "F# Library" to set up for a DLL.  In an Application you expect to have a main entrypoint method (marked with [<EntryPoint>]) where the program starts off (hence the error message you see).

    The F# code you entered is equivalent to this in C#

    using Microsoft.FSharp.Core;
    using System;
    namespace My.Namespace.Here
    {
    	[CompilationMapping(SourceConstructFlags.Module)]
    	public static class MyModule
    	{
    		public static int testIt(int x)
    		{
    			return x + x;
    		}
    	}
    }
    

    Modules are just static classes and the functions within them static methods.

    I've not done any programming against Excel, so I'm not going to start guessing at what is going wrong here

    ---

    The line

    fsc --target:library myfile.fs

    is something to issue at a command prompt to launch the F# compiler to build a library from the one source file.

    Tuesday, November 27, 2012 7:57 PM