How to Obtain Ruby Library for C (MinGW-w64/Windows) - c

Currently, I am trying to run Ruby in a C program. However, I am at a complete loss on where to begin.
What I'm trying to accomplish is outlined in this really helpful guide:
http://silverhammermba.github.io/emberb/embed/
However, it starts with using a gcc command that has -lruby within it. How may I obtain the Ruby library for compiling? (And specifically have it be for MinGW-w64).
I have tried using RubyInstaller (https://github.com/oneclick/rubyinstaller2), but still cannot find anyway to obtain a library.
I've also tried compiling the source from the Ruby Github (https://github.com/ruby/ruby), but once again, no luck.
Any detailed explanations for an idiot like me would be appreciated. ^^

Related

switches in Triangle program

A weeks ago, I started learning about the Triangle program, which is used to generate meshes.
I attach the link from where you can download its zip file and read about it:
http://www.cs.cmu.edu/~quake/triangle.html
I am having trouble running the example using in Linux Ubuntu, which is supposed to be run trivially.
In particular, I don't understand what are exactly the switches that are mentioned in the makefile.
How exactly am I supposed to choose the optimization using switches? Do I need to modify the C source code or is something I need to specify before compiling it on Linux terminal somehow?
Besides, I am required to use the -DLINUX switches in order for arithmetics to run well. I would also need to know where I need to specify this.
If it helps, I am getting errors in the terminal notifying me that there are: "Undefined reference to sqrt" and " "Undefined reference to cos".
I really need help on this because my understanding on Linux and C is quite weak.
Thanks very much.

Using make to compile code in c in terminal on Mac

I'm trying to learn to program in C and I'm getting used to compiling the code after writing it. I'm actually using K&R 2nd edition to start off and they use 'cc' to compile. I originally started using Zed Shaw's Learn C The Hard Way but I've stopped because I haven't heard many good things about it. Zed Shaw's way was to use 'make' to compile code, at least in the beginning.
Is there any technical difference between using 'make' and using 'cc' that I should be concerned about right now?
make simply gives commands to the compiler, such as cc. It automates building a project which has multiple files. To begin, you do not need to concern yourself with those details. Building a single file directly with cc is fine. In fact, you will understand how the compiler works so that you can get the most out of make when you eventually learn it.
I agree with code-apprentice. I would just add that it is important to have a good understanding of different cc command line options before moving on to make. When you reach the point of a more complexe compilation project and the build process of your final binary becomes a pain, that's when you can transition to learning Make!

Minimal working example of mixed Julia and C project on Windows

I'm looking for a simple, minimal working example for a Julia project that includes some C code as part of the project. I need the project to compile the C code into a *.dll and then have some Julia code that can successfully call a function from that *.dll on Windows.
I'm having a lot of trouble getting this to work on Windows, and it would be really helpful to just start from some example that I know actually works, and troubleshoot from there why my larger project isn't working.
My use case is calling the portaudio library for low-latency presentation of sounds, using a lock-less multi-threading model. This isn't really possible to write in julia at this point, that I know of: I really need a system level language like C. But I'd like the rest of the project to be in Julia.
This blog post contains pretty much a minimal working example. It compiles a small linker code to GSL and then writes about 10 lines of Julia to use it. It even in that space shows how to pass a Julia-defined function to C.
This link is not a perfect example, but it's very very close. I haven't run this code, but the poster claims that it compiles and runs successfully on Windows.

How can I inject or dynamically load an c function into another c program

I want to build an interface in a c program which is running on an embedded system. This should accept some bytecode that represents a c function. This code will then be loaded into the memory and executed. This will then be something like remotely inject code into a running app. The only difference here is that i can implement, or change the running code and provide an interface.
The whole thing should be used to inject test code on a target system.
My current problem is that I do not know how to build such a byte code out of an existing c function. Mapping and executing this is no problem if I would knew the start address of the function.
Currently I am working with Ubuntu for testing purposes, this allows me to try some techniques which are not possible in the embedded system (according to missing operating system libs).
I build an shared object and used dlopen() and dlsym() to run this function. This works fine, the problem is just that i do not have such functions in the embedded system. I read something about loading a shared object into memory and run it, but i could not find examples for that. (see http://www.nologin.org/Downloads/Papers/remote-library-injection.pdf)
I also took a simple byte code that just print hello world in stdout. I stored this code in memory using mmap() and execute it. This also worked fine. Here the problem is that I don't know how to create such a byte code, I just used an hello world example form the internet. (see https://www.daniweb.com/programming/software-development/threads/353077/store-binary-code-in-memory-then-execute-it)
I also found something here: https://stackoverflow.com/a/12139145/2479996 which worked very well. But here i need a additional linker script, already for such a simple program.
Further I looked at this post: https://stackoverflow.com/a/9016439/2479996
According to that answer my problem would be solved with the "X11 project".
But I did not really find much about that, maybe some of you can provide me a link.
Is there another solution to do that? Did I miss something? Or can someone provide me another solution to this?
I hope I did not miss something.
Thanks in advance
I see no easy solution. The closest that I am aware of is GCC's JIT backend (libgccjit). Here is a blog post about it.
As an alternative, you could using a scripting language for that code that needs to be injected. For instance, ChaiScript or Lua. In this question, there is a summary of options. As you are on an embedded device, the overhead might be significant, though.
If using an LLVM based backend instead of GCC is possible, you can have a look at Cling. It is a C++ interpreter based on LLVM and Clang. In my personal experience, it was not always stable, but it is used in production in CERN. I would except that the dynamic compilation features are more advanced in LLVM than in GCC.

How to make use of Clang's AST?

I am looking at making use of the Clang's AST for my C code and do some analysis over the AST. Some pointers on where to start, how to obtain the Clang's AST, tutorials or anything in this regard will be of great help!!!
I have been trying to find some and I got this link which was created 2 years back. But for some reason, it is not working for me. The sample code, in the tutorial, gives me too many errors. So I am not sure, if I build the code properly or some thing is wrong with the tutorial. But I would be happy to start from some other page as well.
Start with the tutorial linked by sharth. Then go through Clang's Doxygen. Start with SemaConsumer.
Read a lot of source code. Clang is a moving target. If you are writing tools based on clang, then you need to recognize that clang is adding and fixing features daily, so you should be prepared to read a lot of code!
You probably want the stable C API provided in the libclang library, as opposed to the unstable C++ internal APIs that others have mentioned.
The best documentation to start with currently is the video/slides of the talk, "libclang: Thinking Beyond the Compiler" available on the LLVM Developers Meeting website.
However, do note that the stability of the API comes at a cost of comprehensiveness. You won't be able to do everything with this API, but it is much easier to use.
To obtain the AST as well as get to know stages of the frontend, there is a frontend chapter in the book "LLVM core libraries". Basically it has such a flow (in the case of llvm-4.0.1 and should similar for later versions):
cc1_main.cpp:cc1_main (ExecuteCompilerInvocation)
CompilerInstance.cpp:CompilerInstance::ExecuteAction
ParseAST.cpp:clang::ParseAST (Consumer>HandleTranslationUnit(S.getASTContext())
CodeGenAction.cpp:HandleTranslationUnit
The last function handles the whole translation unit(top level decls are already handled at this point), and calls EmitBackendOutput to do backend stuff. So this function is a good spot where you can do something with the complete AST and before emitting backend output.
In terms of how to manipulate the AST, clang has some basic tutorial on this: http://clang.llvm.org/docs/RAVFrontendAction.html.
Also look at ASTDumper.cpp. It's the best example of visiting the AST.
Another good tutorial: https://jonasdevlieghere.com/understanding-the-clang-ast/ teaches you how to find a specific call expr in the AST via three different approaches.
I find this ASTUnit::LoadFromCompilerInvocation() fn as the most easiest way to construct the AST.
This link may give you some ideas http://comments.gmane.org/gmane.comp.compilers.clang.devel/12471

Resources