# Can't link complete object file in a C program

**URL:** <https://discuss.ocaml.org/t/cant-link-complete-object-file-in-a-c-program/2337>\
**Category:** Learning\
**Tags:** linking, dune\
**Created:** [July 25, 2018, 2:51pm UTC](https://discuss.ocaml.org/t/cant-link-complete-object-file-in-a-c-program/2337 "2018-07-25T14:51:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rauan](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rauan/32/619_2.png) [@rauan](https://discuss.ocaml.org/u/rauan)\
**Post date:** [July 25, 2018, 2:51pm UTC](https://discuss.ocaml.org/t/cant-link-complete-object-file-in-a-c-program/2337/1 "2018-07-25T14:51:53Z")

</div>

When trying to link a complete object file with a C program, this is what I get for `ocaml 4.06`:

```auto
dune build app.exe.o
clang -o main -I /Users/rauan/.opam/workspace/lib/ocaml main.c _build/default/app.exe.o
Undefined symbols for architecture x86_64:
  "_caml_code_area_end", referenced from:
      _segv_handler in app.exe.o
  "_caml_code_area_start", referenced from:
      _segv_handler in app.exe.o
  "_caml_main", referenced from:
      _main in main-7906c3.o
     (maybe you meant: _caml_main_argv)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1

```

Upgrading ocaml to `4.07` reduces the errors amount down to only `_caml_code_area_end` being undefined. (I’m guessing that [GPR#1740](https://github.com/ocaml/ocaml/pull/1740) helped with that)

I made a sample repo to reproduce the issue: [https://github.com/rauanmayemir/linking-example](https://github.com/rauanmayemir/linking-example)

The reason it’s organized that way is because I want to keep my bindings and ocaml logic separate from the main program. (I then intend to have e.g an XCode project target with platform-specific assets and call out my ocaml logic from there)

It happens when I try to link an object file built in dune’s `object` linking mode (here’s the [build log](https://gist.github.com/rauanmayemir/b23e3178f577997ba674f2858335ba11)).

Note: I can successfully execute object file built in `exe` linking mode (without linking it to main.c and preliminarily dropping the callback register and running the code directly instead).

Please help me understand whether I’m doing something wrong or there’s a bug in `ocaml`.

---

<div class="post-metadata">

**Author:** ![rauan](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rauan/32/619_2.png) [@rauan](https://discuss.ocaml.org/u/rauan)\
**Post date:** [August 2, 2018, 5:44pm UTC](https://discuss.ocaml.org/t/cant-link-complete-object-file-in-a-c-program/2337/2 "2018-08-02T17:44:27Z")

</div>

```auto
NSView __block *view = nil;

```

This piece of code in my C stubs was the reason linker failed with undefined `_caml_code_area_end`. (an artifact left from the imprudent decision to click Xcode’s ‘Fix’ suggestion)

Once I removed `__block`, the issue disappeared.

Normally, there’s no issue with dune’s `-output-complete-obj` facilities.
