Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nim: broken liblinkage #3273

Closed
kassane opened this issue Jan 10, 2023 · 7 comments
Closed

Nim: broken liblinkage #3273

kassane opened this issue Jan 10, 2023 · 7 comments
Labels
Milestone

Comments

@kassane
Copy link

kassane commented Jan 10, 2023

Xmake Version

2.7.5

Operating System Version and Architecture

ArchLinux

Describe Bug

When creating an initial example of a dynamic or static library and trying to run it, an error occurs when linking the library to the executable to be generated.
The purpose of the project was to experiment with C++ interop with nim and later to use c2nim (similar to cxxbridge in rust).
However, this error prevents me from continuing the experiment.

Expected Behavior

Output:

$> xmake run
[...] build process...
1

Project Configuration

add_rules("mode.debug", "mode.release")

target("interopCpp")
    set_kind("shared")
    add_files("src/foo.nim")

target("interopCpp_demo")
    set_kind("binary")
    add_deps("interopCpp")
    add_files("src/main.nim")

Structure:

.
├── build
│  └── linux
│     └── x86_64
│        └── release
│           └── libinteropCpp.so
├── src
│  ├── foo.nim
│  └── main.nim
└── xmake.lua

Additional Information and Error Logs

$> xmake -vD
[ 33%]: linking.release libinteropCpp.so
/usr/bin/nim c -d:release --nimcache:build/.gens/interopCpp/linux/x86_64/release/nimcache --app:lib --noMain --passL:-s -o:build/linux/x86_64/release/libinteropCpp.so src/foo.nim
[ 66%]: linking.release interopCpp_demo
/usr/bin/nim c -d:release --nimcache:build/.gens/interopCpp_demo/linux/x86_64/release/nimcache --passL:-Lbuild/linux/x86_64/release --passL:-s --passL:-linteropCpp -o:build/linux/x86_64/release/interopCpp_demo src/main.nim
error: @programdir/modules/private/async/runjobs.lua:256: @programdir/rules/nim/build/target.lua:67: @programdir/core/sandbox/modules/os.lua:268: Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: used config file '/etc/nim/config.nims' [Conf]
.........................................................
CC: ../../../../../../../usr/lib/nim/std/private/digitsutils.nim
CC: ../../../../../../../usr/lib/nim/system/dollars.nim
CC: ../../../../../../../usr/lib/nim/system/io.nim
CC: ../../../../../../../usr/lib/nim/system.nim
CC: main.nim
Hint:  [Link]
/usr/bin/ld: /home/kassane/Documentos/Projetos/Nim/interopCpp/build/.gens/interopCpp_demo/linux/x86_64/release/nimcache/@mmain.nim.c.o: na função "NimMainInner":
@mmain.nim.c:(.text+0x37): referência não definida para "foo"
/usr/bin/ld: /home/kassane/Documentos/Projetos/Nim/interopCpp/build/.gens/interopCpp_demo/linux/x86_64/release/nimcache/@mmain.nim.c.o: na função "NimMainModule":
@mmain.nim.c:(.text+0x177): referência não definida para "foo"
collect2: error: ld returned 1 exit status
Error: execution of an external program failed: 'gcc   -o /home/kassane/Documentos/Projetos/Nim/interopCpp/build/linux/x86_64/release/interopCpp_demo  /home/kassane/Documentos/Projetos/Nim/interopCpp/build/.gens/interopCpp_demo/linux/x86_64/release/nimcache/@m..@s..@s..@s..@s..@s..@s..@susr@slib@snim@sstd@sprivate@sdigitsutils.nim.c.o /home/kassane/Documentos/Projetos/Nim/interopCpp/build/.gens/interopCpp_demo/linux/x86_64/release/nimcache/@m..@s..@s..@s..@s..@s..@s..@susr@slib@snim@ssystem@sdollars.nim.c.o /home/kassane/Documentos/Projetos/Nim/interopCpp/build/.gens/interopCpp_demo/linux/x86_64/release/nimcache/@m..@s..@s..@s..@s..@s..@s..@susr@slib@snim@ssystem@sio.nim.c.o /home/kassane/Documentos/Projetos/Nim/interopCpp/build/.gens/interopCpp_demo/linux/x86_64/release/nimcache/@m..@s..@s..@s..@s..@s..@s..@susr@slib@snim@ssystem.nim.c.o /home/kassane/Documentos/Projetos/Nim/interopCpp/build/.gens/interopCpp_demo/linux/x86_64/release/nimcache/@mmain.nim.c.o   -Lbuild/linux/x86_64/release -s -linteropCpp  -ldl -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now'

stack traceback:
    [C]: in function 'error'
    [@programdir/core/base/os.lua:898]:
    [@programdir/core/sandbox/modules/os.lua:268]: in function 'runv'
    [@programdir/modules/core/tools/nim.lua:163]:
    [C]: in function 'xpcall'
    [@programdir/core/base/utils.lua:280]:
    [@programdir/rules/nim/build/target.lua:67]: in function 'build_sourcefiles'
    [@programdir/rules/nim/build/target.lua:83]:
    [@programdir/actions/build/build.lua:52]: in function 'jobfunc'
    [@programdir/modules/private/async/runjobs.lua:232]:
    [C]: in function 'xpcall'
    [@programdir/core/base/utils.lua:280]: in function 'trycall'
    [@programdir/core/sandbox/modules/try.lua:117]: in function 'try'
    [@programdir/modules/private/async/runjobs.lua:218]: in function 'cotask'
    [@programdir/core/base/scheduler.lua:404]:

