Can I embed go to my C/C++ project using dynamic linking? - c

Take python as example, a C/C++ program can load libpython.so dynamically and create a python VM, and then feed user scripts to the python VM.
Is their a way to use go like this? Namely, dynamically load it from the user's system environment.
We want to embed go to our project for scripting, but we don't want to package it to our final binary program.
Thanks.

On some platforms like Linux you can build your Go code with -buildmode=c-shared to get a shared library which can then be linked into a program written in another language like C.
Some examples and documentation are here: https://github.com/vladimirvivien/go-cshared-examples
Note that this does not make Go a scripting language--you still need to build shared libraries from Go code. However, you could conceivably build them while your application is running and load them using dlopen() and dlsym(). This is the same as how you could compile C code by launching a C compiler from inside your application, then load it.

Related

In a golang project building with gcc, how to statically link a library on macos?

I am making a very small application in golang that happens to encode mp3's. For that I am using the go-lame package that is a go binding to libmp3lame.
I want to distribute this small piece of app to my users without needing them to install anything more, so I want to statically build my project.
I can do that just fine on linux, passing the static flag to the gcc compiler.
But on macos (here on a M1 machine, but that is irrelevant) I cannot statically build the app as lcrt0.o is missing - something that is normal as I saw on SO, because you pretty much cannot statically link a program on macOS, there's going to be a couple of dynamic system lib forced on you.
For my use case this is fine, but i'd like lame to be statically linked and offer a nice distribution experience to the users.
So, here is my question : how can I build my project so that the compilers statically links libmp3lame in my executable but without statically link the other system libs.

Create a final product through CodeBlocks with C Language

I am a newbie programmer (more specifically a student) which use CodeBlocks to many exercises (as much as I can). At the moment I am following this steps to create my own programs (File->New->Project->Console Application). This modality has served to learn C language, but I want create a software which any user could use (for now at Windows). Is it possible create some portable through various computers? I was exploring the another option I have on IDE but I don't understand so much, therefore I am very lost.
I googled about different projects which I can do with language C, but...sincerely, I got even more confused. Therefore, what's the next step, to make a more robust program in C?
Thanks
Make a HelloWorld program.
Run it in codeblocks.
Now go to the folder containing that file. You will find three files there.
HelloWorld.c
HelloWorld.o
HelloWorld
The last file HelloWorld is the executable. You can share it to other windows OS to run it.
EDIT:
C is a portable language so its viable. Do look into makefile as you intend to create a program that uses GUI, network etc. (RECOMMENDED)
Or you might want to create platform specific ones. For example, creating a debian package from your executable

Wrap an executable in another executable

I would like to know if it is possible to include an executable file in another one, and then run it directly from there.
For example, if I am writing a GUI frontend to clprog.exe, I would want to have one file, guiprog.exe, that will run it's internal version of clprog.
Assume including the source of the wrapped program in the wrapper program is not an option.
I am more interested in this as a theoretical question, so answers applying to either windows or linux are fine (I am not familiar with other OSs), as well as using any language (C/Java/ASM/other, though I assume if it will be possible in any of these languages it will be ASM and maybe C, and obviously not Java)
First thought that comes to mind is a .NET solution.
If the external executable is a .NET assembly, you could embed it inside of your own project, and at run time load that into an in-memory assembly and execute using reflection.
If the embedded executable was built with .NET I think you would have to extract and temporarily save the executable, execute it as a separate process and then delete it, if you don't want to leave it's trace.

c exec embeded in webpage

Does anyone know for a tool that allows a c executable to be run in the browser? I'm looking for a javascript, java, or flash solution because I don't have privileges to run c executables on the server.
The executables are basic input and output programs.
Looking at your comments, I hear you mention students and running simple programs. As a suggestion, you might want to look into CodePad. This will let you interpret simple C programs. Note that everything needs to be in one place, so you'll have to combine C and header files.
Here is a sample:
http://codepad.org/qQS31BwM
EDIT
Here's another one I found:
http://ideone.com/
When you run the program, at the bottom there is a link for input. You can use it to run the program with given input as entered.
You could use this as a basis for solving the problem:
http://bellard.org/jslinux/
Emscripten is an LLVM-to-JavaScript compiler. It takes LLVM bitcode (which can be generated from C/C++, using llvm-gcc or clang, or any other language that can be converted into LLVM) and compiles that into JavaScript, which can be run on the web (or anywhere else JavaScript can run).
Using Emscripten, you can
Compile C and C++ code into JavaScript and run that on the web
Run code in languages like Python as well, by compiling CPython from C to JavaScript and interpreting code in that on the web

Is Extendible program in C possible?

I am looking into making a C program which is divided into a Core and Extensions. These extensions should allow the program to be extended by adding new functions. so far I have found c-pluff a plugin framework which claims to do the same. if anybody has any other ideas or reference I can check out please let me know.
You're not mentioning a platform, and this is outside the support of the language itself.
For POSIX/Unix/Linux, look into dlopen() and friends.
In Windows, use LoadLibrary().
Basically, these will allow you to load code from a platform-specific file (.so and .dll, respectively), look up addresses to named symbols/functions in the loaded file, and access/run them.
I tried to limit myself to the low-level stuff, but if you want to have a wrapper for both of the above, look at glib's module API.
The traditional way on windows is with DLLs. But this kind of obselete. If you want users to actually extend your program (as opposed to your developer team releasing official plugins) you will want to embed a scripting language like Python or Lua, because they are easier to code in.
You can extend your core C/C++ program using some script language, for example - Lua
There are several C/C++ - Lua integration tools (toLua, toLua++, etc.)
Do you need to be able to add these extensions to the running program, or at least after the executable file is created? If you can re-link (or even re-compile) the program after having added an extension, perhaps simple callbacks would be enough?
If you're using Windows you could try using COM. It requires a lot of attention to detail, and is kind of painful to use from C, but it would allow you to build extension points with well-defined interfaces and an object-oriented structure.
In this usage case, extensions label themselves with a 'Component Category' defined by your app, hwich allows the Core to find and load them withough havng to know where their DLLs are. The extensions also implement interfaces that are specified using IDL and are consumed by the core.
This is old tech now, but it does work.

Resources