Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6869445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:33:31+00:00 2026-05-27T03:33:31+00:00

I’m interested in calling fortran codes in a Mathematica session. I learn that Mathlink

  • 0

I’m interested in calling fortran codes in a Mathematica session. I learn that Mathlink offers a way to do that. But I have little knowledge on C and nothing on C++.
Is anybody willing to give me a detailed example?

I’m using with Mathematica 8, MS Visual Studio 2008 and Intel Fortran 11. The system is Windows 7 Home Premium.

Many thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:33:32+00:00Added an answer on May 27, 2026 at 3:33 am

    The following is an explicit example which I succeeded using gfortan and gcc with the Windows system:

    I found this blog Adventures in Mathlink.
    It is helpful with a specific example. I installed MinGW in order to use gfortran and gcc. After installation, one must set PATH in order to use gfortran and gcc without typing the path each time. A tip for adding PATH without restarting the system: After adding the PATH, open cmd, and run set PATH=C: Then close cmd, whenyou open it again, with echo %PATH%, you will see the new path list. I followed the steps in the linked blog, adapted to Windows, with the tutorial example addtwo:

    The Mathematica codes writing a .bat file and running it to generate the executable

    (* Write a .bat file to compile the MathLink template *.tm, FORTRAN codes *.f and 
    C codes *.c files, and run it to create an executable file. *)
    CreateExeF[s_String] := 
    Module[{dir, libdir, bindir, BatCode, bat}, dir = NotebookDirectory[];
    {libdir, bindir} = StringJoin[
      "\"", $InstallationDirectory, 
     "\\SystemFiles\\Links\\MathLink\\DeveloperKit\\Windows\\CompilerAdditions\\mldev32\\",
      #] & /@ {"lib\\", "bin\\"};
    BatCode = StringJoin[
     "gfortran -c ", #, ".f -o ", #, "f.o
     gcc -c ", #, ".c -o ", #, ".o
     ",
     bindir, "mprep.exe\" ", #, ".tm -o ", #, "tm.c
     gcc -c ", #, "tm.c -o ", #, "tm.o
     gcc ", #, "tm.o ", #, ".o ", #, "f.o ",
     libdir, "ml32i3m.lib\" ", 
     "-lm -lpthread -mwindows -lstdc++ -o ", #
     ] &;
     (* write the .bat file *)
     bat = Export[FileNameJoin[{dir, # <> ".bat"}], BatCode[dir <> #], 
      "string"] &[s];
     (* run the .bat file *)
     Run[bat]]
    

    FORTRAN codes addtwo.f

       subroutine addtwof(i,j,k)
       integer i, j, k
       k = i + j
       end
    

    C wrapper addtwo.c

    #include "mathlink.h"
    
    int addtwo(int i, int j) 
    {
      int res;
      addtwof_(&i, &j, &res);
      return res;
    }
    
    #if WINDOWS_MATHLINK
    
    #if __BORLANDC__
    #pragma argsused
    #endif
    
    int PASCAL WinMain( HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine,     int nCmdShow)
    {
    char  buff[512];
    char FAR * buff_start = buff;
    char FAR * argv[32];
    char FAR * FAR * argv_end = argv + 32;
    
    hinstPrevious = hinstPrevious; /* suppress warning */
    
    if( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1;
    MLScanString( argv, &argv_end, &lpszCmdLine, &buff_start);
    return MLMain( (int)(argv_end - argv), argv);
    }
    
    #else
    
    int main(int argc, char* argv[])
    {
    return MLMain(argc, argv);
    }
    
    #endif
    

    The template file addtwo.tm is the same as the one in Todd Gayley’s tutorial. For completeness, it is also given here:

    :Begin:
    :Function:       addtwo
    :Pattern:        AddTwo[i_Integer, j_Integer]
    :Arguments:      { i, j }
    :ArgumentTypes:  { Integer, Integer }
    :ReturnType:     Integer
    :End:
    
    :Evaluate:       AddTwo::usage = "AddTwo[i, j] gives the sum of two integer numbers i and j."
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.