# Passing GMP variables from C(++) to Julia

**URL:** https://discourse.julialang.org/t/passing-gmp-variables-from-c-to-julia/123167
**Category:** General Usage
**Tags:** question
**Created:** [November 27, 2024, 1:21pm UTC](https://discourse.julialang.org/t/passing-gmp-variables-from-c-to-julia/123167 "2024-11-27T13:21:39Z")
**Posts on this page:** 10
**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: [November 27, 2024, 1:21pm UTC](https://discourse.julialang.org/t/passing-gmp-variables-from-c-to-julia/123167/1 "2024-11-27T13:21:39Z")

</div>

Hello there,

let me start by thanking the entirely altruistic help; in the long run, I hope to contribute to Julia myself.

Unfortunately, I’ve run into another issue: I’d like to convert a GNU MP type (such as mpz\_t) into a Julia type (such as BigInteger), say when running a C code.

That is to say, I’d have an external (compiled) C program (or C++ program), which I’d like to call from Julia. I’d like this external program to pass a data type such as mpz\_t to Julia, so that it is transformed into a BigInteger or the like.

Does anyone know whether this is possible? I’d be grateful for any help in this matter.

---

<div class="post-metadata">

### Author: ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)
#### Post date: [November 27, 2024, 2:17pm UTC](https://discourse.julialang.org/t/passing-gmp-variables-from-c-to-julia/123167/2 "2024-11-27T14:17:01Z")

</div>

I have some passing knowledge of the internals of Julia’s `BigInt` type; hence, I may be wrong. Julia wraps GMP, as seen in `julia/base/gmp.jl`, so interfacing with C/C++ code that uses GMP should work just fine. A BigInt stores a pointer to limbs that you would pass down to the C API, and it is also possible to initialise a BigInt from a C pointer, which a garbage collector can then track.

The biggest hurdle would be compiling the C/C++ code to use the same GMP library that Julia uses. If you manage to do that, the interfacing will work just fine. I have had a good experience pasting the whole `julia/base/gmp.jl` into Claude to write low-level methods, so I highly recommend trying that for your use case.

---

<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: [November 27, 2024, 5:18pm UTC](https://discourse.julialang.org/t/passing-gmp-variables-from-c-to-julia/123167/3 "2024-11-27T17:18:49Z")

</div>

Thank you very much for your help, but what is “Claude”, apart from an impressionistic composer?

---

<div class="post-metadata">

### Author: ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)
#### Post date: [November 27, 2024, 6:01pm UTC](https://discourse.julialang.org/t/passing-gmp-variables-from-c-to-julia/123167/4 "2024-11-27T18:01:03Z")

</div>

With Claude I meant large language model [claude.ai](https://Claude)

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [November 27, 2024, 7:57pm UTC](https://discourse.julialang.org/t/passing-gmp-variables-from-c-to-julia/123167/5 "2024-11-27T19:57:44Z")

</div>

If some overhead is acceptable, a quick and dirty solution is passing strings between C++ and Julia. The strings can be parsed back to BigInt.

---

<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: [December 4, 2024, 3:02pm UTC](https://discourse.julialang.org/t/passing-gmp-variables-from-c-to-julia/123167/6 "2024-12-04T15:02:30Z")

</div>

OK, I now managed to compile the code on Windows with the most recent GMP version; I know I was supposed to take the same version that Julia uses, but I do need to get it to work with a version discrepancy, because otherwise any new Julia version may destroy my code.

I have not yet managed to pass the data type, but I’m not sure whether thats due to the GMP versions being different or my insufficient understanding of passing types in C. For instance, what sort of pointer should I return from my C function? This last question seems to me of crucial importance.

---

<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: [December 4, 2024, 3:05pm UTC](https://discourse.julialang.org/t/passing-gmp-variables-from-c-to-julia/123167/7 "2024-12-04T15:05:16Z")

</div>

I will now be trying to pass a BigInt from Julia to C by reference and have C modify it, because other methods have so far not produced any success.

---

<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: [December 4, 2024, 3:13pm UTC](https://discourse.julialang.org/t/passing-gmp-variables-from-c-to-julia/123167/8 "2024-12-04T15:13:48Z")

</div>

That did not work, I’ve tried this C file

```julia
#include<gmp.h>

void setToTwo(mpz_t testInt) {
	mpz_init_set_ui(testInt, 2);
}

```

with this ccall

```julia
 @ccall "./gmptest.so".setToTwo(x::BigInt)::Cvoid

```

and the variable that I had passed and that I had initially set to 3 remained 3.

---

<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: [December 4, 2024, 3:16pm UTC](https://discourse.julialang.org/t/passing-gmp-variables-from-c-to-julia/123167/9 "2024-12-04T15:16:53Z")

</div>

I did manage to get the BigInt into C though and then modify it there and return it as an int.

---

<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: [December 4, 2024, 3:38pm UTC](https://discourse.julialang.org/t/passing-gmp-variables-from-c-to-julia/123167/10 "2024-12-04T15:38:07Z")

</div>

I’ve now managed to get it to work. The key was using pointers to pass by reference, as described here: [Ccall: how to wrap call-by-reference C functions? - #2 by giordano](https://discourse.julialang.org/t/ccall-how-to-wrap-call-by-reference-c-functions/53510/2)

C code:

```julia
#include<gmp.h>

void setToTwo(mpz_t* testInt) {
	mpz_init_set_ui(*testInt, 2);
}

```

Julia code:

```julia
x = Ref{BigInt}(3)
@ccall "./gmptest.so".setToTwo(x::Ref{BigInt})::Cvoid

```

In this way, I managed to change x to 2.
