Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Friday, August 3, 2012

Detecting memory leaks in C/C++ executable or libs with vallgrind

This is an awesome tool. It tells u where and how your memory leaks happen. Don't forget to compile with the -g to get debug symbols....anyway the tool is called

Valgrind

on Ubuntu i installed using
sudo apt-get install valgrind

Invoking native C/C++ libs using python

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html...here is what you have to do with python

This one is super ez!
from ctypes import cdll
from ctypes import c_char_p
from ctypes import c_float

lib = cdll.LoadLibrary('libmylib.so')
helloWorld = lib.MYLIB_helloWorld

helloWorld()

Invoking native C/C++ libs using clojure and JNA

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html....here is how you do it in clojure download jna.jar and platform.jar write clojures codes!
(defn helloworld ( []
                ( let [ lib  ( com.sun.jna.NativeLibrary/getInstance "mylib" ) ]
                ( let [ helloWorld ( .getFunction lib  "MYLIB_helloWorld" ) ]
                ( println ( .invokeVoid helloWorld null ) ) ) ) ) )

( helloworld )


then run with
java -cp /usr/share/java/clojure.jar:jna.jar:. -Djna.nosys=true clojure.main helloworld.clj


Invoking native C/C++ lib using groovy and JNA

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html....here is how to invoke using groovy again make sure you get the jna.jar and platform.jar from the interwebs!

here is the groovy code:

import com.sun.jna.*;

def lib = NativeLibrary.getInstance("mylib")
def helloWorld = lib.getFunction( "MYLIB_helloWorld" )

/*
if args do this
Object[] args = new Object[2];
args[0] = "arg1"
args[1] = "arg2"
*/

helloWorld.invokeVoid(null)

then run with

groovy -cp jna.jar:.  -Djna.nosys=true helloworld.groovy

for more invoke options see http://jna.java.net/javadoc/com/sun/jna/Function.html

Invoking native C/C++ lib from C# and mono

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html......here is how u connect using c# and mono (haven't tried .net windows but I'm guessing it's the same)

this one is super ez. here is the full example
using System;
using System.Runtime.InteropServices;

public class HelloWorld
{

        static public void Main( string[] args )
        {
                
MYLIB_helloWorld();

        }

        [DllImport("libmylib.so")]
        private static extern void MYLIB_helloWorld();




}

then run with
gmcs HelloWorld.cs
mono HelloWorld.exe

Invoking native C/C++ lib from Java and JNA

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html......here is how u connect using JNA:
  1. compile your native lib to so or dll and place it in your LIB_LIBRARY_PATH on linux or in your PATH in windows (IMPORTANT needs to start with "lib". So if it's mylib it should be libmylib
  2. download jna.jar and platform.jar either manually or with a tool like maven
  3. create an interface that extends com.sun.jna.Native
  4. create class to use (not implement) the interface
here is sampe interface


import com.sun.jna.Library;
import com.sun.jna.Native;


interface MyLib extends Library {

        MyLib INSTANCE = (MyLib) Native.loadLibrary("mylib", MyLib.class );
        String MYLIB_helloWorld();
        
}

and a class that uses it


import com.sun.jna.Library;
import com.sun.jna.Native;

public class MyLibUser{


        public static void main( String[] args ) {

                MyLib lib = MyLib.INSTANCE;

                lib.MYLIB_helloWorld();
        }


}

now compile with
javac -cp jna.jar:. -Djna.nosys=true  MyLibUser
and run with
java -cp jna.jar:. -Djna.nosys=true  MyLibUser
Gaaaangsta! No?

Making C++ available through native calls

So if you need some raw of performance in your application what do you do? You drop down into a native development language like C. Well what if you want to code with OOP goodness and use C++. Regular calls from Java, Ruby, etc. will not get to your C++ functions because C++ adds all sorts of template madness when it exports your programs symbols.

So what do you do?


extern "C"

so if your want to wrap your c++ classes in a single method called helloWorld (or MYLIB_helloWorld to not be confused with other helloWorlds!):
extern "C" MYLIB_helloWorld();
in your header file.....then in your cpp file you can just do the normal
MYLIB_helloWorld();
Yay! Now you can make native calls from higher level languages like java or even ruby without them getting pissed off at the c++ template symbols. Will post examples soon.

Friday, March 2, 2012