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 ruby

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 ruby

install ffi. linux distros have packages.....so on ubuntu apt-get install ffi...

then here is the module code as well as sample script

module LibMylib
        extend FFI::Library
        ffi_lib "libmylib.so"
        attach_function :MYLIB_helloWorld, [], :void
end
then your script. make sure libmylib is replaced with whatever u named the module file above
require 'ffi'
require 'libmylib'

LibMylib.MYLIB_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.

Cygwin and SSH server

To setup SSH on windows using cygwin....first install cygwin! Make sure you check off the ssh package. Then open up the cygwin shell and do the following:

ssh-host-config -y

when prompted with the CYGWIN=
tty ntsec
and then finally
cygrunsrv -S sshd
You might hit ERRORS wither on first start or 2 years later! Most common that I've run into are:
  • Win32 error 1069: The service did not start due to a logon failure.
  • Error starting a service: QueryServiceStatus: Win32 error 1062: The service has not been started.
For the first one go to services in windows and lookup CYGWINsshd. In properties under "log on" tab make sure your user (usually cyg_server) AND password match the on in your cygwin. So type
passwd cyg_server
in your cygwin shell to change it ... For the second problem just repeat the steps from STEP one to reconfigure.. Cheers!