简体   繁体   中英

How to call a fortran function from a c++ file in node-gyp?

I defined my fortran function in my "a.cpp" file as

extern "C" double trial_func_(long *fsize, double* fvec);

I compiled my fortran code with:

$ gfortran trial.f90 -c -o trial.o

I tried to build a library from the trial.o file using:

$ ar rcs fortranlib.a trial.o

My binding.gyp file:

{
  "targets": [
    {
      "target_name": "rocket",
      "cflags!": [ "-fno-exceptions"],
      "cflags_cc!": [ "-fno-exceptions"],
      "libraries": [ "C:\\fortranlib.a" ],
      "sources": [
        "./cpp/a.cpp",
        "./cpp/index.cpp"
      ],
      "include_dirs": [
        "<!@(node -p \"require('node-addon-api').include\")"
      ],
      'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
    }
  ]
}

I get the following errors when I use "node-gyp rebuild":

a.obj : error LNK2019: unresolved external symbol trial_func_ referenced in function "double __cdecl fortranTrial(void)" (?fortranTrial@@YANXZ)

C:\\fortranlib.a : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'

Any ideas how can I do this properly?

Okay I got it.

I've been using gfortran 32-bit version, so I changed that to 64-bit.

Then I used the .o file in "libraries" instead of .a file and that worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM