Skip to content

Instantly share code, notes, and snippets.

@jeffotoni
Created April 20, 2018 02:58
Show Gist options
  • Save jeffotoni/4f74ae6e55fb3e13687ab9c3e6f6809e to your computer and use it in GitHub Desktop.
Save jeffotoni/4f74ae6e55fb3e13687ab9c3e6f6809e to your computer and use it in GitHub Desktop.
/**
* @autor jeffotoni@gmail.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main()
{
///declara ponteiro para receber lib
void *handle;
///assinatura da funcao Multi
unsigned long int (*Multi)(unsigned long int f);
///capturar o valor na tela..
int valorToGerar;
int n , i = 0, c;
///erro para tratar dlopen
char *error;
///dlopen => abrindo biblioteca
handle = dlopen("./C/libmulti.so",RTLD_LAZY);
if(!handle)
{
fprintf(stderr, "Erro: %s\n", dlerror());
exit(1);
}
///chando a funcao fibonancci
Multi = dlsym(handle, "Multi");
if((error = dlerror()) != NULL)
{
fprintf(stderr,"Erro: %s\n", error);
exit(1);
}
printf("Quantidade de termos da sequencia Multi:\n");
scanf("%d", &n);
for ( c = 1 ; c <= n ; c++ )
{
valorToGerar = c;
printf("Sequencia: %d * 2 = %lu\n",valorToGerar,(*Multi)(c));
i++;
}
dlclose(handle);
return 0;
}
@jeffotoni
Copy link
Author

jeffotoni commented Apr 20, 2018

golang

package main

import "fmt"
import "github.com/rainycape/dl"
import "log"

func main() {

fmt.Println("test lib")                                                                                                                                           
                                                                                                                                                                  
lib, err := dl.Open("./Multi.so", 0)                                                                                                                         
if err != nil {                                                                                                                                                   
                                                                                                                                                                  
    log.Fatalln(err)                                                                                                                                              
}                                                                                                                                                                 
                                                                                                                                                                  
//fmt.Println(lib.Multi(100))                                                                                                                                  
defer lib.Close()                                                                                                                                                 
                                                                                                                                                                  
//var DoubleIt func([]byte, uint, int, ...interface{}) int                                                                                                        
var DoubleIt func(y int) int                                                                                                                                      
                                                                                                                                                                  
if err := lib.Sym("Multi", &Multi); err != nil {                                                                                                            
    log.Println(err)                                                                                                                                              
}                                                                                                                                                                 
                                                                                                                                                                  
//buf := make([]byte, 200)                                                                                                                                        
fmt.Println(Multi(100))                                                                                                                                        
                                                                                                                                                                  
//s := string(buf[:bytes.IndexByte(buf, 0)])                                                                                                                      
//fmt.Println(s)                                                                                                                                                  
                                                                                                                                                                  
//fmt.Println(x.Multi(10))                                                                                                                                     

}

@jeffotoni
Copy link
Author

python

import ctypes
lib = ctypes.CDLL("libmulti.so")
lib.Multi(100)
200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment