How to call the GMP library?

I am learning to program in Zig and would like to use the GMP library for arbitrary precision numbers. I have the GMP library installed on my computer, and when I need to compile a program in C, I use the command:

gcc -o test test.c -I/opt/homebrew/Cellar/gmp/6.3.0/include/ -L/opt/homebrew/Cellar/gmp/6.3.0/lib/ -lgmp

I tried including the commands in main.zig:

const gmp = @cImport({
@cInclude(“gmp.h”);
});

or

const gmp = @cImport({
@cInclude(“/opt/homebrew/Cellar/gmp/6.3.0/lib/gmp.h”);
});

but the program did not compile.
What changes do I need to make to build.zig for the program to compile? Could someone give a working example of some Zig code that uses the GMP library?

I assume you meant you added the include flags to the zig command/build.zig.

why didn’t it compile? It’s very hard to help if you don’t provide any detail.

I can only guess you did not link libc or gmp or both.

1 Like

As I said, I am learning to program in Zig.
I would like to use the GMP library because I want to create some math programs (for example, calculate the number PI with an arbitrary number of digits or generate a list of prime numbers). This would serve as an incentive to learn a new language, but perhaps I should worry about learning the basics first.
The test program didn’t work because obviously it’s not enough to include @cImport({@cInclude(“gmp.h”);}) in main.zig. I tried modifying build.zig following some tutorials (A Simple Example of Calling a C Library from Zig · mtlynch.io, https://www.youtube.com/watch?v=HGlTuDmotnU&t=615s), but I was unsuccessful.

I haven’t done anything yet. I’m trying to call the GMP library first. There’s no point in starting to code anything if I can’t even call the library. My question is about how to call the GMP library, and I am asking for a basic, functional example.

Thank you.

Again, you are providing almost no useful information.

What is the error you get?
What did you try?
What is your build.zig?

I can only guess what the problem is, but the solution will probably look something like (untested code)

mod.linkSystemLibrary("gmp");
mod.linkLibC();

more or less might be needed.