jacobwilliams / json-fortran

A Modern Fortran JSON API
https://jacobwilliams.github.io/json-fortran/
Other
332 stars 83 forks source link

Undefined symbols for architecture x86_64 #513

Closed jaxxstorm closed 2 years ago

jaxxstorm commented 2 years ago

I installed the lib from homebrew, and trying to compile my first program:

gfortran test.f90 -o hello -I /usr/local/Cellar/json-fortran/8.2.5/include/
Undefined symbols for architecture x86_64:
  "___json_value_module_MOD___vtab_json_value_module_Json_core", referenced from:
      _MAIN__ in ccKxWSxi.o
  "___json_value_module_MOD_json_failed", referenced from:
      _MAIN__ in ccKxWSxi.o
  "___json_value_module_MOD_json_initialize", referenced from:
      _MAIN__ in ccKxWSxi.o
  "___json_value_module_MOD_json_print_to_filename", referenced from:
      _MAIN__ in ccKxWSxi.o
  "___json_value_module_MOD_json_value_add_member", referenced from:
      _MAIN__ in ccKxWSxi.o
  "___json_value_module_MOD_json_value_destroy", referenced from:
      _MAIN__ in ccKxWSxi.o
  "___json_value_module_MOD_wrap_json_value_add_integer", referenced from:
      _MAIN__ in ccKxWSxi.o
  "___json_value_module_MOD_wrap_json_value_add_integer_vec", referenced from:
      _MAIN__ in ccKxWSxi.o
  "___json_value_module_MOD_wrap_json_value_add_logical", referenced from:
      _MAIN__ in ccKxWSxi.o
  "___json_value_module_MOD_wrap_json_value_add_logical_vec", referenced from:
      _MAIN__ in ccKxWSxi.o
  "___json_value_module_MOD_wrap_json_value_add_real", referenced from:
      _MAIN__ in ccKxWSxi.o
  "___json_value_module_MOD_wrap_json_value_add_string_vec", referenced from:
      _MAIN__ in ccKxWSxi.o
  "___json_value_module_MOD_wrap_json_value_create_object", referenced from:
      _MAIN__ in ccKxWSxi.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

I'm new to fortran, any idea what I've done wrong here?

jacobwilliams commented 2 years ago

I get something similar for this simple test case:

brew install json-fortran

test.f90:

program test

use json_module

implicit none
type(json_core) :: json 

call json%initialize()
if (json%failed()) error stop 'error'

end program test
gfortran test.f90 -o hello -I /opt/homebrew/Cellar/json-fortran/8.2.5/include/

gives:

Undefined symbols for architecture arm64:
  "___json_value_module_MOD___vtab_json_value_module_Json_core", referenced from:
      _MAIN__ in ccr11AMx.o
  "___json_value_module_MOD_json_failed", referenced from:
      _MAIN__ in ccr11AMx.o
  "___json_value_module_MOD_json_initialize", referenced from:
      _MAIN__ in ccr11AMx.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status

System:

I don't normally use json-fortran from Home-brew though. Did this used to work? Maybe it broke at some point?

jacobwilliams commented 2 years ago

Oh, you forgot to actually link with the library. So something like this:

gfortran test.f90 -o hello -I /opt/homebrew/Cellar/json-fortran/8.2.5/include/ /opt/homebrew/Cellar/json-fortran/8.2.5/lib/libjsonfortran.a

That works for me.

Or, you add the lib path and you can do it like this:

gfortran test.f90 -o hello -I /opt/homebrew/Cellar/json-fortran/8.2.5/include/ -L /opt/homebrew/Cellar/json-fortran/8.2.5/lib -ljsonfortran
jaxxstorm commented 2 years ago

That got it! For anyone else who comes across this:

gfortran example.f90 -o hello -I $(brew --prefix)/Cellar/json-fortran/8.2.5/include/ $(brew --prefix)/Cellar/json-fortran/8.2.5/lib/libjsonfortran.a
YL-hydro commented 1 year ago

Hi,

Can anyone help me with this issue I have when I was compiling Fortran code on my Mac (M1)? It looks like a similar issue as @jaxxstorm post but I still really cannot figure it out. I'm very new to compiling Fortran code:

so I have the following Fortran code (.f) files. They are main and subroutine code files of a model that I want to run on my Mac.

Screenshot 2023-04-01 at 12 10 53 am

so I firstly use the following to get .o files gfortran -c sutra_3_0.f fmods_3_0.f lmods_3_0.f lsubs_3_0.f ssubs_3_0.f usubs_3_0.f

then I tried to use the .o files to compile the code so I can run the model (using sutra3_mac), as below: gfortran -o sutra3_mac fmods_3_0.o lmods_3_0.o lsubs_3_0.o ssubs_3_0.o usubs_3_0.o

but it gave me this error

Screenshot 2023-04-01 at 12 17 15 am

I'm still not clear about how to link the library. I located the library of gfortran installed on my Mac:

Screenshot 2023-04-01 at 12 20 24 am

and under /usr/local/gfortran/lib folder it has the following:

Screenshot 2023-04-01 at 12 22 48 am

and under /usr/local/gfortran/lib/gcc/aarch64-apple-darwin22/12.2.0 it has the following:

Screenshot 2023-04-01 at 12 24 15 am

Can someone provide any guidance on compiling the code? Thanks a lot!