amikos-tech / llamacpp-embedder

Llama.cpp embedding shared library
MIT License
0 stars 0 forks source link

[Go Binding] Windows dynamic function casting #46

Open tazarov opened 3 weeks ago

tazarov commented 3 weeks ago

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 commented 2 weeks ago

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);
}