# Calling C and Fortran Code

**URL:** <https://discourse.julialang.org/t/calling-c-and-fortran-code/15665>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 29, 2018, 1:36pm UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code/15665 "2018-09-29T13:36:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)\
**Post date:** [September 29, 2018, 1:36pm UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code/15665/1 "2018-09-29T13:36:30Z")

</div>

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
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

```

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [September 29, 2018, 1:59pm UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code/15665/2 "2018-09-29T13:59:09Z")

</div>

Works for me:

```julia
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.

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [September 29, 2018, 2:30pm UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code/15665/3 "2018-09-29T14:30:53Z")

</div>

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

```julia
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
julia> t = ccall((:clock, "libc"), Int64, ())
1920451

```

---

<div class="post-metadata">

**Author:** ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)\
**Post date:** [September 29, 2018, 2:32pm UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code/15665/4 "2018-09-29T14:32:36Z")

</div>

I am on windows 10 and Julia 1.0

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [September 29, 2018, 2:34pm UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code/15665/5 "2018-09-29T14:34:53Z")

</div>

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

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

```

See [https://github.com/JuliaLang/julia/issues/26557](https://github.com/JuliaLang/julia/issues/26557)

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [September 30, 2018, 6:35am UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code/15665/6 "2018-09-30T06:35:42Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)\
**Post date:** [September 30, 2018, 8:56am UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code/15665/7 "2018-09-30T08:56:58Z")

</div>

> [@Tero\_Frondelius](#):
>
> t = ccall((:clock, “libc”), Int32, ())

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.

---

<div class="post-metadata">

**Author:** ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)\
**Post date:** [September 30, 2018, 10:50am UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code/15665/8 "2018-09-30T10:50:05Z")

</div>

Thanks it works.
