* Interface C and Caml
@ 2008-01-03 14:54 David LONY
2008-01-03 20:03 ` [Caml-list] " Alain Frisch
0 siblings, 1 reply; 3+ messages in thread
From: David LONY @ 2008-01-03 14:54 UTC (permalink / raw)
To: caml-list
Hi all,
I'm trying to interface Ocaml with C code. But unfortunately I don't
know why it doesn't work well...
This is my caml code (truc.ml) :
let test_char a = a+1
let _ = Callback.register "caml_test_char" test_char;;
and this is my C code (test.c) :
#include <stdio.h>
#include <caml/mlvalues.h>
#include <caml/callback.h>
int test_char(int n)
{
static value * test_char_closure = NULL;
if (test_char_closure == NULL)
{
test_char_closure = caml_named_value("caml_test_char");
}
return Int_val(caml_callback(*test_char_closure, Val_int(n)));
}
int main(int argc, char ** argv)
{
int result;
caml_startup(argv);
result = test_char(10);
printf("%d\n", result);
return 0;
}
I obtain the final executable by typing this command :
ocamlopt -cc "gcc" -o test truc.ml test.c
But if I try to do the same thing by typing :
ocamlopt -output-obj truc.ml -o truc.o
gcc -c test.c
gcc -o test test.o truc.o -L/usr/lib/ocaml/3.09.2 -lasmrun
I obtain :
truc.o: In function `caml_program':
(.text+0xd): undefined reference to `camlTruc__entry'
truc.o: In function `caml_globals':
(.data+0x1b4): undefined reference to `camlTruc'
truc.o: In function `caml_data_segments':
(.data+0x21c): undefined reference to `camlTruc__data_begin'
truc.o: In function `caml_data_segments':
(.data+0x220): undefined reference to `camlTruc__data_end'
truc.o: In function `caml_code_segments':
----BLABLA
/usr/lib/ocaml/3.09.2/libasmrun.a(floats.o): In function `caml_atan_float':
----BLABLA
/usr/lib/ocaml/3.09.2/libasmrun.a(floats.o): In function `caml_power_float':
(.text+0x380): undefined reference to `pow'
/usr/lib/ocaml/3.09.2/libasmrun.a(floats.o): In function `caml_sqrt_float':
(.text+0x3b1): undefined reference to `sqrt'
/usr/lib/ocaml/3.09.2/libasmrun.a(floats.o): In function `caml_log10_float':
(.text+0x492): undefined reference to `log10'
/usr/lib/ocaml/3.09.2/libasmrun.a(floats.o): In function `caml_log_float':
/usr/lib/ocaml/3.09.2/libasmrun.a(unix.o): In function `caml_dlclose':
----BLABLA
What is wrong ? Does anyone has the same problem ?
Regards
David LONY
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Interface C and Caml
2008-01-03 14:54 Interface C and Caml David LONY
@ 2008-01-03 20:03 ` Alain Frisch
2008-01-04 9:24 ` David LONY
0 siblings, 1 reply; 3+ messages in thread
From: Alain Frisch @ 2008-01-03 20:03 UTC (permalink / raw)
To: David LONY; +Cc: caml-list
David LONY wrote:
> ocamlopt -output-obj truc.ml -o truc.o
> gcc -c test.c
> gcc -o test test.o truc.o -L/usr/lib/ocaml/3.09.2 -lasmrun
Two problems here:
1. the first line first compiles truc.ml into truc.cmx/truc.o as usual,
and then combine truc.o with stdlib.a and a temporary startup object
into truc.o. It seems that the linker first creates a fresh truc.o file
before reading the existing truc.o, so the native code produced by the
compiler is actually lost. You should use a different file name:
ocamlopt -output-obj truc.ml -o truc_main.o
2. the last line should mention the -lm and -ldl libraries, as can be
found by:
grep NATIVECCLIBS `ocamlc -where`/Makefile.config
So:
gcc -o test test.o truc_main.o -L/usr/lib/ocaml/3.09.2 -lasmrun -lm -ldl
-- Alain
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Interface C and Caml
2008-01-03 20:03 ` [Caml-list] " Alain Frisch
@ 2008-01-04 9:24 ` David LONY
0 siblings, 0 replies; 3+ messages in thread
From: David LONY @ 2008-01-04 9:24 UTC (permalink / raw)
To: Alain Frisch; +Cc: caml-list
It work well!!
Thanks a lot Alain
David
Alain Frisch a écrit :
> David LONY wrote:
>> ocamlopt -output-obj truc.ml -o truc.o
>> gcc -c test.c
>> gcc -o test test.o truc.o -L/usr/lib/ocaml/3.09.2 -lasmrun
>
> Two problems here:
>
> 1. the first line first compiles truc.ml into truc.cmx/truc.o as
> usual, and then combine truc.o with stdlib.a and a temporary startup
> object into truc.o. It seems that the linker first creates a fresh
> truc.o file before reading the existing truc.o, so the native code
> produced by the compiler is actually lost. You should use a different
> file name:
>
> ocamlopt -output-obj truc.ml -o truc_main.o
>
>
> 2. the last line should mention the -lm and -ldl libraries, as can be
> found by:
>
> grep NATIVECCLIBS `ocamlc -where`/Makefile.config
>
> So:
>
> gcc -o test test.o truc_main.o -L/usr/lib/ocaml/3.09.2 -lasmrun -lm -ldl
>
> -- Alain
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-01-04 9:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-03 14:54 Interface C and Caml David LONY
2008-01-03 20:03 ` [Caml-list] " Alain Frisch
2008-01-04 9:24 ` David LONY
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox