vmagnin / gtk-fortran

A GTK / Fortran binding, and its documentation in the Wiki tab.
GNU General Public License v3.0
256 stars 43 forks source link

Unable to compile gtkhello.f90 using gfortran on WSL Ubuntu-20.04 #231

Closed ifemi closed 3 years ago

ifemi commented 3 years ago

Describe the bug I ran the following: gfortranpkg-config --cflags --libs gtk+-3.0./gtk-examples/gtkhello.f90 ./gtk-src/gtk.f90

Expected behavior Expected to compile and run a gui

To Reproduce

Your system:

Additional context Error:

`./gtk-examples/gtkhello.f90:35:6:

35 | use gtk, only: gtk_main_quit, FALSE | 1 Fatal Error: Cannot open module file ‘gtk.mod’ for reading at (1): No such file or directory compilation terminated. ./gtk-src/gtk.f90:53:8:

53 | use g, only: g_signal_connect_data | 1 Fatal Error: Cannot open module file ‘g.mod’ for reading at (1): No such file or directory compilation terminated.`

ifemi commented 3 years ago

The main issue seems to be: `./gtk-src/gtk.f90:53:8:

53 | use g, only: g_signal_connect_data | 1 Fatal Error: Cannot open module file ‘g.mod’ for reading at (1): No such file or directory`

vmagnin commented 3 years ago

Welcome @ifemi , you must compile the files in a precise order: if a file uses a module, the module must be placed before in the compilation command line: ./gtk-src/glib-auto.f90 ./gtk-src/gtk.f90 ./gtk-examples/gtkhello.f90 because gtkhello.f90 uses the gtk module which uses the g module (g stands for glib).

And for more complicated examples you may need to identify and compile more modules. That's why it's better to use CMake and install the gtk-fortran library in your system (see https://github.com/vmagnin/gtk-fortran/wiki#linux).

Then compiling a program will be very easy:

$ gfortran ./gtk-examples/gtkhello.f90 $(pkg-config --cflags --libs gtk-3-fortran)

As you are the first user to report using WSL, let me know your progress. Are you using WSL1 or WSL2?

ifemi commented 3 years ago

Very many thanks @vmagnin everything is quite ok now

I did use CMake to install gtk-fortran.

My environment is WSL2, Ubuntu-20.04 and Visual Studio Code as IDE and of course gfortran, gtk-fortran and glade for the UI

NOTE: In VS Code the command used in the task.json is: gfortran ${file} pkg-config --cflags --libs gtk-3-fortran -o ${fileDirname}/${fileBasenameNoExtension}.out

However that gives a permision error and so I had to sudo is and provide the password when requested.

Without sudo:

`/gtk-examples/gtkhello.f90:72:19:

72 | end module handlers | 1 Fatal Error: Cannot open module file ‘handlers.mod0’ for writing at (1): Permission denied compilation terminated.`

ifemi commented 3 years ago

@vmagnin is there a way of getting past the permission problem?

vmagnin commented 3 years ago

@ifemi , thanks for sharing information about WSL2 and VS Code, that could be useful for other users! Does the compilation command works in command line? It seems VS Code is running gfortran from a directory where it has no permission.

Maybe you could try using the gfortran's -J option:

       -Jdir
       This option specifies where to put .mod files for compiled modules.  It is also added to the list of
       directories to searched by an "USE" statement.

       The default is the current directory.
ifemi commented 3 years ago

Many thanks @vmagnin it worked on the command line.

The -Jdir worked perfectly. Please note for VS Code I used: "gfortran -g -J${fileDirname} ${file} pkg-config --cflags --libs gtk-3-fortran -o ${fileDirname}/${fileBasenameNoExtension}.out"

or

"gfortran -g -J${fileDirname} ${file} pkg-config --cflags --libs gtk-3-fortran -o ${fileDirname}/${fileBasenameNoExtension}"