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

[Go Binding] Windows dynamic function casting #46

Open
tazarov opened this issue Sep 14, 2024 · 1 comment
Open

[Go Binding] Windows dynamic function casting #46

tazarov opened this issue Sep 14, 2024 · 1 comment
Labels
bug Something isn't working cpp Golang

Comments

@tazarov
Copy link
Contributor

tazarov commented Sep 14, 2024

The following warnings are visibile in logs:

wrapper.cpp:85:27: warning: cast between incompatible function types from 'FARPROC' {aka 'long long int (*)()'} to 'init_embedder_local_func' {aka 'llama_embedder* (*)(const char*, unsigned int)'} [-Wcast-function-type]
   85 |         init_embedder_f = (init_embedder_local_func) GetProcAddress(libh, "init_embedder");
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wrapper.cpp:99:27: warning: cast between incompatible function types from 'FARPROC' {aka 'long long int (*)()'} to 'free_embedder_local_func' {aka 'void (*)(llama_embedder*)'} [-Wcast-function-type]
   99 |         free_embedder_f = (free_embedder_local_func) GetProcAddress(libh, "free_embedder");
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wrapper.cpp:113:19: warning: cast between incompatible function types from 'FARPROC' {aka 'long long int (*)()'} to 'embed_c_local_func' {aka 'FloatMatrix (*)(llama_embedder*, const char**, long long unsigned int, int)'} [-Wcast-function-type]
  113 |         embed_f = (embed_c_local_func) GetProcAddress(libh, "embed_c");
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wrapper.cpp:127:26: warning: cast between incompatible function types from 'FARPROC' {aka 'long long int (*)()'} to 'get_metadata_c_local_func' {aka 'int (*)(llama_embedder*, MetadataPair**, long long unsigned int*)'} [-Wcast-function-type]
  127 |         get_metadata_f = (get_metadata_c_local_func) GetProcAddress(libh, "get_metadata_c");
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wrapper.cpp:142:27: warning: cast between incompatible function types from 'FARPROC' {aka 'long long int (*)()'} to 'free_metadata_c_local_func' {aka 'void (*)(MetadataPair*, long long unsigned int)'} [-Wcast-function-type]
  142 |         free_metadata_f = (free_metadata_c_local_func) GetProcAddress(libh, "free_metadata_c");
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

should be fixed with something like:

        get_metadata_f = reinterpret_cast<get_metadata_c_local_func>(GetProcAddress(libh, "get_metadata_c"));

Note: This may also be needed in the unix version

@tazarov tazarov added bug Something isn't working cpp labels Sep 14, 2024
@tazarov tazarov self-assigned this Sep 14, 2024
@tazarov tazarov added this to the Python release 0.0.8 milestone Sep 14, 2024
@tazarov tazarov added the Golang label Sep 14, 2024
@tazarov
Copy link
Contributor Author

tazarov commented Sep 15, 2024

we can try this:

template<typename T>
T get_function_address(lib_handle handle, const char* func_name) {
#if defined(_WIN32) || defined(_WIN64)
    return reinterpret_cast<T>(GetProcAddress(handle, func_name));
#else
    return reinterpret_cast<T>(dlsym(handle, func_name));
#endif
}

// Usage:
init_embedder_f = get_function_address<init_embedder_local_func>(libh, "init_embedder");
if (!init_embedder_f) {
#if defined(_WIN32) || defined(_WIN64)
    std::string error_message = "Failed to load init_embedder function: " + GetLastErrorAsString();
#else
    std::string error_message = "Failed to load init_embedder function: " + std::string(dlerror());
#endif
    throw std::runtime_error(error_message);
}

@tazarov tazarov removed their assignment Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cpp Golang
Projects
None yet
Development

No branches or pull requests

1 participant