Desarrollando Libreria Para Operadores Con Bit [Solucionado]


Goto page 1, 2, 3  Next

Subject: Desarrollando Libreria Para Operadores Con Bit [Solucionado]
Bueno sigo con el tema de los operadores con bit,
operadores_bit_a_bit
estoy intentando hacer una libreria en C para llamarla desde gambas3

Me he hecho en programita en C:
#include <stdio>

// ejemplo tomado de: http://www.zator.com/Cpp/E4_9_3.htm
// para compilar: gcc libm.cpp -o m
// para ejecutar: ./libm

// crear libreria .so:
// gcc -o libm.so libm.cpp -shared -fPIC


//x &= y; // equivale a: x = (x & y);
//x ^= y; // equivale a: x = (x ^ y);
//x |= y; // equivale a: x = (x | y);



int BitBarraVertical(int x,int y)
{
return x|=y;
}

int BitAmpersand(int x,int y)
{
return x&=y;
}

int BitComplemento(int x,int y) // complemento a uno'
{
return x=~y;
}


int BitPotencia(int x,int y)
{
return x^=y;
}



int main() {
signed int x = 2, y = 7, z = 6, a = 2, b= -2;

printf("Valor Inicial x = %d \n", x);
printf("Valor Inicial y = %d \n", y );
printf("Valor Inicial z = %d \n", z );
printf("Valor Inicial a = %d \n", a );
printf("Valor Inicial b = %d \n", b );


printf("\nOperaciones:\n") ;

printf("x &= -2\n");
printf("y ^= -2\n");
printf("z |= 13\n");
printf("a ~= 1\n");

x &= -2;
y ^= -2;
z |= 13;
a <<1>>= 1;

printf("\nResultado:\n") ;
printf("Valor x = %d \n", x);
printf("Valor y = %d \n", y );
printf("Valor z = %d \n", z );
printf("Valor a = %d \n", a );
printf("Valor b = %d \n", b );


x = 2;
y = 7;
z = 6;
a = 2;
b= -2;

x= BitAmpersand(x,-2);
y= BitPotencia(y,-2);
z= BitBarraVertical(z,13);




printf("\nResultado con funciones:\n") ;
printf("Valor x = %d \n", x);
printf("Valor y = %d \n", y );
printf("Valor z = %d \n", z );



}


Luego lo compilo, y creo el archivo .so:
gcc -o libm.so libm.cpp -shared -fPIC

El archivo que crea lo copio en el mismo directorio donde estoy creando el programa en gambas3 (para que lo encuentre sin problemas)

Ahora me hago el programita en gambas3:
' gambas module file
library "libm"

private extern BitBarraVertical(x as integer,y as integer) as integer
private extern BitAmpersand(x as integer,y as integer) as integer
private extern BitPotencia(x as integer,y as integer) as integer


Public Sub Main()
dim n as integer
dim x as integer=2
dim y as integer=7
dim z as integer=6



x= BitAmpersand(x,-2)
y= BitPotencia(y,-2)
z= BitBarraVertical(z,13)

print "aplicando funciones externas:"
print "x ",x
print "y ",y
print "z ",z

end


Y cuando lo ejecuto me dice:
Citar:
MMain.Main.20: #61: Cannot find symbol 'BitAmpersand' in dynamic library 'libm'
1: MMain.Main.20


Osea que es como si no encontrare la función "BitAmpersand" en la libreria libm....

¿alguna ayuda?

Last edited by jsbsan on Thursday, 19 May 2016, 11:54; edited 3 times in total
Subject: Re: Desarrollando Libreria Para Operadores Con Bit
El tu codigo en C++ al compilarlo me da estos errores:

/tmp/c.cpp: In function ‘int BitBarraVertical(int, int)’:
/tmp/c.cpp:23:11: error: ‘PIPE’ was not declared in this scope
return x<PIPE>=y;
^
/tmp/c.cpp: In function ‘int main()’:
/tmp/c.cpp:47:37: error: ‘printf’ was not declared in this scope
printf("Valor Inicial x = %d \n", x);
^
/tmp/c.cpp:63:4: error: ‘PIPE’ was not declared in this scope
z <PIPE>= 13;
^
/tmp/c.cpp:64:6: error: lvalue required as left operand of assignment
a <<1>>= 1;
^

Profile PM  
Subject: Re: Desarrollando Libreria Para Operadores Con Bit
Vuott:


Donde pone <PIPE> hay que poner el caracter de la barra vertical

Y a <<1>>= 1;
es solo doble <

Te adjunto el código (no es exactamente el mismo que he pegado arriba), pero lo he simplificado más.


