caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Embedding Ocaml in a windows application
@ 2014-05-09  7:42 Leonardo Laguna
  2014-05-09 12:13 ` Adrien Nader
  0 siblings, 1 reply; 8+ messages in thread
From: Leonardo Laguna @ 2014-05-09  7:42 UTC (permalink / raw)
  To: caml-list

Hello,

I’m trying to make a plugin for a third party application using Ocaml.  This
plugin is a shared library that is usually written in C, so you take your C
code and link it with a static library (provided by the software vendor) in
order to get a shared library that can be loaded by the application.  I made a
small test in OSX that embeds Ocaml  (as shown in ‘Interoperability with C’ of
the book ‘Developing applications with Objective Caml’) and works fine.
However in windows I’m running into problems.

The shared library that the vendor provides is compiler with VC++, therefore
is not possible to link object files produced by the Cygwin neither MinGW
port. For that reason I tried to compile the MSVC port of Ocaml and I didn’t
succeed.

I followed the instructions in the README.win32. The first problem I had was
that flexlink was not able to call ‘link’, so I downloaded the flexlink code
and changed it to call instead ‘link.exe’, this worked. Then flexlink could
not handle Cygwin paths like ‘/tmp/’or ‘/cygdrive/c/’ . I modified the code so
it replaces the Cygwin paths to Windows paths. It worked. I continued until I
got the message that the ‘ml’ command does not exist. The environment is set
correctly and I can call ‘ml’ from the terminal.

Has anyone tried to compile the MSVC port lately?

I wonder if it’s possible to do a kind of cross-compilation of Ocaml code,
like, taking the cygwin Ocaml compiler and tell it to use the MSVC tools for
the final executable or object files.
Is there any chance to get my plugin working on windows?

Thank you beforehand,
Leonardo

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Embedding Ocaml in a windows application
  2014-05-09  7:42 [Caml-list] Embedding Ocaml in a windows application Leonardo Laguna
@ 2014-05-09 12:13 ` Adrien Nader
  2014-05-09 20:16   ` Leonardo Laguna
  0 siblings, 1 reply; 8+ messages in thread
From: Adrien Nader @ 2014-05-09 12:13 UTC (permalink / raw)
  To: Leonardo Laguna; +Cc: caml-list

Hi,

On Fri, May 09, 2014, Leonardo Laguna wrote:
> Hello,
> 
> I’m trying to make a plugin for a third party application using Ocaml.  This
> plugin is a shared library that is usually written in C, so you take your C
> code and link it with a static library (provided by the software vendor) in
> order to get a shared library that can be loaded by the application.  I made a
> small test in OSX that embeds Ocaml  (as shown in ‘Interoperability with C’ of
> the book ‘Developing applications with Objective Caml’) and works fine.
> However in windows I’m running into problems.
> 
> The shared library that the vendor provides is compiler with VC++, therefore
> is not possible to link object files produced by the Cygwin neither MinGW
> port. For that reason I tried to compile the MSVC port of Ocaml and I didn’t
> succeed.

Depends: if the library is C then you can perfectly mix the two
compilers. Keep in mind that when you build with GCC on Windows, you're
still using the Windows libraries like msvcrt.dll or kernel32.dll and
they've been built using MSVC.

> I followed the instructions in the README.win32. The first problem I had was
> that flexlink was not able to call ‘link’, so I downloaded the flexlink code
> and changed it to call instead ‘link.exe’, this worked. Then flexlink could
> not handle Cygwin paths like ‘/tmp/’or ‘/cygdrive/c/’ . I modified the code so
> it replaces the Cygwin paths to Windows paths. It worked. I continued until I
> got the message that the ‘ml’ command does not exist. The environment is set
> correctly and I can call ‘ml’ from the terminal.
> 
> Has anyone tried to compile the MSVC port lately?

IIRC even the MSVC port is built from Cygwin and I think it won't mind
whether there are trailing '.exe' or not.
But now I'm not sure I understand: what is calling flexlink? Is it your
build system or the ocaml compiler's?

-- 
Adrien Nader

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Embedding Ocaml in a windows application
  2014-05-09 12:13 ` Adrien Nader
@ 2014-05-09 20:16   ` Leonardo Laguna
  2014-05-09 20:20     ` Adrien Nader
  0 siblings, 1 reply; 8+ messages in thread
From: Leonardo Laguna @ 2014-05-09 20:16 UTC (permalink / raw)
  To: Adrien Nader; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 3271 bytes --]

I have given up on compiling the msvc port. I don't know why I'm getting
errors when the '.exe' is not there.

I will try to explain better what I want to do.

I have the following files:

