Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Victor Pereira <vitorm2p@gmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] Interop C-OCaml
Date: Mon, 3 Oct 2016 16:32:31 +0100	[thread overview]
Message-ID: <DF066DA2-459B-4F24-8BC6-3FD7F68E8C1B@gmail.com> (raw)

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

Hello all!

I have some function written in OCaml that I need to call in a C program.

The OCaml function takes a string, performs some computation and also 
outputs a string. Note that these types are not mandatory, just for testing purposes. 

Therefore, I defined a C function like:

char * foo (char * inp) {
  
  CAMLlocal1 (caml_inp) ;
  caml_inp = caml_copy_string (inp);
  
  value * func = caml_named_value (“foo") ;
  
  if (func == NULL)
    puts (“Unable to load OCaml function!") ;

  return strdup(String_val(caml_callback(*func, oinp)));
}

I am able to correctly compile the file with:

ocamlc -c foo.c (according to the manual)

Next, I define the main.c file as:

extern char * foo(char * inp);

int main (int argc, char ** argv)
{

  /* Initialize OCaml code */
  caml_startup(argv);

  /* Do some computation */
  char * inp = “some string";

  printf ("%s\n", foo(inp));
  
  return 0;
}

However, I’m not able to compile the main.c file!
The error output is the following: 

Undefined symbols for architecture x86_64:
  "_caml_startup", referenced from:
      _main in main-05d208.o
  “_foo", referenced from:
      _main in main-05d208.o

I followed the instructions in the manual to interop C and OCaml when
the main program is in the C side.

Nevertheless, If I run the example in the manual, it works very well!

Any idea on what am I doing wrong?

Best,
Vitor



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

                 reply	other threads:[~2016-10-03 15:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DF066DA2-459B-4F24-8BC6-3FD7F68E8C1B@gmail.com \
    --to=vitorm2p@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox