2

In my .nim code, I'm using the header pragma to include symbols from /usr/local/include/node/node_api.h (which then includes /usr/local/include/node/js_native_api.h).

proc napi_create_function(
  env: napi_env, 
  utf8name: cstring, 
  length: csize_t, 
  cb: napi_callback, 
  data: pointer, 
  res: napi_value
): int {.header:"<node/node_api.h>".}

When I run nim c foo.nim, I get Undefined symbols for architecture x86_64 for symbols in js_native_api.h (eg: napi_create_function), but the symbols in node_api.h are found by the linker. Remember that node_api.h includes js_native_api.h (as seen here).

Undefined symbols for architecture x86_64:
  "_napi_create_function", referenced from:
      _createFn__NEWhgHCwqbksHULYRnxXfA in @m..@s..@s..@s.nimble@spkgs@snapibindings-0.1.0@snapibindings.nim.c.o

The root problem likely isn't related to Nim, but I don't know how to use clang to check if the problem is reproducible without Nim.

So my question is:
How do I get the linker to find the missing symbols?

Versions

  • nim v1.4.8
  • clang v12.0.0
  • x86_64-apple-darwin19.6.0
  • nodejs v14.13.1 (installed with Homebrew into /usr/local/Cellar/node/14.13.1)

nim c

/Users/alec/.nimble/bin/nim
  c
  --colors:on
  --noNimblePath
  -d:NimblePkgVersion=0.1.0
  --path:/Users/alec/.nimble/pkgs/nimdbx-0.4.1
  --path:/Users/alec/.nimble/pkgs/nimterop-0.6.13
  --path:/Users/alec/.nimble/pkgs/regex-0.19.0
  --path:/Users/alec/.nimble/pkgs/unicodedb-0.9.0
  --path:/Users/alec/.nimble/pkgs/cligen-1.5.4
  --path:/Users/alec/.nimble/pkgs/cbor-0.6.0
  --path:/Users/alec/.nimble/pkgs/napibindings-0.1.0
  --path:'/Users/alec/.nimble/pkgs/docopt-#master'
  --path:/Users/alec/.nimble/pkgs/regex-0.19.0
  --path:/Users/alec/.nimble/pkgs/unicodedb-0.9.0
  --hints:off
  -o:/Users/alec/my-project/dist/foo
  /Users/alec/my-project/foo.nim

clang

clang
  -o
  /Users/alec/my-project/foo
  /Users/alec/.cache/nim/foo_d/stdlib_assertions.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_dollars.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_formatfloat.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_io.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_system.nim.c.o
  /Users/alec/.cache/nim/foo_d/@m..@s..@s..@s.nimble@spkgs@snapibindings-0.1.0@snapibindings@sutils.nim.c.o
  /Users/alec/.cache/nim/foo_d/@m..@s..@s..@s.nimble@spkgs@snapibindings-0.1.0@snapibindings.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_parseutils.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_math.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_unicode.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_strutils.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_posix.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_options.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_times.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_os.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_hashes.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_tables.nim.c.o
  /Users/alec/.cache/nim/foo_d/@m..@s..@s..@s.nimble@spkgs@snimterop-0.6.13@snimterop@sglobals.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_streams.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_lexbase.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_parsejson.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_json.nim.c.o
  /Users/alec/.cache/nim/foo_d/stdlib_cpuinfo.nim.c.o
  /Users/alec/.cache/nim/foo_d/@m..@s..@s..@s.nimble@spkgs@snimdbx-0.4.1@snimdbx@sprivate@slibmdbx.nim.c.o
  /Users/alec/.cache/nim/foo_d/@m..@s..@s..@s.nimble@spkgs@snimdbx-0.4.1@snimdbx@sprivate@svals.nim.c.o
  /Users/alec/.cache/nim/foo_d/@m..@s..@s..@s.nimble@spkgs@snimdbx-0.4.1@snimdbx@sError.nim.c.o
  /Users/alec/.cache/nim/foo_d/@m..@s..@s..@s.nimble@spkgs@snimdbx-0.4.1@snimdbx@sDatabase.nim.c.o
  /Users/alec/.cache/nim/foo_d/@m..@s..@s..@s.nimble@spkgs@snimdbx-0.4.1@snimdbx@sData.nim.c.o
  /Users/alec/.cache/nim/foo_d/@m..@s..@s..@s.nimble@spkgs@snimdbx-0.4.1@snimdbx@sCollection.nim.c.o
  /Users/alec/.cache/nim/foo_d/@m..@s..@s..@s.nimble@spkgs@snimdbx-0.4.1@snimdbx@sTransaction.nim.c.o
  /Users/alec/.cache/nim/foo_d/@m..@s..@s..@s.nimble@spkgs@snimdbx-0.4.1@snimdbx@sCRUD.nim.c.o
  /Users/alec/.cache/nim/foo_d/@mfoopkg@sdata.nim.c.o
  /Users/alec/.cache/nim/foo_d/@mfoopkg@sdata@sfrom_json.nim.c.o
  /Users/alec/.cache/nim/foo_d/@mfoopkg@sdata@sto_json.nim.c.o
  /Users/alec/.cache/nim/foo_d/@mfoopkg@squery.nim.c.o
  /Users/alec/.cache/nim/foo_d/@m..@s..@s..@s.nimble@spkgs@scbor-0.6.0@scbor.nim.c.o
  /Users/alec/.cache/nim/foo_d/@mfoopkg@sdata@sfrom_cbor.nim.c.o
  /Users/alec/.cache/nim/foo_d/@mfoopkg@sdata@sto_cbor.nim.c.o
  /Users/alec/.cache/nim/foo_d/@mfoopkg@sref.nim.c.o
  /Users/alec/.cache/nim/foo_d/@mfoopkg@squery@sdocument.nim.c.o
  /Users/alec/.cache/nim/foo_d/@mfoopkg@sfunctions.nim.c.o
  /Users/alec/.cache/nim/foo_d/@mfoopkg@seval.nim.c.o
  /Users/alec/.cache/nim/foo_d/@mfoo.nim.c.o
  -lm
  /Users/alec/.nimble/pkgs/nimdbx-0.4.1/libmdbx-dist/libmdbx.a
  -ldl
aleclarson
  • 18,087
  • 14
  • 64
  • 91
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – 273K Jan 01 '23 at 01:33

1 Answers1

0

So my question is:
How do I get the linker to find the missing symbols?

  1. make sure you are actually linking a library (statically or dynamically) which holds symbols you need. (please show how you link it.)
  2. make sure your library actually HAS correct symbols (open file with hex editor and search for symbols)
  3. make sure this library is of correct architecture. there are tools that let you check this. (on Windows it's dumpbin /headers file)
  4. make sure you are importing it correctly. (i see only {.header.} pragma but others needed pragmas are absent). please show more code and command lines. so we can investigate further.