How to use GMT API

Dear GMT friends,

I try to Compile the api C example code:

#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:

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

But given error:

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?

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.

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).

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

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?

According to last post in this SO thread, 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.