Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DllImport doesn't work as advertised in Mono (Linux, C#)

I'm getting to know Mono development in Linux, in baby steps. I'm trying to call Linux C libraries. This page, in theory, tells me how, but when I type the code below in MonoDevelop 2.2.2 (Fedora 13), I get a "Parsing Error (CS8025)" in "private static extern int getpid();". Moreover, the help system doesn't work.

using System;
using System.Runtime.InteropServices;

[DllImport("libc.so")]
private static extern int getpid();

namespace LinuxCaller
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Console.WriteLine ("Hello World!");
        }
    }
}
like image 747
JCCyC Avatar asked Aug 27 '10 16:08

JCCyC


1 Answers

Function definitions cannot appear in the namespace scope in C#. This includes DLL import definitions. To fix this just move the function definition inside a type.

class MainClass {
  [DllImport("libc.so")]
  private static extern int getpid();

  ...
}
like image 91
JaredPar Avatar answered Sep 29 '22 17:09

JaredPar