Minimal working example of mixed Julia and C project on Windows - c

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.

Related

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

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. ^^

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.

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 Google Test OK for testing C code?

So I've come to like and enjoy using Google Test for a C++ project I'm involved in. I'm just bringing up a new project that will be straight C (a library) and so far can't see any reason why not to continuing using Google Test, even though its a C++ framework. Having a C++ compiler available will not be an issue.
Are there are specific reasons why I shouldn't use Google Test for testing straight C code?
Thanks.
It is pretty common to test C code using a C++ testing frameworks, even the leading book on the subject follows this approach. I have used googletest extensively in the past to unit test C code and can recommend it.
I have written a blog post about it that might be useful:
http://meekrosoft.wordpress.com/2009/11/09/unit-testing-c-code-with-the-googletest-framework/
As all Google's C++ code, Google Test does not use exceptions, so exception safety flow won't be an issue. As long as your headers are C++-compatible (not using C++ keywords, export symbols with correct linkage), it should be fine.
I just thought I'd add another point: since gtest is C++, you'll be parsing your C headers under test as C++. This means the tests don't guarantee that the headers are consumable from C. I recently ran into this with a C library I'm building.
Jason,
be aware of that!!! :D
As Meekrosoft said, yes, it is possible. I also used his website when I tried to do that. It works, but there is one big problem:
GTest is objected oriented tool and C language isn't!
In example, in GTest you have a lot of functions (80% of whole API) that request object as parameter, for example:
EXPECT_CALL(turtle, PenDown()) // turtle is object(class) and PenDown() is method of that object
.Times(AtLeast(1));
from GTest website gmock_for_dummies.md
so you will use only macros like expect_equal, expect_bigger_than and so on...
I would like to suggest you tool CMocka (or some other C unit testing tools). It is also from google (modified by group of non-google developers) and it is created directly for C language. I use this one when I want to test C-type source code.
I hope this helps.. :-) Have a nice day.. :-)
I could not name one. I guess there will be some things which you don't have in "normal" C. E.g I think the TestCase are derived from a certain class. But within the test you can test whatever you like and so why not C?

Program visible to Linux as normal directory

I'm trying to write program to work as programmable directory, in other words: User, or other systems open that directory and read/write files or dirs. I try to create program to cache most used files in memory (less I/O to HDD), but right now I don't know how to achive that. There are probably some docs about this but I can't find them. I know that there is FUSE, NFS and others, but reading their source is quite difficult. If any one has info about implementation in C lang I'll be very grateful.
Sorry for my English..
FUSE has a C interface - take a look at their Hello World example.
If you want a simple implementation, try Python's FUSE library. A quick tutorial can be found here.
You could have a look at the GIO library — it's part of GTK, but can be used separately. The documentation is pretty thorough, and if you need to do some quick prototyping you can use the PyGTK GIO bindings to mess around before going back and writing it in C.
It's licensed under the LGPL.
If you find it easier to code in Python, it's possible to create a compiled program using cx_Freeze.

Resources