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

Disable export of NimMain and NimDestroyGlobals #24471

Open
ashe27 opened this issue Nov 24, 2024 · 3 comments
Open

Disable export of NimMain and NimDestroyGlobals #24471

ashe27 opened this issue Nov 24, 2024 · 3 comments

Comments

@ashe27
Copy link

ashe27 commented Nov 24, 2024

Summary

For dynamic library projects, Nim enforces the export of the NimMain and NimDestroyGlobals functions. In my project, the exported functions have a unified naming convention and include their own initialization and unloading functions (MyInit, MyUninit). I would prefer to initialize within my own exported functions, but Nim does not provide me with this opportunity. Therefore, I have no choice but to proceed in the following way:

{.emit: """
# Forward declaration
N_LIB_EXPORT N_CDECL(void, NimMain)(void);
N_LIB_EXPORT N_CDECL(void, NimDestroyGlobals)(void);

# Proxy method
static void CallNimMain() { NimMain(); }
static void CallNimDestroyGlobals() { NimDestroyGlobals(); }
""".}

proc CallNimMain() {.importc, cdecl.}
proc CallNimDestroyGlobals() {.importc, cdecl.}

proc MyInit() {.exportc, dynlib.} =
  CallNimMain()
  # some code...

proc MyUninit() {.exportc, dynlib.} =
  # some code...
  CallNimDestroyGlobals()

Of course, I have seen such declarations in the community:

proc NimMain() {.cdecl, importc.}

However, it doesn't work in Nim 2.2.0. The generated C code doesn't have a forward declaration for NimMain.

Description

Give the choice of exports to the developers, and provide the opportunity to directly call NimMain and NimDestroyGlobals in the Nim code.

Alternatives

No response

Examples

No response

Backwards Compatibility

No response

Links

No response

@litlighilit
Copy link
Contributor

litlighilit commented Nov 30, 2024

I would prefer to initialize within my own exported functions, but Nim does not provide me with this opportunity.

Consider --nimMainPrefix:prefix?

doc:

--nimMainPrefix:prefix

use {prefix}NimMain instead of NimMain in the produced C/C++ code

(see https://nim-lang.org/docs/nimc.html)

@litlighilit
Copy link
Contributor

And IIRC, NimDestoryGlobals is currently not documented.

So rename NimMain via --nimMainPrefix:prefix may be a better choice

@ringabout
Copy link
Member

NimDestroyGlobals is documented in https://nim-lang.org/docs/backends.html

ringabout added a commit that referenced this issue Dec 3, 2024
ref #24471

---------

Co-authored-by: metagn <metagngn@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants