vikash
vikash

Reputation: 23

How do I run gfortran code from sublime text?

I have a Fortran 95 code which I compile and run on my Linux OS through the terminal window, by first compiling the individual modules and then the main program. Now I am trying to run the same code from sublime text version 3.0, without using the terminal window. I got the motivation from this video: https://www.youtube.com/watch?v=_eZyTNthJG4

But when I ctrl+b, build the code, it gives the following console output:

/tmp/cc4nYhyn.o: In function `MAIN__':
problem_main.f95:(.text+0x2e5): undefined reference to `__universal_parameter_declaration_MOD_compute_starts'
problem_main.f95:(.text+0x33e): undefined reference to `__array_creator_MOD_linear_spaced'
problem_main.f95:(.text+0x356): undefined reference to `__derivatives_MOD_dydx'
problem_main.f95:(.text+0x3e4): undefined reference to `__ode_solver_MOD_heun'
problem_main.f95:(.text+0x69e): undefined reference to `__universal_parameter_declaration_MOD_compute_ends'
problem_main.f95:(.text+0x6b0): undefined reference to `__universal_parameter_declaration_MOD_compute_ends'
problem_main.f95:(.text+0x6b8): undefined reference to `__universal_parameter_declaration_MOD_compute_starts'
problem_main.f95:(.text+0x6c4): undefined reference to `__universal_parameter_declaration_MOD_compute_time'
problem_main.f95:(.text+0x726): undefined reference to `__universal_parameter_declaration_MOD_compute_time'
problem_main.f95:(.text+0x988): undefined reference to `__universal_parameter_declaration_MOD_data_write_ends'
problem_main.f95:(.text+0x99a): undefined reference to `__universal_parameter_declaration_MOD_data_write_ends'
problem_main.f95:(.text+0x9a2): undefined reference to `__universal_parameter_declaration_MOD_compute_ends'
problem_main.f95:(.text+0x9ae): undefined reference to `__universal_parameter_declaration_MOD_data_write_time'
problem_main.f95:(.text+0xa10): undefined reference to `__universal_parameter_declaration_MOD_data_write_time'
problem_main.f95:(.text+0xa77): undefined reference to `__universal_parameter_declaration_MOD_plot_ends'
problem_main.f95:(.text+0xa89): undefined reference to `__universal_parameter_declaration_MOD_plot_ends'
problem_main.f95:(.text+0xa91): undefined reference to `__universal_parameter_declaration_MOD_data_write_ends'
problem_main.f95:(.text+0xa9d): undefined reference to `__universal_parameter_declaration_MOD_plot_time'
problem_main.f95:(.text+0xaa5): undefined reference to `__universal_parameter_declaration_MOD_plot_ends'
problem_main.f95:(.text+0xaad): undefined reference to `__universal_parameter_declaration_MOD_compute_starts'
problem_main.f95:(.text+0xab9): undefined reference to `__universal_parameter_declaration_MOD_total_time'
problem_main.f95:(.text+0xb1b): undefined reference to `__universal_parameter_declaration_MOD_plot_time'
problem_main.f95:(.text+0xbad): undefined reference to `__universal_parameter_declaration_MOD_total_time'
collect2: error: ld returned 1 exit status
[Finished in 0.1s with exit code 1]
[shell_cmd: gfortran '/home/vikash/Work/Fort_test/problem_main.f95' -o '/home/vikash/Work/Fort_test/problem_main']
[dir: /home/vikash/Work/Fort_test]
[path: /home/vikash/bin:/home/vikash/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]

It seems when running from the sublime text, the compiler is unable to detect the modules that are in the same folder as the main program file. I have also tried to build the individual modules through the editor, but it did not worked either. How do I fix this?

Upvotes: 2

Views: 1896

Answers (2)

Enrique
Enrique

Reputation: 9

In Windows, create a "Testing" file such as: "Testing.sublime-build", to be saved in the folder of User Packages. Then, enter inside this file the following:

