Calling C and Fortran Code

I am reading julia 1.0.0 manual. In chapter 17 page 300, it uses the following command to call Clock function from C but I get an error. I tried install Cxx but of no help.

julia> ccall((:clock, "libc"), Int32, ())
ERROR: error compiling top-level scope: could not load library "libc"
The specified module could not be found.

(v1.0) pkg> add Cxx;build;precompile
  Updating registry at `C:\Users\chatura\.julia\registries\General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
ERROR: Unsatisfiable requirements detected for package Cxx [a0b5b9ef]:
 Cxx [a0b5b9ef] log:
 ├─possible versions are: [0.0.1-0.0.2, 0.1.0-0.1.1, 0.2.0] or uninstalled
 ├─restricted to versions * by an explicit requirement, leaving only versions [0.0.1-0.0.2, 0.1.0-0.1.1, 0.2.0]
 └─restricted by julia compatibility requirements to versions: uninstalled — no versions left

Works for me:

julia> t = ccall((:clock, "libc"), Int32, ())
566964

julia> t = ccall((:clock, "libc"), Int32, ())
682890

julia> t
682890

julia> typeof(ans)
Int32

Edit: osx (mac) where it works on v1.0.0.

I can confirm this but I get a bit different error message (Ubuntu 18.04, julia
0.7 and 1.0):

julia> t = ccall((:clock, "libc"), Int64, ())
ERROR: error compiling top-level scope: could not load library "libc"
/usr/lib/x86_64-linux-gnu/libc.so: invalid ELF header

In 0.6.4

julia> t = ccall((:clock, "libc"), Int64, ())
1920451

I am on windows 10 and Julia 1.0

I have to give the full name of the library (Ubuntu 18.04). This works for me in Julia 1.0:

ccall((:clock, "libc.so.6"), Int, ())

See https://github.com/JuliaLang/julia/issues/26557

1 Like

@Ajaychat3 I think the Julia documentation needs to be updated here.
Please look at this issue
https://github.com/JuliaLang/julia/issues/26557

As far as I can see Julia does not know the path to the libc

On windows, this usually works:

t = ccall((:clock, “msvcrt”), Int32, ())

Use msvcrt for libc, most times, but sometimes it needs a different dll with a version like 140 in the name.

Thanks it works.