# Compiling C++ file for usage in Julia

**URL:** <https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693>\
**Category:** General Usage\
**Created:** [October 24, 2024, 11:51am UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693 "2024-10-24T11:51:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Questioner](https://avatars.discourse-cdn.com/v4/letter/q/e8c25b/32.png) [@Questioner](https://discourse.julialang.org/u/Questioner)\
**Post date:** [October 24, 2024, 11:51am UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693/1 "2024-10-24T11:51:23Z")

</div>

I have been trying to replicate the example given in a different post, to which I can’t link, but here’s the relative URL: /t/can-someone-provide-a-simple-example-of-using-cxxwrap/115908/6

I’m on Windows (because there doesn’t seem to be a Debian package), and I’ve installed the MinGW compiler, which has successfully compiled a simple “Hello World” program.

Unfortunately, upon running

```julia
g++ -shared -fPIC -o greetings.so -I"C:\Users\<My name>\.julia\artifacts\6907059f4600a8fd77020c49e83de7dbbea3a28c\include" -I"C:\users\<MyName>\AppData\local\programs\julia-1.10.5\include\julia" greetings.cpp

```

I get a multitude of error messages ascertaining the lack of certain references (of the type `undefined reference to `__imp_jl_compute_fieldtypes'` or similar).

The file that I’m trying to compile looks like this:

```julia
#include <string>
#include "jlcxx/jlcxx.hpp"

std::string greet() {
	return "Hello World!";
}

JLCXX_MODULE define_julia_module(jlcxx::Module& mod) {
	mod.method("greet", &greet);
}

```

I’d be grateful for some hints as to what may be going wrong here.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 24, 2024, 1:27pm UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693/2 "2024-10-24T13:27:51Z")

</div>

> [@Questioner](#):
>
> because there doesn’t seem to be a Debian package

Debian package of what?

---

<div class="post-metadata">

**Author:** ![Questioner](https://avatars.discourse-cdn.com/v4/letter/q/e8c25b/32.png) [@Questioner](https://discourse.julialang.org/u/Questioner)\
**Post date:** [October 24, 2024, 3:43pm UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693/3 "2024-10-24T15:43:05Z")

</div>

Of Julia.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 24, 2024, 3:54pm UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693/4 "2024-10-24T15:54:54Z")

</div>

There is no need for a Debian package of Julia. On Linux, just install `juliaup` using

```julia
curl -fsSL https://install.julialang.org | sh

```

and then install the desired Julia version with

```julia
juliaup install 1.10
juliaup default 1.10

```

or whatever version you want. And you can update it with

```julia
juliaup update

```

---

<div class="post-metadata">

**Author:** ![Questioner](https://avatars.discourse-cdn.com/v4/letter/q/e8c25b/32.png) [@Questioner](https://discourse.julialang.org/u/Questioner)\
**Post date:** [October 24, 2024, 3:57pm UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693/5 "2024-10-24T15:57:31Z")

</div>

Thanks, I (vaguely) knew of that, but on Windows, I got the Chocolatey Julia package, which is being kept up to date automatically, whence I’m working under Windows.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 24, 2024, 3:59pm UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693/6 "2024-10-24T15:59:51Z")

</div>

> [@Questioner](#):
>
> whence I’m working under Windows.

A very bad reason to use Windows. I at least often need different versions of Julia, and with `juliaup` you can install any number of versions in parallel and switch easily per project. And programming on Linux is so much faster and more convenient, in particular if it comes to C++. But also Julia is much faster on Linux, building system images works in parallel on Linux (not on Windows), and operations on many small files are also much faster on Linux.

---

<div class="post-metadata">

**Author:** ![Questioner](https://avatars.discourse-cdn.com/v4/letter/q/e8c25b/32.png) [@Questioner](https://discourse.julialang.org/u/Questioner)\
**Post date:** [October 24, 2024, 4:00pm UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693/7 "2024-10-24T16:00:32Z")

</div>

I do use Linux for C++ programming, unless it be for inclusion into Julia.

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [October 24, 2024, 7:48pm UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693/8 "2024-10-24T19:48:36Z")

</div>

Hi, and welcome to the Julia community!

I can confirm that your code and the one from the [post you referred to](https://discourse.julialang.org/t/can-someone-provide-a-simple-example-of-using-cxxwrap/115908/6) also does not work for me on Windows. The CMake example from the CxxWrap.jl [github page](https://github.com/JuliaInterop/libcxxwrap-julia/tree/main/testlib-builder/src/testlib) did work without any issues though.

I’m certainly no expert on these matters, but it seems on Windows you need to explicitly link to the binaries:

```bash
g++ -shared -fPIC -o greetings.so -I (...)\.julia\artifacts\6907059f4600a8fd77020c49e83de7dbbea3a28c\include -I (...)\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\include\julia -L (...)\.julia\artifacts\6907059f4600a8fd77020c49e83de7dbbea3a28c\bin -L (...)\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\bin .\greetings.cpp -lcxxwrap_julia -ljulia

```

```julia-repl
julia> using CxxWrap

julia> @wrapmodule(() -> "greetings.so") # Assuming you started Julia in the same folder

julia> greet()
"Hello World!"

```

works for me.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 24, 2024, 8:10pm UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693/9 "2024-10-24T20:10:02Z")

</div>

If you want to call Julia from C++ then consider: [GitHub - Clemapfel/jluna: Julia Wrapper for C++ with Focus on Safety, Elegance, and Ease of Use](https://github.com/Clemapfel/jluna)

It also supports Windows (and I assume also 1.11, it’s at least tested up to 1.10).

> [@eldee](#):
>
> I can confirm that your code and the one from the [post you referred to](https://discourse.julialang.org/t/can-someone-provide-a-simple-example-of-using-cxxwrap/115908/6) also does not work for me on Windows.

> [@eldee](#):
>
> I can confirm that your code and the one from the [post you referred to](https://discourse.julialang.org/t/can-someone-provide-a-simple-example-of-using-cxxwrap/115908/6) also does not work for me on Windows. The CMake example from the [CxxWrap.jl](https://juliahub.com/ui/Packages/General/CxxWrap) [github page](https://github.com/JuliaInterop/libcxxwrap-julia/tree/main/testlib-builder/src/testlib) did work without any issues though.

I’m on Linux, not Windows, so I can’t test, but assumed calling from Julia to C++ also worked with CppWrap (at least no less than from Linux, major upgrades have sometimes broken CxxWrap, I think so far always fixed, but haven’t looked into if .11 is an issue).

---

<div class="post-metadata">

**Author:** ![Questioner](https://avatars.discourse-cdn.com/v4/letter/q/e8c25b/32.png) [@Questioner](https://discourse.julialang.org/u/Questioner)\
**Post date:** [October 30, 2024, 2:00pm UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693/10 "2024-10-30T14:00:46Z")

</div>

Thanks a lot, the only problem is that on my PC, the folders (such as the juliaup folder) you are referencing simply do not exist.

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [October 30, 2024, 4:06pm UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693/11 "2024-10-30T16:06:47Z")

</div>

Well, you should use the folder where Julia is installed on your machine. Similar to in @sylvaticus 's post, you can obtain this via `Sys.BINDIR` and a modification where you replace `bin` by `include\julia`.

Same for the CxxWrap.jl directory, via

```julia
using CxxWrap
CxxWrap.prefix_path()

```

with `\bin` and `\include` appended (e.g. via `joinpath`).

---

<div class="post-metadata">

**Author:** ![Questioner](https://avatars.discourse-cdn.com/v4/letter/q/e8c25b/32.png) [@Questioner](https://discourse.julialang.org/u/Questioner)\
**Post date:** [October 31, 2024, 3:57pm UTC](https://discourse.julialang.org/t/compiling-c-file-for-usage-in-julia/121693/12 "2024-10-31T15:57:34Z")

</div>

Thank you so much, it works exactly like on your PC.
