简体   繁体   中英

Is it possible for a program written in C to download an external function and treat this external function as a compiled and linked shared object?

I am working on a program in C, and I am having trouble with libconfig.h. Because of this, I think if I could have my program download an external function from the Internet (using libcurl.h) and have my program treat it as a compiled and linked shared object, that would be perfect. It would need to work on all desktop platforms (Windows, Mac, and Linux), so no .dll's, and would have to be downloaded by the program, treated as a function, and then get deleted by the program. So, my question is: is that possible in C?

The reason that I need to download it separately is because the function would need to be updated regularly, and requiring the user to download a new version of the program regularly would defeat the purpose of the program.

Well the closest to what you ask for would be this

  1. Download .so/.dll using curl
  2. Dynamically load .so/.dll into your process
  3. set up function pointer in your process to point to a function in .so/.dll

On Windows :

HMODULE handle = LoadLibrary("mylib.dll");
if (handle)
    myfunc = GetProcAddress(handle, "myfunc");

To unload call

FreeLibrary(handle)

It decreases ref count, and the DLL is actually unloaded when ref count hits 0.

On Linux , check this post: How do I load a shared object in C++?

You can't just treat it as compiled; you would have to do one of two things:

  • Actually compile it on the fly, then load it as a dynamic library, which requires ensuring that there is a compiler on the system and will probably cause an unholy mess of errors on the user end.
  • Build your own C parser to interpret the external function, which is no small feat.

Far simpler solution: just write a function that works and compile platform-specific versions of it into your binary (or a library, if you prefer) before shipping the product.

You could link a Python interpreter into your program and have it execute a Python version of your function.

This approach would actually work with different languages, such as Java, Ruby, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM