-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Static linking with a .lib file not working #15955
Comments
One workaround is to instruct the linker to ignore the multiple definitions. {.passL:"libfoo.a -Wl,--allow-multiple-definition".}
... |
…ing weak symbols
…ing weak symbols
…ing weak symbols
yes, see why here https://nim-lang.github.io/Nim/backends.html#interfacing-backend-code-calling-nim
pass EDIT: works on posix, not yet on windows |
Works on Linux with a warning on the C code generated :
I'll see if I can test on Windows later |
…ing weak symbols
…ing weak symbols
Commit Windows doesn't compile on the other hand. I used gcc.exe from MinGW. Here are the versions :
The error message on powershell is rather long so I put it into a text file :
Here's a preview because it basically repeat the same message on different places :
Hope that helps you ! |
I am trying to let xmake support building nim projects, xmake-io/xmake#1756 I also encountered a similar problem when compiling and generating static libraries. I also very much hope that this problem can be resolved. But I now have a workaround solution, which is temporarily compatible and fixed, and I can successfully link the static library. we can set function buildargv(self, sourcefiles, targetkind, targetfile, flags)
local flags_extra = {}
if targetkind == "static" then
-- fix multiple definition of `NimMain', it is only workaround solution
-- we need to wait for this problem to be resolved
--
-- @see https://github.com/nim-lang/Nim/issues/15955
local uniquekey = hash.uuid(targetfile):split("-", {plain = true})[1]
table.insert(flags_extra, "--passC:-DNimMain=NimMain_" .. uniquekey)
table.insert(flags_extra, "--passC:-DNimMainInner=NimMainInner_" .. uniquekey)
table.insert(flags_extra, "--passC:-DNimMainModule=NimMainModule_" .. uniquekey)
table.insert(flags_extra, "--passC:-DPreMain=PreMain_" .. uniquekey)
table.insert(flags_extra, "--passC:-DPreMainInner=PreMainInner_" .. uniquekey)
end
return self:program(), table.join("c", flags, flags_extra, "-o:" .. targetfile, sourcefiles)
end Now can we successfully compile and link it. $ xmake -v
[ 33%]: linking.release libfoo.a
/usr/local/bin/nim c --opt:speed --nimcache:build/.gens/foo/macosx/x86_64/release/nimcache --app
:staticlib --noMain --passC:-DNimMain=NimMain_B6D5BD02 --passC:-DNimMainInner=NimMainInner_B6D5B
D02 --passC:-DNimMainModule=NimMainModule_B6D5BD02 --passC:-DPreMain=PreMain_B6D5BD02 --passC:-D
PreMainInner=PreMainInner_B6D5BD02 -o:build/macosx/x86_64/release/libfoo.a src/foo.nim
[ 66%]: linking.release test
/usr/local/bin/nim c --opt:speed --nimcache:build/.gens/test/macosx/x86_64/release/nimcache --pa
ssL:-Lbuild/macosx/x86_64/release --passL:-lfoo -o:build/macosx/x86_64/release/test src/main.nim
[100%]: build ok! |
I'm having the same issue and seems like the nim developers do not indent to resolve this (I guess it is more or less a linking issue rather than nim issue). Such a bummer I cannot use nim to generate a static binary library where other nim developer can use it. |
The issue is open and PRs to fix it are welcome. |
@timotheecour had commits that fixed this if I remember correctly. Maybe his work could be merged? |
I am also hitting this, I have a static archive generated with the CPP backend that externs some functions to be used with a program written with the C backend, both have NimMain, PreMain etc and so fails to link the executable. I can use |
Got a link handy? |
… [backport:1.6] (nim-lang#19235)
related: #19830 It wasn't fixed. It is still broken with 1.6.6. |
Compiling to CPP with a namespace addressed the immediate problem. The real fix as mentioned in the linked issue is to have the compiler add prefixes to visible symbols. However looking through the compiler this seems like a daunting change. |
Expand `--nimMainPrefix` semantics to include prefixing these symbols for the C, C++ and ObjC backends: * `cmdCount` * `cmdLine` * `gEnv` * `PreMain` * `PreMainInner` * `NimMainInner` * `NimMainModule` Fixes nim-lang#15955 Fixes nim-lang#19830
If i statically link a nim application with a .lib file, i get an error output.
Example
foo.nim
Build with
nim c --app:staticlib foo.nim
to generate the "foo.lib" file.main.nim
Build and run with
nim c -r main.nim
Current Output
Expected Output
Possible Solution
Because of the error "mulitple definition of" the nim compiler should exclude runtime specific code in libraries. But i could also not link to a static .lib of a third party library. There could be a general problem with static linking.
Additional Information
Tested with Nim version 1.5.1, Windows 10 and reinstalled MingW.
I firstly reported this problem here: https://stackoverflow.com/questions/64677151/nim-use-a-static-library
Then here: https://forum.nim-lang.org/t/7080.
I tried the parameter
--dynlibOverride:foo
and--noMain:on
and also the pragma {.link: "foo.lib", passc:"-L foo".}
If i add the dynlib pragma to both files and create a .dll file with
nim c --app:lib foo.nim
it just works.Thanks for every help.
The text was updated successfully, but these errors were encountered: