views:

134

answers:

2

Hi,

I've got a lexer created with flex (cygwin). Normally I compile it to an .exe file.

For the newest project I need a lexer to use in a bigger C# program running on Windows XP. Of course I can execute a file using System.Diagnostics.Process. But it is not the best solution for me as I want that program to run on several machines.

How can I create a dll under cygwin having the source code of the lexer?

Thanks in advance... Szpilona

A: 

Flex produces a C or C++ source file with a yylex() function.

Run flex on your grammar (.l file) to produce a .c or .cpp file, then compile that file with gcc -c to produce an object file, or compile it with gcc -shared to produce a .so library.

flex toy.l
gcc -shared toy.lexer.c

Flex or Flex++ will produce a lexer that can be compiled with or without cywgin. You can also use MinGW or Visual C++.

mrjoltcola
Well, I've already tried that. Precisely I ran flex on my grammar and then compiled the obtained c file with:gcc -c lekser.cgcc -shared -o lekser.dll lekser.odll file was created but it does not work. I've tried to access the library using the code: [DllImport("lekser.dll")] public static extern int yylex();but the DllNotFoundException occured. The file, for sure, is placed in the right directory and the C# code is correct (at least it works with another dll I've created to test this). That's why I thought there was an option needed when running flex on grammar file.
Szpilona
Is the dll in your PATH? That exception is a "file found found" type of exception.
mrjoltcola
A: 

I'm not sure what you mean by "dll in my PATH". But if you suggest that it I just put the file in the wrong directory, look at the code below. To be sure that I did every think all right I created another dll file (testDll) and put it in the same place as lekser.dll. The code works fine for testDll.dll but for lekser.dll throw the mentioned DllNotFoundException...

    [DllImport("testDll.dll")]
    public static extern int Sum(int a);
    [DllImport("lekser.dll")]
    public static extern int yylex();

    static void Main(string[] args)
    {
        Console.WriteLine("First library:");
        int sum = Sum(2);
        Console.WriteLine("sum = {0}", sum);
        int flag = yylex();
        Console.WriteLine("flag = {0}");
        Console.ReadKey();
    }

That's why I suggested that something wrong is with the dll...

Szpilona