stack traceback:
        [C]: in function 'error'
        @programdir/core/base/os.lua:898: in function 'os.raiselevel'
        (...tail calls...)
        @programdir/modules/private/async/runjobs.lua:256: in field 'catch'
        @programdir/core/sandbox/modules/try.lua:123: in global 'try'
        @programdir/modules/private/async/runjobs.lua:218: in upvalue 'cotask'
        @programdir/core/base/scheduler.lua:404: in function <@programdir/core/base/scheduler.lua:397>
@kassane kassane added the bug label Jan 10, 2023
@kassane kassane changed the title Nim Link Nim: broken liblinkage Jan 10, 2023
@waruqi
Copy link
Member

waruqi commented Jan 10, 2023

This is supposed to be a linking problem with nim in the new version and I don't know how to fix it at the moment.

@kassane
Copy link
Author

kassane commented Jan 10, 2023

My tests with shared library need dynlib.:

Library

proc foo(n: cint): cint {.cdecl, exportc, dynlib.} =

App

proc foo(n: cint): cint {.cdecl, importc, dynlib.} =

or

proc foo(n: cint): cint {.cdecl, importc, dynlib: "path/lib.so"} =
make -vD
[ 33%]: linking.release libinteropCpp.so
/usr/bin/nim c -d:release --nimcache:build/.gens/interopCpp/linux/x86_64/release/nimcache --app:lib --noMain --passL:-s -o:build/linux/x86_64/release/libinteropCpp.so src/foo.nim
[ 66%]: linking.release interopCpp_demo
/usr/bin/nim c -d:release --nimcache:build/.gens/interopCpp_demo/linux/x86_64/release/nimcache --passL:-Lbuild/linux/x86_64/release --passL:-s --passL:-linteropCpp -o:build/linux/x86_64/release/interopCpp_demo src/main.nim

build cache stats:
cache directory: build/.build_cache
cache hit rate: 0%
cache hit: 0
cache miss: 0
new cached files: 0
remote cache hit: 0
remote new cached files: 0
preprocess failed: 0
compile fallback count: 0

[100%]: build ok!

@kassane
Copy link
Author

kassane commented Jan 10, 2023

This is supposed to be a linking problem with nim in the new version and I don't know how to fix it at the moment.

Fix:

Worked for me too!!

waruqi added a commit that referenced this issue Jan 11, 2023
@waruqi
Copy link
Member

waruqi commented Jan 11, 2023

This is supposed to be a linking problem with nim in the new version and I don't know how to fix it at the moment.

Fix:

Worked for me too!!

thanks, it works now. xmake update -s dev

@waruqi
Copy link
Member

waruqi commented Jan 11, 2023

My tests with shared library need dynlib.:

Library

proc foo(n: cint): cint {.cdecl, exportc, dynlib.} =

App

proc foo(n: cint): cint {.cdecl, importc, dynlib.} =

or

proc foo(n: cint): cint {.cdecl, importc, dynlib: "path/lib.so"} =
make -vD
[ 33%]: linking.release libinteropCpp.so
/usr/bin/nim c -d:release --nimcache:build/.gens/interopCpp/linux/x86_64/release/nimcache --app:lib --noMain --passL:-s -o:build/linux/x86_64/release/libinteropCpp.so src/foo.nim
[ 66%]: linking.release interopCpp_demo
/usr/bin/nim c -d:release --nimcache:build/.gens/interopCpp_demo/linux/x86_64/release/nimcache --passL:-Lbuild/linux/x86_64/release --passL:-s --passL:-linteropCpp -o:build/linux/x86_64/release/interopCpp_demo src/main.nim

build cache stats:
cache directory: build/.build_cache
cache hit rate: 0%
cache hit: 0
cache miss: 0
new cached files: 0
remote cache hit: 0
remote new cached files: 0
preprocess failed: 0
compile fallback count: 0

[100%]: build ok!

it works for me, what's problem?

@waruqi waruqi added this to the v2.7.6 milestone Jan 11, 2023
@kassane
Copy link
Author

kassane commented Jan 11, 2023

it works for me, what's problem?

Shared library in the tests, the application does not need the dynlib keyword, only the library.
Because dynlib requires only exportc and not importc.

If you correct this, you can close the issue!

@waruqi
Copy link
Member

waruqi commented Jan 11, 2023

it works for me, what's problem?

Shared library in the tests, the application does not need the dynlib keyword, only the library. Because dynlib requires only exportc and not importc.

If you correct this, you can close the issue!

no dylib in main.nim

ruki-2:xmake ruki$ rg dynlib tests/projects/nim/shared_library/
tests/projects/nim/shared_library/src/foo.nim
1:proc foo(n: int): int {.cdecl, exportc, dynlib.} =

https://github.com/xmake-io/xmake/blob/master/tests/projects/nim/shared_library/src/main.nim

@waruqi waruqi closed this as completed Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants