diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 1a9c3cc3a46a..f4d71876ec71 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -1361,9 +1361,8 @@ proc genMainProc(m: BModule) = "$1" & "}$N$N" - NimMainProc = - "N_LIB_WEAK N_CDECL(void, NimMain)(void) /*zook1*/ {$N" & + "N_LIB_WEAK_MAYBE N_CDECL(void, NimMain)(void) /*zook1*/ {$N" & "\tvoid (*volatile inner)(void);$N" & "$4" & "\tinner = NimMainInner;$N" & @@ -1856,7 +1855,7 @@ proc writeHeader(m: BModule) = if optGenDynLib in m.config.globalOptions: result.add("N_LIB_IMPORT ") - result.addf("N_LIB_PRIVATE N_CDECL(void, NimMain)(void);$n", []) + result.addf("N_LIB_WEAK_MAYBE N_CDECL(void, NimMain)(void);$n", []) if m.config.cppCustomNamespace.len > 0: result.add closeNamespaceNim() result.addf("#endif /* $1 */$n", [guard]) if not writeRope(result, m.filename): diff --git a/lib/nimbase.h b/lib/nimbase.h index 58f24af6ff58..2529bc0b095c 100644 --- a/lib/nimbase.h +++ b/lib/nimbase.h @@ -177,13 +177,12 @@ __AVR__ // semantics differ a bit from __attribute__((weak)), // see https://stackoverflow.com/questions/2290587/gcc-style-weak-linking-in-visual-studio // for alternative based on `/alternatename:` if this isn't enough. -# define N_LIB_WEAK __declspec(selectany) -# define N_LIB_PRIVATE +# define N_LIB_WEAK_MAYBE __declspec(selectany) # else -# define N_LIB_WEAK -# define N_LIB_PRIVATE +# define N_LIB_WEAK_MAYBE # endif +# define N_LIB_PRIVATE N_LIB_WEAK_MAYBE # define N_CDECL(rettype, name) rettype __cdecl name # define N_STDCALL(rettype, name) rettype __stdcall name # define N_SYSCALL(rettype, name) rettype __syscall name @@ -205,19 +204,17 @@ __AVR__ # endif # define N_LIB_EXPORT_VAR __declspec(dllexport) # define N_LIB_IMPORT extern __declspec(dllimport) + #else -# define N_LIB_WEAK __attribute__((weak)) -// # define N_LIB_PRIVATE N_LIB_WEAK -// # define N_LIB_PRIVATE __attribute__((visibility("hidden"))) -// # define N_LIB_PRIVATE __attribute__((visibility("hidden"))) __attribute__((weak)) -// # define N_LIB_PRIVATE __attribute__((weak)) + # ifdef NIM_nimLinkerWeakSymbols -# define N_LIB_WEAK __attribute__((weak)) -# define N_LIB_PRIVATE __attribute__((weak)) __attribute__((visibility("hidden"))) +# define N_LIB_WEAK_MAYBE __attribute__((weak)) # else -# define N_LIB_WEAK -# define N_LIB_PRIVATE __attribute__((visibility("hidden"))) +# define N_LIB_WEAK_MAYBE # endif + +# define N_LIB_PRIVATE N_LIB_WEAK_MAYBE __attribute__((visibility("hidden"))) + # if defined(__GNUC__) # define N_CDECL(rettype, name) rettype name # define N_STDCALL(rettype, name) rettype name diff --git a/tests/misc/tapp_lib_staticlib.nim b/tests/misc/tapp_lib_staticlib.nim index 92c9acbc306c..b840f656c9fd 100644 --- a/tests/misc/tapp_lib_staticlib.nim +++ b/tests/misc/tapp_lib_staticlib.nim @@ -3,6 +3,7 @@ joinable: false """ # bug #16949 +# bug #15955 when defined case1: proc foo(): int {.exportc.} = 10 @@ -20,8 +21,10 @@ else: file = currentSourcePath nim = getCurrentCompilerExe() mode = querySetting(backend) + proc test(lib, options: string) = runCmd fmt"{nim} {mode} -o:{lib} --nomain {options} -f {file}" - # runCmd fmt"{nim} r -b:{mode} --passl:{lib} -d:caseMain -f {file}" # pending https://github.com/nim-lang/Nim/pull/16945 + runCmd fmt"{nim} r -b:{mode} --passl:{lib} -d:caseMain -f {file}" + test(buildDir / "libD20210205T172314.a", "--app:staticlib -d:nimLinkerWeakSymbols -d:case1") test(buildDir / DynlibFormat % "D20210205T172720", "--app:lib -d:case2")