LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to compile a code in gcc, linux by using self-created library??? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-compile-a-code-in-gcc-linux-by-using-self-created-library-719753/)

payu21 04-17-2009 12:30 AM

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

theNbomr 04-17-2009 06:45 PM

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
For the full details,
Code:

man gcc
man ld

--- rod.

maresmasb 04-17-2009 08:07 PM

makefile sample
 
For your viewing pleasure some generic Makefile contents from some CSIG codework:
Code:

# ------------------------------------------------------
# building a static library (lib<name>.a)
# ------------------------------------------------------
static: $(SRCS) $(INCS)
        $(CC) -Wall -c $(SRCS)
        ar -qv lib$(LIB_NAME).a $(OBJS)
        ranlib lib$(LIB_NAME).a
        @@echo static library lib$(LIB_NAME).a created

# ------------------------------------------------------
# building a shared library (lib<name>.so)
# ------------------------------------------------------
shared: $(SRCS) $(INCS)
        $(CC) -fPIC -Wall -c $(SRCS)
        $(CC) -shared -Wl,-soname,lib$(LIB_NAME).so \
                -o lib$(LIB_NAME).so $(OBJS) -lc
        @@echo shared library lib$(LIB_NAME).so created

# ------------------------------------------------------
# create test application compiled from sources directly
# ------------------------------------------------------
$(PROJECT): $(SRCS) $(INCS) $(TSRCS) $(TINCS)
        $(CC) $(DEBUG) -Wall $(COB_BITSMAIN) -c $(SRCS) $(TSRCS)
        $(CC) -o $(PROJECT)$(EXE_SUFFIX) $(OBJS) $(TOBJS)
        @@echo application $(PROJECT)$(EXE_SUFFIX) created

# ------------------------------------------------------
# create test application based on static library
# ------------------------------------------------------
$(PROJECT)_static: static $(TSRCS) $(TINCS)
        $(CC) $(DEBUG) -Wall $(COB_BITSMAIN) -c $(TSRCS)
        $(CC) -o $(PROJECT)_static$(EXE_SUFFIX) $(TOBJS) lib$(LIB_NAME).a
        @@echo application $(PROJECT)_static$(EXE_SUFFIX) created

# ------------------------------------------------------
# create test application based on shared library
# ------------------------------------------------------
$(PROJECT)_shared: shared $(TSRCS) $(TINCS)
        $(CC) $(DEBUG) -Wall $(COB_BITSMAIN) -c $(TSRCS)
        $(CC) $(TOBJS) -o $(PROJECT)_shared$(EXE_SUFFIX) -L. -l$(LIB_NAME)
        @@echo application $(PROJECT)_shared$(EXE_SUFFIX) created


payu21 04-22-2009 04:13 AM

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

pixellany 04-22-2009 07:14 AM

Without seeing the code---or the error--I don't see how anyone will be able to help you.
Quote:

Can anyone pls help me on this as soon as possible
As the saying goes: "We have two speeds---If you don't like this one, you surely won't like the other." What's the urgency?-----Homework?

payu21 04-27-2009 11:16 PM

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

theNbomr 04-28-2009 06:38 PM

You do know that your code can only run with root privileges, right?
--- rod.

payu21 04-30-2009 09:01 AM

yes i do

norobro 04-30-2009 10:28 AM

Don't you need to call "ioperm" on the port or did I miss it in your code?

From man inb:
Quote:

You use ioperm(2) or alternatively iopl(2) to tell the kernel to allow the user space application to access the I/O ports in question. Failure to do this will cause the application to receive a segmentation fault.

payu21 05-22-2009 01:40 AM

Quote:

Originally Posted by norobro (Post 3525903)
Don't you need to call "ioperm" on the port or did I miss it in your code?

From man inb:


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.