libreria y gambas.zip
Description:  
Download
Filename: libreria y gambas.zip
Filesize: 1.01 KB
Downloaded: 103 Time(s)
libreria y gambas.zip
Description:  
Download
Filename: libreria y gambas.zip
Filesize: 1.01 KB
Downloaded: 103 Time(s)
libreria y gambas.zip
Description:  
Download
Filename: libreria y gambas.zip
Filesize: 1.01 KB
Downloaded: 103 Time(s)

Subject: Re: Desarrollando Libreria Para Operadores Con Bit
jsbsan escribió:  
Te adjunto el código

He visto.
Lo he modificado de C++ a C:

#include <stdio.h>

int BitBarraVertical(int x,int y)
{
return x|=y;
}

int BitAmpersand(int x,int y)
{
return x&=y;
}

int BitComplemento(int x,int y) // complemento a uno'
{
return x=~y;
}


int BitPotencia(int x,int y)
{
return x^=y;
}

y lo probé con el tu programito gambas. Esta la su salida:
aplicando funciones externas:
x 2
y -7
z 15
El complemento a uno de 0 es: -1



Pero esto ^ en C/C++ no es Potencia, es XOR.
saludos

Last edited by vuott on Thursday, 19 May 2016, 01:45; edited 2 times in total
Profile PM  
Subject: Re: Desarrollando Libreria Para Operadores Con Bit
vuott:

¿me puedes decir los pasos que has seguido para compilarlo y crear la libreria?

Vale, lo acabo de compilar asi:
$ gcc -fPIC -c Bit.c

$ gcc -shared -o Bit.so Bit.o

Y el archivo generado lo he copiado (bit.so) a donde esta el proyecto de gambas, y funciona!!!!

que cosa más rara que con .cpp no funcione ¿no?

Last edited by jsbsan on Wednesday, 18 May 2016, 13:07; edited 1 time in total

bit y gambas3 v2.zip
Description:  
Download
Filename: bit y gambas3 v2.zip
Filesize: 1 KB
Downloaded: 111 Time(s)
bit y gambas3 v2.zip
Description:  
Download
Filename: bit y gambas3 v2.zip
Filesize: 1 KB
Downloaded: 111 Time(s)
bit y gambas3 v2.zip
Description:  
Download
Filename: bit y gambas3 v2.zip
Filesize: 1 KB
Downloaded: 111 Time(s)

Subject: Re: Desarrollando Libreria Para Operadores Con Bit
...en Gambas:

Print " & ---->", x And -2
Print " ^ ---->", y Xor -2
Print " | ---->", z Or 13
Print " ~ ---->", Not 0

Profile PM  
Subject: Re: Desarrollando Libreria Para Operadores Con Bit
jsbsan escribió:  
¿me puedes decir los pasos que has seguido para compilarlo y crear la libreria?


gcc -o /tmp/libBit.so /tmp/libBit.c -shared

Last edited by vuott on Wednesday, 18 May 2016, 13:04; edited 2 times in total
Profile PM  
Subject: Re: Desarrollando Libreria Para Operadores Con Bit
Vuott:

Citar:
Print " & ---->", x And -2
Print " ^ ---->", y Xor -2
Print " | ---->", z Or 13
Print " ~ ---->", Not 0

¿y eso de donde lo has sacado??

Subject: Re: Desarrollando Libreria Para Operadores Con Bit
jsbsan escribió:  
¿y eso de donde lo has sacado??

...de un cualquier manual de C.

Profile PM  
Subject: Re: Desarrollando Libreria Para Operadores Con Bit
jsbsan escribió:  
Y cuando lo ejecuto me dice:
Citar:
MMain.Main.20: #61: Cannot find symbol 'BitAmpersand' in dynamic library 'libm'
1: MMain.Main.20


Osea que es como si no encontrare la función "BitAmpersand" en la libreria libm....


Esta es la razón por la que yo no creo librerias .so escritas en C++. No es posible llamar las sus funciones directamente con sus nombres.
Si quieres utilizar una tu libreria escrita en C++, tienes que escribir y llamar la routine principal "main( )". Entre de esta routine puedes escribir lo que te necesita, o puedes llamar desde main() una sub-routine:
http://www.gambas-it.org/wiki/index...unzioni_esterne

Last edited by vuott on Wednesday, 18 May 2016, 23:23; edited 3 times in total
Profile PM  
Goto page 1, 2, 3  Next

Page 1 of 3


  
You cannot post new topics
You cannot reply to topics
You cannot edit your posts
You cannot delete your posts
You cannot vote in polls
You cannot attach files
You can download files
You cannot post calendar events

   

This is a "Lo-Fi" version of our main content. To view the full version with more information, formatting and images, please click here.

Powered by Icy Phoenix based on phpBB
Design by DiDiDaDo

Generation Time: 0.185s (PHP: 14% SQL: 86%)
SQL queries: 32 - Debug Off - GZIP Enabled