Skip to content

Multiple definition error when using codegenDecl #26015

Description

@exelotl

Nim Version

nim -v
Nim Compiler Version 2.3.1 [Linux: amd64]
Compiled at 2026-07-16
Copyright (c) 2006-2026 by Andreas Rumpf

git hash: a6fa322
active boot switches: -d:release

Description

As of Nim 2.4.0 RC1 it seems that the 'extern' keyword is no longer emitted for variables imported from another module which have the codegenDecl pragma applied to them.

Example:

# foo.nim
var myvar* {.codegenDecl: "__attribute__ ((aligned (16))) $# $#".}: int = 123
# main.nim
import ./foo
echo myvar

Current Output

/usr/bin/ld: /home/exelotl/.cache/nim/main_d/@mmain.nim.c.o:(.bss+0x0): multiple definition of `myvar_u0__foo'; /home/exelotl/.cache/nim/main_d/@mfoo.nim.c.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
Error: execution of an external program failed: 'gcc   -o /home/exelotl/Dev/nim-tests/2026-07-16_19.09/main  /home/exelotl/.cache/nim/main_d/@psystem@sexceptions.nim.c.o /home/exelotl/.cache/nim/main_d/@pstd@sprivate@sdigitsutils.nim.c.o /home/exelotl/.cache/nim/main_d/@psystem@sdollars.nim.c.o /home/exelotl/.cache/nim/main_d/@psystem.nim.c.o /home/exelotl/.cache/nim/main_d/@mfoo.nim.c.o /home/exelotl/.cache/nim/main_d/@mmain.nim.c.o  -pthread -pthread   -ldl'

Expected Output

123

Known Workarounds

No response

Additional Information

Previously in the generated C code for main.nim, myvar would be declared as such:

extern NI myvar__foo_u1;

But now it is declared as:

__attribute__ ((aligned (16))) NI myvar_u0__foo = ((NI) 123);

which is the same as its definition in the generated C code for foo.nim, leading to multiple definition errors.

This issue also occurs if you don't use the variable directly but call an inline function that uses it.
It doesn't occur in the last stable release (2.2.10).
It only seems to happen for variables, i.e. codegenDecl procs are still working fine.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions