Jörg Haubrichs
Jörg Haubrichs

Reputation: 2253

Nim: Include C header for Posix

I'm trying to make use of the Nim Posix library, specifically fileystem stats:

http://nim-lang.org/docs/posix.html#Stat

From my tests with other Nim modules, importing and using should work like this:

import posix

var stats: Stat
stat("/", stats)
echo stats.st_blksize

But the compiler gives me a

Error: undeclared identifier: 'Stat'

Do I have to add some manual includes when compiling, or am I missing something with the import?

Upvotes: 2

Views: 1089

Answers (1)

Grzegorz Adam Hankiewicz
Grzegorz Adam Hankiewicz

Reputation: 7681

You are likely putting these lines into a file named posix.nim. This is a problem, because the nim compiler will see that the posix module is already imported and won't look for any other in the standard library. The solution to this would be rename your program to po.nim or something else.

Once you go past this unlucky behaviour you will find out that the code doesn't compile because the stat proc returns a cint which you are not assigning to anything:

po.nim(4, 4) Error: value of type 'cint' has to be discarded

You can discard the value. The following modified version compiles and runs for me on the stable version 0.9.4 of the nimrod compiler:

import posix

var stats: Stat
discard stat("/", stats)
echo stats.st_blksize

Upvotes: 6

Related Questions