1

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.

J. Royal
  • 21
  • 3
  • 3
    Why would you want to do that as opposed to making the required functionality an integral part of the program? Furthermore, I think you have a bit of a misunderstanding about how functions work, how compilers/linkers work, and what header files are. – dandan78 Feb 20 '14 at 17:37

3 Answers3

4

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++?

Community
  • 1
  • 1
Alexey Polonsky
  • 1,141
  • 11
  • 12
0

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.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • I believe the poster was talking about downloading pre-compiled code. – Chris Stratton Mar 03 '14 at 19:56
  • I don't think that's accurate; OP says, "It would need to work on all desktop platforms (Windows, Mac, and Linux), so no .dll's." OP wants to "treat it as compiled," but that's impossible without pre-compiling (thus, `.so` or `.dll` files, which violates the questions parameters) or JIT compilation (which includes the two options I described). – elixenide Mar 03 '14 at 20:41
  • Not really true - with care, compiled binary code and traps for all services would probably work . But likely building it once for each platform and hosting that on a server is going to be the most realistic. The point is that there are options beyond compiling on the client or interpreting. – Chris Stratton Mar 03 '14 at 20:58
0

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.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55