# Add my own lib

**URL:** <https://community.haxe.org/t/add-my-own-lib/2217>\
**Category:** Haxe\
**Tags:** haxe-cpp, haxelib, haxe-java\
**Created:** [January 16, 2020, 2:09pm UTC](https://community.haxe.org/t/add-my-own-lib/2217 "2020-01-16T14:09:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bifi](https://avatars.discourse-cdn.com/v2/letter/b/ee7513/32.png) [@Bifi](https://community.haxe.org/u/Bifi)\
**Post date:** [January 16, 2020, 2:09pm UTC](https://community.haxe.org/t/add-my-own-lib/2217/1 "2020-01-16T14:09:27Z")

</div>

Hello, community,

maybe a rather beginner’s question, but is there a possibility to attach your own libs in Haxe?

example: I have written a calculator in C++ and in Java and don’t want to rewrite it completely in Haxe. But I want to use the function “Add(x,y)” in my Haxe code.

Is it possible to include the calculator as a library depending on the compiler.

Unfortunately I have not found any beginner-friendly guides for this.

Greetings

---

<div class="post-metadata">

**Author:** ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)\
**Post date:** [January 16, 2020, 2:14pm UTC](https://community.haxe.org/t/add-my-own-lib/2217/2 "2020-01-16T14:14:36Z")

</div>

For Java, you can just include a `.jar` in compilation via `--java-lib`. For C++… it might be quicker to rewrite it in Haxe rather than trying to figure out how C++ externs work, depending on how much code we’re talking about and how familiar you are with C++. 😃 Plus, it would work on targets other than Java and C++ then.

---

<div class="post-metadata">

**Author:** ![Bifi](https://avatars.discourse-cdn.com/v2/letter/b/ee7513/32.png) [@Bifi](https://community.haxe.org/u/Bifi)\
**Post date:** [January 16, 2020, 2:39pm UTC](https://community.haxe.org/t/add-my-own-lib/2217/3 "2020-01-16T14:39:31Z")

</div>

First of all, thank you very much,

for my work I will probably have to find out how it works with the C++ libs …

Does anyone have any helpful links to get started ?

---

<div class="post-metadata">

**Author:** ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)\
**Post date:** [January 16, 2020, 3:18pm UTC](https://community.haxe.org/t/add-my-own-lib/2217/4 "2020-01-16T15:18:58Z")

</div>

For C++ you can check `haxe.Int64` as an example of interoperation:

> <https://github.com/HaxeFoundation/haxe/blob/8dee14b/std/cpp/_std/haxe/Int64.hx#L28>

AFAIK `@:include` generates respective instruction in the compiled c++ code.  
`@:native` specifies for the Haxe compiler which name to generate for a symbol instead of its haxe name.

---

<div class="post-metadata">

**Author:** ![Bifi](https://avatars.discourse-cdn.com/v2/letter/b/ee7513/32.png) [@Bifi](https://community.haxe.org/u/Bifi)\
**Post date:** [January 17, 2020, 11:16am UTC](https://community.haxe.org/t/add-my-own-lib/2217/5 "2020-01-17T11:16:41Z")

</div>

Thank you for the example that has explained a lot.

I have another question about the directory structure:

The Int64.h has been added by installing hxcpp via haxelib and can be found at :

> …HaxeToolkit\haxe\lib\hxcpp"version"\include\cpp\Int64.h

Is then used in the code by

> [@RealyUniqueName](#):
>
> @:include(“cpp/Int64.h”)

called.

Where exactly would I store my Addieren.h to call it up in a Main.hx?  
In :

> haxe\lib\hxcpp"version"\include\calculator\Add.h

---

<div class="post-metadata">

**Author:** ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)\
**Post date:** [January 17, 2020, 3:13pm UTC](https://community.haxe.org/t/add-my-own-lib/2217/6 "2020-01-17T15:13:03Z")

</div>

I guess you need to add the directory with your lib to include paths for hxcpp build tool somehow.  
Maybe you can find a hint here: [hxcpp/linux-toolchain.xml at master · HaxeFoundation/hxcpp · GitHub](https://github.com/HaxeFoundation/hxcpp/blob/master/toolchain/linux-toolchain.xml#L56)

The easiest way probably would be to just put your lib into `/path/to/hxcpp/include/`, but that seems to be a dirty workaround 🙂

---

<div class="post-metadata">

**Author:** ![Bifi](https://avatars.discourse-cdn.com/v2/letter/b/ee7513/32.png) [@Bifi](https://community.haxe.org/u/Bifi)\
**Post date:** [January 17, 2020, 3:20pm UTC](https://community.haxe.org/t/add-my-own-lib/2217/7 "2020-01-17T15:20:53Z")

</div>

Hello, thank you, I will definitely take a look at that.

After some searching I found this:  
[http://old.haxe.org/doc/cpp/ffi](http://old.haxe.org/doc/cpp/ffi)

Not exactly what I was looking for but it’s a start. I will take a closer look at how promising this approach is.  
Greetings

---

<div class="post-metadata">

**Author:** ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)\
**Post date:** [January 17, 2020, 3:53pm UTC](https://community.haxe.org/t/add-my-own-lib/2217/8 "2020-01-17T15:53:05Z")

</div>

AFAIK it’s an outdated approach.
