# How to use GMT API

**URL:** <https://forum.generic-mapping-tools.org/t/how-to-use-gmt-api/219>\
**Category:** Q&A\
**Tags:** gmt\
**Created:** [January 28, 2020, 11:29am UTC](https://forum.generic-mapping-tools.org/t/how-to-use-gmt-api/219 "2020-01-28T11:29:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yangleir](https://avatars.discourse-cdn.com/v4/letter/y/a8b319/32.png) [@yangleir](https://forum.generic-mapping-tools.org/u/yangleir)\
**Post date:** [January 28, 2020, 11:29am UTC](https://forum.generic-mapping-tools.org/t/how-to-use-gmt-api/219/1 "2020-01-28T11:29:42Z")

</div>

Dear GMT friends,

I try to Compile the api C example code:

```auto
#include "gmt.h"
int main (int argc, char *argv[]) {
    void *API; /* The API control structure */
    struct GMT_DATASET *D = NULL; /* Structure to hold input dataset */
    struct GMT_GRID *G = NULL; /* Structure to hold output grid */
    char input[GMT_STR16] = {""}; /* String to hold virtual input filename */
    char output[GMT_STR16] = {""}; /* String to hold virtual output filename */
    char args[128] = {""}; /* String to hold module command arguments */

    /* Initialize the GMT session */
    API = GMT_Create_Session ("test", 2U, 0, NULL);
    /* Read in our data table to memory */
    D = GMT_Read_Data (API, GMT_IS_DATASET, GMT_IS_FILE, GMT_IS_PLP, GMT_READ_NORMAL, NULL,
        "@table_5.11", NULL);
    /* Associate our data table with a virtual file */
    GMT_Open_VirtualFile (API, GMT_IS_DATASET, GMT_IS_PLP, GMT_IN, D, input);
    /* Create a virtual file to hold the resulting grid */
    GMT_Open_VirtualFile (API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_OUT, NULL, output);
    /* Prepare the module arguments */
    sprintf (args, "-R0/7/0/7 -I0.2 -D1 -St0.3 %s -G%s", input, output);
    /* Call the greenspline module */
    GMT_Call_Module (API, "greenspline", GMT_MODULE_CMD, args);
    /* Obtain the grid from the virtual file */
    G = GMT_Read_VirtualFile (API, output);
    /* Close the virtual files */
    GMT_Close_VirtualFile (API, input);
    GMT_Close_VirtualFile (API, output);
    /* Write the grid to file */
    GMT_Write_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_READ_NORMAL, NULL,
        "junk.nc", G);
    /* Destroy the GMT session */
    GMT_Destroy_Session (API);
};

```

Then set:

```auto
inc=`gmt-config --cflags`
lib=`gmt-config --libs`
gcc example1.c $inc $lib -o example1 

```

But given error:

```auto
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\yangleir\AppData\Local\Temp\ccJ1yhUf.o:main.c:(.text+0xba): undefined reference to `_imp__GMT_Create_Session'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\yangleir\AppData\Local\Temp\ccJ1yhUf.o:main.c:(.text+0x10a): undefined reference to `_imp__GMT_Read_Data'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\yangleir\AppData\Local\Temp\ccJ1yhUf.o:main.c:(.text+0x150): undefined reference to `_imp__GMT_Open_VirtualFile'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\yangleir\AppData\Local\Temp\ccJ1yhUf.o:main.c:(.text+0x18c): undefined reference to `_imp__GMT_Open_VirtualFile'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\yangleir\AppData\Local\Temp\ccJ1yhUf.o:main.c:(.text+0x1df): undefined reference to `_imp__GMT_Call_Module'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\yangleir\AppData\Local\Temp\ccJ1yhUf.o:main.c:(.text+0x1fb): undefined reference to `_imp__GMT_Read_VirtualFile'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\yangleir\AppData\Local\Temp\ccJ1yhUf.o:main.c:(.text+0x21e): undefined reference to `_imp__GMT_Close_VirtualFile'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\yangleir\AppData\Local\Temp\ccJ1yhUf.o:main.c:(.text+0x23a): undefined reference to `_imp__GMT_Close_VirtualFile'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\yangleir\AppData\Local\Temp\ccJ1yhUf.o:main.c:(.text+0x286): undefined reference to `_imp__GMT_Write_Data'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\yangleir\AppData\Local\Temp\ccJ1yhUf.o:main.c:(.text+0x297): undefined reference to `_imp__GMT_Destroy_Session'
collect2.exe: error: ld returned 1 exit status

```

The compile environment is MINGW64 gcc under win10.

Could someone give me some clues?

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [January 28, 2020, 2:30pm UTC](https://forum.generic-mapping-tools.org/t/how-to-use-gmt-api/219/2 "2020-01-28T14:30:37Z")

</div>

If you didn’t build GMT with Mingw’s gcc (don’t know if anyone has ever donne it) you are trying to link an import lib made with VisualStudio with .obj made by gcc. I know that it is supposed to exist interoperability between MingW and VS binaries but don’t know to what extent.

---

<div class="post-metadata">

**Author:** ![yangleir](https://avatars.discourse-cdn.com/v4/letter/y/a8b319/32.png) [@yangleir](https://forum.generic-mapping-tools.org/u/yangleir)\
**Post date:** [January 28, 2020, 3:02pm UTC](https://forum.generic-mapping-tools.org/t/how-to-use-gmt-api/219/3 "2020-01-28T15:02:04Z")

</div>

Thanks. You are right. I did not build gmt with Mingw. The GMT in my computer was installed from .exe. So `gmt-config --libs` will get `-L/c/programs/gmt6/lib -lgmt` (windows dir).

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [January 28, 2020, 3:41pm UTC](https://forum.generic-mapping-tools.org/t/how-to-use-gmt-api/219/4 "2020-01-28T15:41:35Z")

</div>

Yep, the issue is that `-lgmt` is unix syntax and it’s expecting to find a `libgmt.a` whilst what you have under `.../gmt6/lib` is a `gmt.lib`. You can try to rename the lib file but I have my doubts it works. You can also search internet on how to proceed in these cases.

I have also a distant recall that one could link against the `dll` directly. That is against `.../gmt6/bin/gmt_w64.dll`

---

<div class="post-metadata">

**Author:** ![yangleir](https://avatars.discourse-cdn.com/v4/letter/y/a8b319/32.png) [@yangleir](https://forum.generic-mapping-tools.org/u/yangleir)\
**Post date:** [January 29, 2020, 1:13am UTC](https://forum.generic-mapping-tools.org/t/how-to-use-gmt-api/219/5 "2020-01-29T01:13:30Z")

</div>

I tried both. “Rename gmt.lib” and “Link to gmt\_w64.dll” can not work. So dose that mean I must go to Linux or use VS to do this work?

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [January 29, 2020, 1:29am UTC](https://forum.generic-mapping-tools.org/t/how-to-use-gmt-api/219/6 "2020-01-29T01:29:56Z")

</div>

According to last post in [this SO thread](https://stackoverflow.com/questions/2472924/linking-to-msvc-dll-from-mingw), it should work.  
Don’t know what “can not work” means (aside that it didn’t work, but why?).  
You can also try to build GMT with MinGW but notice that our only experience on Windows is with VS.