- plug.lib (provided by the software vendor, closed source, compiled with
VC++), in OSX I have plug.a
- my_main.c (the code for the plugin)
- ocaml_stub.c (this code calls 'caml_startup()')
- ocaml_code.ml

in OSX I compile my_main.c, ocaml_stub.c and ocaml_code.ml to obtain '.o'
files. Then I link the .o files with plug.a and libasmrun.a. This works
fine.

In windows I have wodi32, visual studio 2008 and Flexdll. I tried the
following.
- Using cl compile, ocaml_stub.c, my_main.c to obtain '.obj' files
- Using ocamlopt, compile ocaml_code.ml to obtain a '.o' file
- Using flexlink to link the .obj, .o, libasmrun.a, plug.lib, libgcc.a and
libc.a

This gives me an error:

** Cannot resolve symbols for libasmrun.a(floats.o):
 ___strtod

I have tried to create my own strtod function and link it does not pick it.

Anybody knows how can I link my program.

Thanks.

Leonardo





On Fri, May 9, 2014 at 2:13 PM, Adrien Nader <adrien@notk.org> wrote:

> Hi,
>
> On Fri, May 09, 2014, Leonardo Laguna wrote:
> > Hello,
> >
> > I’m trying to make a plugin for a third party application using Ocaml.
>  This
> > plugin is a shared library that is usually written in C, so you take
> your C
> > code and link it with a static library (provided by the software vendor)
> in
> > order to get a shared library that can be loaded by the application.  I
> made a
> > small test in OSX that embeds Ocaml  (as shown in ‘Interoperability with
> C’ of
> > the book ‘Developing applications with Objective Caml’) and works fine.
> > However in windows I’m running into problems.
> >
> > The shared library that the vendor provides is compiler with VC++,
> therefore
> > is not possible to link object files produced by the Cygwin neither MinGW
> > port. For that reason I tried to compile the MSVC port of Ocaml and I
> didn’t
> > succeed.
>
> Depends: if the library is C then you can perfectly mix the two
> compilers. Keep in mind that when you build with GCC on Windows, you're
> still using the Windows libraries like msvcrt.dll or kernel32.dll and
> they've been built using MSVC.
>
> > I followed the instructions in the README.win32. The first problem I had
> was
> > that flexlink was not able to call ‘link’, so I downloaded the flexlink
> code
> > and changed it to call instead ‘link.exe’, this worked. Then flexlink
> could
> > not handle Cygwin paths like ‘/tmp/’or ‘/cygdrive/c/’ . I modified the
> code so
> > it replaces the Cygwin paths to Windows paths. It worked. I continued
> until I
> > got the message that the ‘ml’ command does not exist. The environment is
> set
> > correctly and I can call ‘ml’ from the terminal.
> >
> > Has anyone tried to compile the MSVC port lately?
>
> IIRC even the MSVC port is built from Cygwin and I think it won't mind
> whether there are trailing '.exe' or not.
> But now I'm not sure I understand: what is calling flexlink? Is it your
> build system or the ocaml compiler's?
>
> --
> Adrien Nader
>

