LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2009, 12:30 AM   #1
payu21
LQ Newbie
 
Registered: Apr 2009
Location: australia
Posts: 12

Rep: Reputation: 0
Question 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
 
Old 04-17-2009, 06:45 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 04-17-2009, 08:07 PM   #3
maresmasb
Member
 
Registered: Apr 2009
Posts: 108

Rep: Reputation: 24
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

Last edited by Tinkster; 10-30-2010 at 03:17 PM.
 
Old 04-22-2009, 04:13 AM   #4
payu21
LQ Newbie
 
Registered: Apr 2009
Location: australia
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
Old 04-22-2009, 07:14 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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?
 
Old 04-27-2009, 11:16 PM   #6
payu21
LQ Newbie
 
Registered: Apr 2009
Location: australia
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
Old 04-28-2009, 06:38 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You do know that your code can only run with root privileges, right?
--- rod.
 
Old 04-30-2009, 09:01 AM   #8
payu21
LQ Newbie
 
Registered: Apr 2009
Location: australia
Posts: 12

Original Poster
Rep: Reputation: 0
yes i do
 
Old 04-30-2009, 10:28 AM   #9
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 663

Rep: Reputation: 252Reputation: 252Reputation: 252
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.

Last edited by norobro; 04-30-2009 at 10:34 AM. Reason: clean up formatting in text from man page
 
Old 05-22-2009, 01:40 AM   #10
payu21
LQ Newbie
 
Registered: Apr 2009
Location: australia
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by norobro View Post
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to compile C program using the OSSP mm library and gcc? seleria Linux - Software 0 11-06-2006 03:08 AM
gcc compile error with X11 library routines jrbrearley Programming 2 07-26-2006 01:50 PM
Strange GCC compile error with C code exvor Programming 16 08-23-2005 06:05 PM
I cannot compile my souce code created by Glade on redhat9 msramachandran Programming 0 06-02-2004 02:13 AM
why gcc permit this code to compile? is there a reason? zeppelin Programming 10 04-30-2004 04:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration