[Gmsh] Build the mesh in my own program

Mikhail Artemiev artemiev.mikhail at ngs.ru
Mon Oct 4 07:32:26 CEST 2010

Hi everybody once more!
Now I am ready to share my experience to use Gmsh 
functions in my own program.
Thank to Mikhail Titov's advice I found in utils/api_demos 
the examples of using Gmsh API.
Then I found in Gmsh reference manual this text:
<<Finally, to use Gmsh at the third level (i.e., to link 
the Gmsh library with your own code),
you will need to learn the internal Gmsh Application 
Programming Interface (API). No
complete documentation of this API is available yet; a 
good starting point is Section C.2
[Source code structure], page 185, which gives a short 
introduction to Gmsh's internal source
code structure. Then have a look e.g. at the examples in 
the 'utils/api_demos/' directory
in the source code. Due to the many possible configuration 
options (and associated external
dependencies), we currently do not distibute precompiled 
versions of the Gmsh library.
To build the library see the instructions in the top-level 
'README.txt' file in the source

Ok, cmake was downloaded and after some torture Gmsh.exe, 
Gmsh.dll and Gmsh.lib was created.
Then I included Gmsh.lib in my Visual Studio project 
(Properties->Linker->Input->Additional Dependencies) where 
I had only one cpp-file (mainSimple.cpp from 

#include <stdio.h>
#include <gmsh/Gmsh.h>
#include <gmsh/GModel.h>
#include <gmsh/MElement.h>

int main(int argc, char **argv)
   GmshInitialize(argc, argv);
   //GmshSetOption("Mesh", "Algorithm", 5);
   GModel *m = new GModel();
   //GmshMergeFile("../../tutorial/t5.geo"); // will also 
set the bbox
   for(GModel::riter it = m->firstRegion(); it != 
m->lastRegion(); ++it){
     GRegion *r = *it;
     printf("volume %d contains %d elements:\n", r->tag(), 
     for(unsigned int i = 0; i < r->getNumMeshElements(); 
       printf(" %d", r->getMeshElement(i)->getNum());
   delete m;

After compilation I have 1225 errors like these:
Error	1	error LNK2001: unresolved external symbol "public: 
static int __cdecl Fl::check(void)" 
(?check at Fl@@SAHXZ)	Gmsh.lib	delme
Error	5	error LNK2001: unresolved external symbol "void 
__cdecl fl_alert(char const *,...)" 
(?fl_alert@@YAXPBDZZ)	Gmsh.lib	delme
et cetera

If I delete Gmsh.lib from 'Additional Dependencies', after 
compilation I have 8 errors like these:
Error	4	error LNK2001: unresolved external symbol "public: 
int __thiscall GModel::mesh(int)" 
(?mesh at GModel@@QAEHH at Z)	main.obj	delme
Error	7	error LNK2001: unresolved external symbol "int 
__cdecl GmshFinalize(void)" (?GmshFinalize@@YAHXZ)	 
       main.obj	delme
et cetera

In first case I tried to include a fltk library (I thought 
that 'Fl' means a part of 'fltk'), but I miscarried.
Because of <<no complete documentation of this API is 
available yet>> can anybody help me?

Mikhail Artemiev, PhD student, NSTU

> Mikhail Titov:
> I would suggest to communicate with Gmsh using network. 
>Take a look at the
> source code. It has a couple of nice examples in 
>utils/solvers subfolder. It
> will definitely save some time since you can reuse 
>existing instance instead
> of launching new process every time.
> IMHO it would be the last resort to link gmsh code in.
> Mikhail