# How do you reference a symbol from a specific module in a fortran shared object library with the ccall utility?

**URL:** <https://discourse.julialang.org/t/how-do-you-reference-a-symbol-from-a-specific-module-in-a-fortran-shared-object-library-with-the-ccall-utility/93580>\
**Category:** General Usage\
**Created:** [January 26, 2023, 3:29pm UTC](https://discourse.julialang.org/t/how-do-you-reference-a-symbol-from-a-specific-module-in-a-fortran-shared-object-library-with-the-ccall-utility/93580 "2023-01-26T15:29:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmmpie](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@dmmpie](https://discourse.julialang.org/u/dmmpie)\
**Post date:** [January 26, 2023, 3:29pm UTC](https://discourse.julialang.org/t/how-do-you-reference-a-symbol-from-a-specific-module-in-a-fortran-shared-object-library-with-the-ccall-utility/93580/1 "2023-01-26T15:29:48Z")

</div>

When I have a “simple” fortran program with a singular module, I can reference the symbols simply by copying the symbol name from the result of the `nm` command:

```julia
x = ccall((:__mod_julfort_MOD_dot, "path/to/my/lib.so"),
            Float64,
            (Ref{Int64}, Ptr{Float64}, Ptr{Float64}),
            l, arrx, arry)

```

However, I have a larger library with many different modules, so nm instead returns something like

`00000000000ef4e0 T __my_mod_name.my_MOD_function_name`

Simply copying `__my_mod_name.my_MOD_function_name` doesn’t work because the `.` is not a recognized syntax, and replacing `.` with `_` like

```julia
x = ccall((:__my_mod_name_my_MOD_function_name, "path/to/my/lib.so"),
               ...

```

Returns the error

`Error: ErrorException("could not load symbol \" __my_mod_name_my_MOD_function_name\":\n../path/to/my/lib.so: undefined symbol:__ my_mod_name_my_MOD_function_name")`

How do I correctly call this function using `ccall`?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 26, 2023, 4:10pm UTC](https://discourse.julialang.org/t/how-do-you-reference-a-symbol-from-a-specific-module-in-a-fortran-shared-object-library-with-the-ccall-utility/93580/2 "2023-01-26T16:10:40Z")

</div>

> [@dmmpie](#):
>
> Simply copying `__my_mod_name.my_MOD_function_name` doesn’t work because the `.` is not a recognized syntax

I don’t have a Fortran module at hand for trying this out, but perhaps try with

```julia
Symbol("__my_mod_name.my_MOD_function_name")

```

?

---

<div class="post-metadata">

**Author:** ![dmmpie](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@dmmpie](https://discourse.julialang.org/u/dmmpie)\
**Post date:** [January 26, 2023, 4:21pm UTC](https://discourse.julialang.org/t/how-do-you-reference-a-symbol-from-a-specific-module-in-a-fortran-shared-object-library-with-the-ccall-utility/93580/3 "2023-01-26T16:21:12Z")

</div>

This returns the error

`Error: TypeError(Symbol("ccall: first argument not a pointer or valid constant expression"), "", Ptr, (Symbol("__my_mod_name.my_MOD_function_name"), "path/to/my/lib.so"))`

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 26, 2023, 4:27pm UTC](https://discourse.julialang.org/t/how-do-you-reference-a-symbol-from-a-specific-module-in-a-fortran-shared-object-library-with-the-ccall-utility/93580/4 "2023-01-26T16:27:59Z")

</div>

Can you please show the code you’re using?

---

<div class="post-metadata">

**Author:** ![dmmpie](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@dmmpie](https://discourse.julialang.org/u/dmmpie)\
**Post date:** [January 26, 2023, 4:38pm UTC](https://discourse.julialang.org/t/how-do-you-reference-a-symbol-from-a-specific-module-in-a-fortran-shared-object-library-with-the-ccall-utility/93580/5 "2023-01-26T16:38:41Z")

</div>

I am using the library [DAMASK](https://github.com/eisenforschung/DAMASK/tree/master/src), which I compiled into a shared object library and where I created a new subroutine in the file `phase_mechanical_plastic_phenopowerlaw.f90` called `test_routine`:

```julia
module subroutine test_routine(ph,group)
  integer :: ph
  character(len=*) :: group
  print *, "test", ph, group
  ph = ph+2
end subroutine test_routine

```

My julia file looks like this:

```julia
function main()
    try
        err = ccall((Symbol("__phase.mechanical_MOD_test_routine"), "../DAMASK/lib/libDAMASK_grid.so"), 
                        Cvoid, 
                        (Int64,Ptr{UInt8}), 
                        3, "asd")
    catch e
        println("Error: ", e)
    end
end
main()

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 26, 2023, 5:43pm UTC](https://discourse.julialang.org/t/how-do-you-reference-a-symbol-from-a-specific-module-in-a-fortran-shared-object-library-with-the-ccall-utility/93580/6 "2023-01-26T17:43:27Z")

</div>

Uhm, I wonder if it doesn’t work because `ccall` doesn’t like the dynamic call to `Symbol`.

Does

```julia
using Libdl
sym = Libdl.dlsym(Libdl.dlopen("../DAMASK/lib/libDAMASK_grid.so"), Symbol("__phase.mechanical_MOD_test_routine"))
function main()
    try
        err = ccall(sym, 
                        Cvoid, 
                        (Int64,Ptr{UInt8}), 
                        3, "asd")
    catch e
        println("Error: ", e)
    end
end
main()

```

work any better?

---

<div class="post-metadata">

**Author:** ![dmmpie](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@dmmpie](https://discourse.julialang.org/u/dmmpie)\
**Post date:** [January 27, 2023, 4:17pm UTC](https://discourse.julialang.org/t/how-do-you-reference-a-symbol-from-a-specific-module-in-a-fortran-shared-object-library-with-the-ccall-utility/93580/7 "2023-01-27T16:17:59Z")

</div>

Your solution works and julia is now able to enter the fortran subroutine I defined. This is how the final working version looked, replacing the string with another int for simplicity:

```julia
using Libdl
sym = Libdl.dlsym(Libdl.dlopen("../DAMASK/lib/libDAMASK_grid.so"), Symbol("__phase.mechanical_MOD_test_routine"))
function main()
    try
        a::Int64 = 3
        b::Int64 = 4
        err = ccall(sym, 
                        Cvoid, 
                        (Ref{Int64},Ref{Int64}), 
                        a, b)
    catch e
        println("Error: ", e)
    end
end
main()

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 27, 2023, 4:55pm UTC](https://discourse.julialang.org/t/how-do-you-reference-a-symbol-from-a-specific-module-in-a-fortran-shared-object-library-with-the-ccall-utility/93580/8 "2023-01-27T16:55:48Z")

</div>

Small nitpick: you don’t need to type-annotate the local variables `a` and `b`, also, it’d be better to avoid non-constant global variables (this is a mistake I made in my post above). I’d slightly simplify your code to:

```julia
using Libdl
function main()
    sym = Libdl.dlsym(Libdl.dlopen("../DAMASK/lib/libDAMASK_grid.so"), Symbol("__phase.mechanical_MOD_test_routine"))
    try
        a = 3
        b = 4
        err = ccall(sym, 
                    Cvoid, 
                    (Ref{Int64},Ref{Int64}), 
                    a, b)
    catch e
        println("Error: ", e)
    end
end
main()

```
