How to compile a code in gcc, linux by using self-created library???
Hi all,
i am a new user of Linux. I am doing a project in which i need to get data from LDR and display it on Computer using parallel port. I have created my own library to access the ports, but i dnt know how to use that library to run my program for LDR sensitivity measurement. Can anyone pls help me on this as soon as possible Thanks |
The gcc/ld option to link any library is the '-l' (that is lower-case ell), followed by the name of the library, without the leading 'lib' prefix and trailing '.so' suffix. There is the accompanying '-L' option to specify a single directory in which to look for any specified object libraries. So, if your library is named 'libHortyFlorty.so', you might link it to your main object module thusly:
Code:
ld -lHortyFlorty -L/sesame/Street muppet.o -o jimHenson Code:
man gcc |
makefile sample
For your viewing pleasure some generic Makefile contents from some CSIG codework:
Code:
# ------------------------------------------------------ |
Thankyou very much for your help with it!!
i did try that and it is working a bit ahead but now when i am actually running the code, it is showing a segmentation fault error. SO can anyone tell em what exactly it is?? payal |
Without seeing the code---or the error--I don't see how anyone will be able to help you.
Quote:
|
Hi all,
here is my code with the libraries created. after compling it i get a segmentation fault error... ------------------------ MAIN CODE ADC1.c ---------------------- #include "libblib.h" #include <stdio.h> #include <unistd.h> #include <sys/io.h> #define ChipSelect Clear_ControlPort_bit(0) // to clear RD' and CS' int Read_adc(void); int main() { float a=1,b; int i=0; for(i=0;i<400;i++) { b= Read_adc(); printf("\nadc output:%f\n",b); a=b*5/255; printf("input voltage is %f",a); } } int Read_adc(void) { ChipSelect; //to select the chip, enabled usleep(10000); Clear_ControlPort_bit(1); usleep (10000); Set_ControlPort_bit(1); usleep(10000); return(Read_DataPort()); } ---------------------------- libblib.h ---------------------------- #include <stdio.h> #define PORT1 0x378 //basic port address #define DataPort PORT1 #define ControlPort PORT1+2 #define StatusPort PORT1+1 #define _BV(a) (1<<a) int Read_ControlPort(void); void Write_ControlPort(int a); void Set_DataPort_input(void); void Write_DataPort(int a); int Read_DataPort(void); int Read_StatusPort(void); void Set_DataPort_bit(int a); void Clear_DataPort_bit(int a); void Set_ControlPort_bit(int a); void Clear_ControlPort_bit(int a); int Show_DataPort_bit(int a); int Show_ControlPort_bit(int a); int Show_StatusPort_bit(int a); --------------------------- libblib.c ----------------------------- #include "libblib.h" #include <stdio.h> #include <unistd.h> #include<sys/io.h> /*---------------------------------------------------------------- -------------function for reading control port------------------- -----------------------------------------------------------------*/ int Read_ControlPort(void) { return(inb(ControlPort)); } /*---------------------------------------------------------------- -------------function for writting control port------------------- -----------------------------------------------------------------*/ void Write_ControlPort(int a) { outb(ControlPort,a) ; } /*---------------------------------------------------------------- -------------function for reading data port---------------------- -----------------------------------------------------------------*/ int Read_DataPort(void) { int b; Set_DataPort_input(); b=inb(DataPort); Clear_ControlPort_bit(5); return(b); } /*---------------------------------------------------------------- -------------function for writting data port---------------------- -----------------------------------------------------------------*/ void Write_DataPort(int a) { Clear_ControlPort_bit(5); outb(DataPort,a); } /*---------------------------------------------------------------- -------------function for reading status port--------------------- -----------------------------------------------------------------*/ int Read_StatusPort(void) { return(inb(StatusPort)); } /*---------------------------------------------------------------- -------------function for setting a bit in Data Port-------------- -----------------------------------------------------------------*/ void Set_DataPort_bit(int a) { if(a<8) Write_DataPort(Read_DataPort()|_BV(a)); } /*---------------------------------------------------------------- -------------function for clearing a bit in Data Port------------- -----------------------------------------------------------------*/ void Clear_DataPort_bit(int a) { if(a<8) Write_DataPort(Read_DataPort()&~_BV(a)); } /*---------------------------------------------------------------- -------------function for setting a bit in ControlPort------------ -----------------------------------------------------------------*/ void Set_ControlPort_bit(int a) { if(a<8) { if(a==0||a==1||a==3) Write_ControlPort(Read_ControlPort()&~_BV(a)); else Write_ControlPort(Read_ControlPort()|_BV(a)); } } /*---------------------------------------------------------------- ----------function for clearing a bit in Control Port------------- -----------------------------------------------------------------*/ void Clear_ControlPort_bit(int a) { if(a<8) { if(a==0||a==1||a==3) Write_ControlPort(Read_ControlPort()|_BV(a)); else Write_ControlPort(Read_ControlPort()&~_BV(a)); } } /*---------------------------------------------------------------- -----function for making data port for input (C5=1)--------------- -----------------------------------------------------------------*/ void Set_DataPort_input(void) { Write_ControlPort(Read_ControlPort()|_BV(5)); } /*---------------------------------------------------------------- -----function for showing Data Port bit ------------------------- -----returns (2) if the a is invalid -- ------------------------- -----------------------------------------------------------------*/ int Show_DataPort_bit(int a) { if(a<8) return((Read_DataPort()&_BV(a))>>a); else return (2); } /*---------------------------------------------------------------- -----function for showing Control Port bit ------------------------- -----returns (2) if the a is invalid -- ------------------------- -----------------------------------------------------------------*/ int Show_ControlPort_bit(int a) { if(a<8) return((Read_ControlPort()&_BV(a))>>a); else return (2); } /*---------------------------------------------------------------- -----function for showing Status Port bit ----------------------- -----returns (2) if the a is invalid -- ------------------------- -----------------------------------------------------------------*/ int Show_StatusPort_bit(int a) { int i; if(a<8) { i=(Read_StatusPort()&_BV(a))>>a; if(a==7) return(!i); else return(i); } else return (2); } ---------------------------------------------- Compling commands i used for above code ....... gcc -c libblib.c gcc -o ADC2 ADC1.c libblib.o ./ADC2 I have been trying to solve this issue but not able to understand why i can't get the LDR sensitivity displayed? please help me |
You do know that your code can only run with root privileges, right?
--- rod. |
yes i do
|
Don't you need to call "ioperm" on the port or did I miss it in your code?
From man inb: Quote:
|
Quote:
I finally solved my segmentation fault by using the command: sudo ./programfilename.c passowrd: ./programfilename.c there wasnt any problem in my code... ;) |
All times are GMT -5. The time now is 06:49 AM. |