{
"cmd" : ["gfortran","$file_name","&","a.exe"],
"selector" : "source.f90",
"working_dir" : "${file_path}",
"shell" : true
}

And finally build/run the fortran (.f90) file with this new builder.

Upvotes: 0

OdatNurd
OdatNurd

Reputation: 22791

Sublime has the ability to execute an arbitrary external program or series of commands that you define, and this is at the core of how Build Systems in Sublime work. A Build System is inherently just a specific set of rules for what action to take in order to build your program, run it, or do some other task such as loading a web page in your browser as you're working on it.

Something to keep in mind that Sublime is a text editor and not an IDE, and as such things like building programs may not work the way you would expect them to as in other software. This can often catch you unaware because although Sublime is quite powerful in this regard, it doesn't handle all of the low level details for you and relies on you to fill in the gaps.

In your particular case, it looks like the crux of your problem is that your program is made up of several modules, but the build system that you're using is trying to compile and link only the file that you're currently editing.

Sublime doesn't ship with support for Fortran enabled, so you likely have installed a package to provide Fortran support. The Fortran package contains the following GFortran.sublime-build file to use as illustration here:

{
    "cmd": ["gfortran", "${file}",  "-o",  "${file_base_name}"],
    "file_regex": "^(?xi:( ^[/] [^:]* ) : (\\d+) : (\\d+) :)",
    "working_dir": "${file_path}",
    "selector": "source.modern-fortran, source.fixedform-fortran",
    "syntax": "GFortranBuild.sublime-syntax",
    "windows": {
        "cmd": ["gfortran", "${file}", "-o", "${file_base_name}.exe"]
    },
    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "gfortran \"${file}\" -o \"${file_base_name}\" && \"./${file_base_name}\"",
            "shell": true,
            "windows": {
                "cmd": ["gfortran", "${file}", "-o", "${file_base_name}.exe", "&&", "${file_base_name}.exe"]
            }
        }
    ]
}

In a nutshell, this build system will work only to either compile the current file or to compile the current file and then run the result; anything else is "too complicated" for it to understand. This sounds like the problem that you're having; the current file is being compiled and linked without regard for the rest of the files that are needed, so it fails to link.

The solution here is to use or make a build system that does what you're doing in the terminal; compile the individual modules one by one and then link them into the main program.

My best advice would be to use the appropriate tool for the appropriate job; for example something like make can be given a simple control file that tells it what files make up your project, and it takes care of the heavy lifting in regards to knowing what needs to be compiled, and Sublime already supports make out of the box. There are a ton of similar tools out there depending on your programming language and environment that you could use as well.

Failing that you need to create a build system that knows how to build your program, which would generally involve writing something that mimics what you do in the terminal manually in order to build your program.

You mentioned in comments on your question that you use sh to run a file that does the job of a Makefile. To create a custom build system that does that, you would do something like the following:

  • Select Tools > Build System > New Build System... from the menu
  • Modify the template sublime-build file to run the appropriate command(s)
  • Save the result as a sublime-build file in the default location Sublime prompts you with (your User folder).

Once you follow these steps, a build with the same name as the file you saved will appear in the Tools > Build System menu.

Such a build system might look like the following:

{
    "shell_cmd": "sh myfile.txt",
    "working_dir": "${file_path}",
    "selector": "source.modern-fortran, source.fixedform-fortran",
}

The selector makes this build system available automatically in fortran files (at least in the syntax defined by the package above), in which case to use this you can either set the build system to Automatic and have it be selected for you, or you can set the build explicitly to this build file to ensure that it's used.

This would use the shell to execute myfile.txt after first switching the current directory to the location of the file you're currently editing. You can adjust this as appropriate, such as by changing the name of the file being executed and making sure that the working directory is otherwise correct, and so on.

Refer to the Official Documentation for more information on how build systems work and what options are available to you.

Upvotes: 4

Related Questions