Need help with "core:c" and integrating Odin with C code

I am new to Odin. I thought I would give it a try with an existing C programming project on which I am working. I would like to make an Odin proc() which can be called from C and the C layer passes a pointer to a struct to the Odin proc(). The problem that I am having is that one of my struct members is a variable named ‘c’. For example, in Odin, I would declare this line as:

BravaisLattice :: struct {
a, b, c, alpha, beta, gamma: f64
// many other structure members
}

However, because the struct is coming from C code, I tried this:
BravaisLattice :: struct {
a, b, c, alpha, beta, gamma: c.double
}

The Odin compiler isn’t happy with this, and here is error and some presumably related errors:

The names of these variables (the crystallographic unit cell parameters) are well established in my field, and I would prefer to not have to rename them as, e.g. ax, by, cz, if at all possible. Thanks for any guidance.

I’m not entirely sure, but if it’s name collision you are running into, you could alias the “core:c” library to a different name. Something like…

import cee "core:c"

main :: proc() {
  myvar: cee.double
}

or

import core_c "core:c"

main :: proc() {
  myvar: core_c.double
}

I tried that. I used import ct “core:c”, but it didn’t resolve the errors.

I think it would help if you shared the first 27 lines of that file. Obfuscate what you want, but without any context, one can only guess.

Edit: Or share a small (but full) example file that reproduces the error.

My take on what I can see is…
That first error " cyclic of ‘c’ " I’m pretty sure is name collision on ‘c’. The second error is saying there is another variable somewhere with the name ‘c’ that is already defined as f64. The third is saying ‘c’ is not a type, but a previously define variable. I interpret the last 2 as the same as the second error.

Taking everything that I can get from your original post, the following compiles for me. So I think there may be something else going on that I cannot see from your post. Sorry if I’m not very helpful.

import core_c "core:c"

BravaisLattice :: struct {
	a, b, c, alpha, beta, gamma: core_c.double,
	metric_tensor: [3][3]core_c.double,
	volume: core_c.double,
	sum_abc: core_c.double,
	cos_alpha, cos_beta, cos_gamma: core_c.double,
}
odin build scratch.odin -file -out:export_odin.o -no-entry-point -build-mode:object -reloc-mode:pic

produces…

scratch/export_odin-builtin.o
scratch/export_odin-runtime-core.o
scratch/export_odin-runtime-core_builtin.o
scratch/export_odin-runtime-default_temp_allocator_arena.o
scratch/export_odin-runtime-default_temporary_allocator.o
scratch/export_odin-runtime-error_checks.o
scratch/export_odin-runtime-heap_allocator.o
scratch/export_odin-runtime-heap_allocator_unix.o
scratch/export_odin-runtime-internal.o
scratch/export_odin-runtime-os_specific.o
scratch/export_odin-runtime-os_specific_linux.o
scratch/export_odin-runtime-print.o
scratch/export_odin-runtime-procs.o
scratch/export_odin-runtime-random_generator_chacha8.o
scratch/export_odin-runtime-random_generator_chacha8_simd128.o
1 Like

OK, that worked. I think in my text substitution, I mistyped something embarassingly dumb. I typed in:

import “core:c”
import core_c “core:fmt” // doh!

When I aliased it properly,

import core_c “core:c”
import “core:fmt”

Most of the compilation errors cleaned up. I was also getting a “context not defined in this scope” error. I added, as the compiler message suggested, “context = runtime.default_context()” into my Odin procedure, and the code compiled cleanly.

Thanks for your help!

Paul

2 Likes