LoginSignup
3
1

More than 5 years have passed since last update.

Nim と C の連携でエラー

Last updated at Posted at 2018-03-21

エラー

C で書いた関数を Nim で使うとき

add.c
int addInts(int a, int b) {
    return a + b;
}
add.nim
{.compile "add.c".}
proc addInts(a, b: cint): cint {.importc.}

when isMainModule:
    echo addInts(3, 7)

みたいにすると

$ nim c -r add.nim
Hint: used config file '/etc/nim.cfg' [Conf]
Hint: system [Processing]Hint: add [Processing]
CC: add
CC: add
CC: stdlib_system
Hint:  [Link]/home/username/Develop/Nim/nimcache/add.o: In function `PreMainInner':
add.c:(.text+0x1b): multiple definition of `PreMainInner'
/home/username/Develop/Nim/nimcache/add.o:add.c:(.text+0x1b): first defined here
/home/username/Develop/Nim/nimcache/add.o: In function `addDatInit000':
add.c:(.text+0x26a): multiple definition of `addDatInit000'
/home/username/Develop/Nim/nimcache/add.o:add.c:(.text+0x26a): first defined here
/home/username/Develop/Nim/nimcache/add.o: In function `PreMain':
add.c:(.text+0x2c): multiple definition of `PreMain'
/home/username/Develop/Nim/nimcache/add.o:add.c:(.text+0x2c): first defined here
/home/username/Develop/Nim/nimcache/add.o: In function `NimMainInner':
add.c:(.text+0x7c): multiple definition of `NimMainInner'
/home/username/Develop/Nim/nimcache/add.o:add.c:(.text+0x7c): first defined here
/home/username/Develop/Nim/nimcache/add.o: In function `NimMainModule':
add.c:(.text+0x1b1): multiple definition of `NimMainModule'
/home/username/Develop/Nim/nimcache/add.o:add.c:(.text+0x1b1): first defined here
/home/username/Develop/Nim/nimcache/add.o: In function `NimMain':
add.c:(.text+0x88): multiple definition of `NimMain'
/home/username/Develop/Nim/nimcache/add.o:add.c:(.text+0x88): first defined here
/home/username/Develop/Nim/nimcache/add.o: In function `main':
add.c:(.text+0xd8): multiple definition of `main'
/home/username/Develop/Nim/nimcache/add.o:add.c:(.text+0xd8): first defined here
/home/username/Develop/Nim/nimcache/add.o: In function `NimMainModule':
add.c:(.text+0x225): undefined reference to `addInts'
/home/username/Develop/Nim/nimcache/add.o: In function `NimMainModule':
add.c:(.text+0x225): undefined reference to `addInts'
collect2: error: ld returned 1 exit status
Error: execution of an external program failed: 'gcc   -o /home/username/Develop/Nim/add  /home/username/Develop/Nim/nimcache/add.o /home/username/Develop/Nim/nimcache/add.o /home/username/Develop/Nim/nimcache/stdlib_system.o    -ldl'

エラーが出る。

解決策

*.nim*.c の名前は同じにできないらしい。
add.c の名前を logic.c に、
add.nimcalc.nim に変え、

calc.nim
{.compile: "logic.c".}
proc addInts(a, b: cint): cint {.importc.}

when isMainModule:
    echo addInts(3, 7)

のように修正すると

$ nim c -r calc.nim
Hint: used config file '/etc/nim.cfg' [Conf]
Hint: system [Processing]
Hint: calc [Processing]
CC: logic
CC: calc
CC: stdlib_system
Hint:  [Link]
Hint: operation successful (11721 lines compiled; 0.821 sec total; 22.008MiB peakmem; Debug Build) [SuccessX]
Hint: /home/username/Develop/Nim/calc  [Exec]
10

動いた。

参考

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1