[-- Attachment #2: Type: text/html, Size: 4316 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Embedding Ocaml in a windows application
  2014-05-09 20:16   ` Leonardo Laguna
@ 2014-05-09 20:20     ` Adrien Nader
  2014-05-09 20:27       ` Leonardo Laguna
  0 siblings, 1 reply; 8+ messages in thread
From: Adrien Nader @ 2014-05-09 20:20 UTC (permalink / raw)
  To: Leonardo Laguna; +Cc: caml-list

On Fri, May 09, 2014, Leonardo Laguna wrote:
> I have given up on compiling the msvc port. I don't know why I'm getting
> errors when the '.exe' is not there.
> 
> I will try to explain better what I want to do.
> 
> I have the following files:
> 
> - plug.lib (provided by the software vendor, closed source, compiled with
> VC++), in OSX I have plug.a
> - my_main.c (the code for the plugin)
> - ocaml_stub.c (this code calls 'caml_startup()')
> - ocaml_code.ml
> 
> in OSX I compile my_main.c, ocaml_stub.c and ocaml_code.ml to obtain '.o'
> files. Then I link the .o files with plug.a and libasmrun.a. This works
> fine.
> 
> In windows I have wodi32, visual studio 2008 and Flexdll. I tried the
> following.
> - Using cl compile, ocaml_stub.c, my_main.c to obtain '.obj' files
> - Using ocamlopt, compile ocaml_code.ml to obtain a '.o' file
> - Using flexlink to link the .obj, .o, libasmrun.a, plug.lib, libgcc.a and
> libc.a
> 
> This gives me an error:
> 
> ** Cannot resolve symbols for libasmrun.a(floats.o):
>  ___strtod
> 
> I have tried to create my own strtod function and link it does not pick it.
> 
> Anybody knows how can I link my program.

Can you provide the commands? In particular the flexlink one?

Also you probably shouldn't call flexlink directly; using ocamlc should
do it.

-- 
Adrien Nader

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Embedding Ocaml in a windows application
  2014-05-09 20:20     ` Adrien Nader
@ 2014-05-09 20:27       ` Leonardo Laguna
  2014-05-09 20:40         ` David Allsopp
  2014-05-09 21:54         ` Adrien Nader
  0 siblings, 2 replies; 8+ messages in thread
From: Leonardo Laguna @ 2014-05-09 20:27 UTC (permalink / raw)
  To: Adrien Nader; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1853 bytes --]

This is what I'm executing:

ocamlopt -output-obj -o ocamllibrary.o library.ml
cl /c my_plugin.c /I"C:\\wodi32\\opt\\wodi32\\lib\\ocaml\\std-lib"
cl /c ocaml_stub.c /I"C:\\wodi32\\opt\\wodi32\\lib\\ocaml\\std-lib"
flexlink -chain msvc -exe -o my_plugin.exe my_plugin.obj ocaml_stub.obj
ocamllibrary.o plug.lib -lasmrun -lgcc -L
"C:\wodi32\lib\gcc\i686-w64-mingw32\4.8.2"

Leonardo



On Fri, May 9, 2014 at 10:20 PM, Adrien Nader <adrien@notk.org> wrote:

> On Fri, May 09, 2014, Leonardo Laguna wrote:
> > I have given up on compiling the msvc port. I don't know why I'm getting
> > errors when the '.exe' is not there.
> >
> > I will try to explain better what I want to do.
> >
> > I have the following files:
> >
> > - plug.lib (provided by the software vendor, closed source, compiled with
> > VC++), in OSX I have plug.a
> > - my_main.c (the code for the plugin)
> > - ocaml_stub.c (this code calls 'caml_startup()')
> > - ocaml_code.ml
> >
> > in OSX I compile my_main.c, ocaml_stub.c and ocaml_code.ml to obtain
> '.o'
> > files. Then I link the .o files with plug.a and libasmrun.a. This works
> > fine.
> >
> > In windows I have wodi32, visual studio 2008 and Flexdll. I tried the
> > following.
> > - Using cl compile, ocaml_stub.c, my_main.c to obtain '.obj' files
> > - Using ocamlopt, compile ocaml_code.ml to obtain a '.o' file
> > - Using flexlink to link the .obj, .o, libasmrun.a, plug.lib, libgcc.a
> and
> > libc.a
> >
> > This gives me an error:
> >
> > ** Cannot resolve symbols for libasmrun.a(floats.o):
> >  ___strtod
> >
> > I have tried to create my own strtod function and link it does not pick
> it.
> >
> > Anybody knows how can I link my program.
>
> Can you provide the commands? In particular the flexlink one?
>
> Also you probably shouldn't call flexlink directly; using ocamlc should
> do it.
>
> --
> Adrien Nader
>

[-- Attachment #2: Type: text/html, Size: 2838 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [Caml-list] Embedding Ocaml in a windows application
  2014-05-09 20:27       ` Leonardo Laguna
@ 2014-05-09 20:40         ` David Allsopp
  2014-05-09 21:54         ` Adrien Nader
  1 sibling, 0 replies; 8+ messages in thread
From: David Allsopp @ 2014-05-09 20:40 UTC (permalink / raw)
  To: caml-list

Leonardo Laguna wrote:
> This is what I'm executing:
> 
> ocamlopt -output-obj -o ocamllibrary.o library.ml

Unless you've a supremely advanced reason for doing so, use .obj not .o, but then...

> cl /c my_plugin.c /I"C:\\wodi32\\opt\\wodi32\\lib\\ocaml\\std-lib"
> cl /c ocaml_stub.c /I"C:\\wodi32\\opt\\wodi32\\lib\\ocaml\\std-lib"

Woah! C:\wodi32? From http://wodi.forge.ocamlcore.org:

"WODI is an extended Windows port of GODI... compiled with mingw-w64"

You need to use the MSVC port of OCaml for this to work and you'll need to build that from sources.

> flexlink -chain msvc -exe -o my_plugin.exe my_plugin.obj ocaml_stub.obj
> ocamllibrary.o plug.lib -lasmrun -lgcc -L "C:\wodi32\lib\gcc\i686-w64-mingw32\4.8.2"

-lasmrun and -lgcc have no place linking with MSVC. It's either libasmrun.lib (or asmrun.lib) that you specify and certainly no -lgcc. 


David


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Embedding Ocaml in a windows application
  2014-05-09 20:27       ` Leonardo Laguna
  2014-05-09 20:40         ` David Allsopp
@ 2014-05-09 21:54         ` Adrien Nader
  2014-05-09 22:00           ` Leonardo Laguna
  1 sibling, 1 reply; 8+ messages in thread
From: Adrien Nader @ 2014-05-09 21:54 UTC (permalink / raw)
  To: Leonardo Laguna; +Cc: caml-list

On Fri, May 09, 2014, Leonardo Laguna wrote:
> This is what I'm executing:
> 
> ocamlopt -output-obj -o ocamllibrary.o library.ml
> cl /c my_plugin.c /I"C:\\wodi32\\opt\\wodi32\\lib\\ocaml\\std-lib"
> cl /c ocaml_stub.c /I"C:\\wodi32\\opt\\wodi32\\lib\\ocaml\\std-lib"
> flexlink -chain msvc -exe -o my_plugin.exe my_plugin.obj ocaml_stub.obj
> ocamllibrary.o plug.lib -lasmrun -lgcc -L
> "C:\wodi32\lib\gcc\i686-w64-mingw32\4.8.2"

My take on it (as you can see, it's really mine) would be something
like (with an ocaml toolchain based on the mingw port):

ocamlopt -output-obj -o ocamllibrary.o library.ml
ocamlc -c my_plugin.c
ocamlc -c ocaml_stub.c
ocamlopt -o my_plugin.exe ocaml_stub.o my_plugin.o ocamllibrary.o plug.lib

The ideas behind this are:
- gcc and msvc create compatible object files even though they have a
  different extension
- static libraries are archives of objects and are also usable directly
- the ocaml compiler knows best how to call the C compiler for C files;
  calling the C compiler by hand is an endless source of pain (if you
  want more details, add -verbose to the ocaml* invocations)
- same for the linker

-- 
Adrien Nader

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Embedding Ocaml in a windows application
  2014-05-09 21:54         ` Adrien Nader
@ 2014-05-09 22:00           ` Leonardo Laguna
  0 siblings, 0 replies; 8+ messages in thread
From: Leonardo Laguna @ 2014-05-09 22:00 UTC (permalink / raw)
  To: Adrien Nader; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1590 bytes --]

Hi Adrien,

I have changed to other computer and I could compile the msvc port of
Ocaml. This time I didn't had any problem. I don't know what was wrong with
my other computer.

Now using ocamlopt (instead of flexlink) and the libasmrun.lib works fine.

Thank you very much for your help.

Leonardo


On Fri, May 9, 2014 at 11:54 PM, Adrien Nader <adrien@notk.org> wrote:

> On Fri, May 09, 2014, Leonardo Laguna wrote:
> > This is what I'm executing:
> >
> > ocamlopt -output-obj -o ocamllibrary.o library.ml
> > cl /c my_plugin.c /I"C:\\wodi32\\opt\\wodi32\\lib\\ocaml\\std-lib"
> > cl /c ocaml_stub.c /I"C:\\wodi32\\opt\\wodi32\\lib\\ocaml\\std-lib"
> > flexlink -chain msvc -exe -o my_plugin.exe my_plugin.obj ocaml_stub.obj
> > ocamllibrary.o plug.lib -lasmrun -lgcc -L
> > "C:\wodi32\lib\gcc\i686-w64-mingw32\4.8.2"
>
> My take on it (as you can see, it's really mine) would be something
> like (with an ocaml toolchain based on the mingw port):
>
> ocamlopt -output-obj -o ocamllibrary.o library.ml
> ocamlc -c my_plugin.c
> ocamlc -c ocaml_stub.c
> ocamlopt -o my_plugin.exe ocaml_stub.o my_plugin.o ocamllibrary.o plug.lib
>
> The ideas behind this are:
> - gcc and msvc create compatible object files even though they have a
>   different extension
> - static libraries are archives of objects and are also usable directly
> - the ocaml compiler knows best how to call the C compiler for C files;
>   calling the C compiler by hand is an endless source of pain (if you
>   want more details, add -verbose to the ocaml* invocations)
> - same for the linker
>
> --
> Adrien Nader
>

[-- Attachment #2: Type: text/html, Size: 2364 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-05-09 22:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-09  7:42 [Caml-list] Embedding Ocaml in a windows application Leonardo Laguna
2014-05-09 12:13 ` Adrien Nader
2014-05-09 20:16   ` Leonardo Laguna
2014-05-09 20:20     ` Adrien Nader
2014-05-09 20:27       ` Leonardo Laguna
2014-05-09 20:40         ` David Allsopp
2014-05-09 21:54         ` Adrien Nader
2014-05-09 22:00           ` Leonardo Laguna

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).