LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2005, 04:11 AM   #1
Andrea_81
LQ Newbie
 
Registered: May 2005
Posts: 3

Rep: Reputation: 0
How to link a library to gcc?


I have installed Libnet (some network library), I have to compile a file wrote in c (server.c), this file uses Libnet. Using gcc how can i link the file server.c to Libnet?
P.S. the file server.c is located in home/andrea/ Libnet is located in home/andrea/libnet
 
Old 05-04-2005, 04:32 AM   #2
__J
Senior Member
 
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973

Rep: Reputation: 46
depends on libnet's configuration. some libs come with scripts that you specify on the command line and they do the work for you, some rely on programs like pkg-config, others you have to do everything manually. for the linker to pick up the libs, you will either have to set LDFLAGS or LD_LIBRARY_PATH to point to the libnet install directory ( probably /home/andrea/libnet/lib). for the includes, if libnet doesn't have a method like above, you will have to set the CFLAGS varable. try this and see if it helps:

(note this is for bash and zsh)
export CFLAGS="-I/home/andrea/libnet/include"
export LDFLAGS="-L/home/andrea/libnet/lib"
or
export LD_LIBRARY_PATH=/home/andrea/libnet/lib:$LD_LIBRARY_PATH

you may have to possibly tell the linker explicitly which lib to link against, for instance if the lib is called libnet.so ( or libnet.so.1.2.1 or whatever):

gcc blah blah blah -lnet
tells the linker to link with libnet
 
Old 05-04-2005, 05:08 AM   #3
Andrea_81
LQ Newbie
 
Registered: May 2005
Posts: 3

Original Poster
Rep: Reputation: 0
I have wrote this:
[andrea@localhost chat]$ gcc -o server -lnet -L /home/andrea/libnet/include server.c

but there is this error:
server.c:14:20: libnet.h: No such file or directory

P.S. the file libnet.h is in the directory file:/home/andrea/libnet/include
 
Old 05-04-2005, 05:13 AM   #4
__J
Senior Member
 
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973

Rep: Reputation: 46
did you set the CFLAGS environmental variable or just pass -I/home/andrea/libnet/include on the gcc command line

gcc -I/home/andrea/libnet/include -L/home/andrea/libnet/lib server.c -o server

try that out and see if it helps
 
Old 05-05-2005, 03:10 AM   #5
Andrea_81
LQ Newbie
 
Registered: May 2005
Posts: 3

Original Poster
Rep: Reputation: 0
I'have exported CFLAGS and LDFLAGS then i wrote:

gcc -I/home/andrea/libnet/include -L/home/andrea/libnet/lib server.c -o server

but are appeared a lot of errors like this
:
/home/andrea/tmp/cclqmr4a.o(.text+0x7): In function `conio_init':
: undefined reference to `initscr'

what thoes it means?
 
Old 05-05-2005, 04:16 AM   #6
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Quote:
Originally posted by Andrea_81
I'have exported CFLAGS and LDFLAGS then i wrote:

gcc -I/home/andrea/libnet/include -L/home/andrea/libnet/lib server.c -o server

but are appeared a lot of errors like this
:
/home/andrea/tmp/cclqmr4a.o(.text+0x7): In function `conio_init':
: undefined reference to `initscr'

what thoes it means?
gcc -I/home/andrea/libnet/include -L/home/andrea/libnet/lib server.c -o server -lcurses
 
Old 05-05-2005, 04:22 AM   #7
__J
Senior Member
 
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973

Rep: Reputation: 46
Quote:
Originally posted by Andrea_81
I'have exported CFLAGS and LDFLAGS then i wrote:

gcc -I/home/andrea/libnet/include -L/home/andrea/libnet/lib server.c -o server

but are appeared a lot of errors like this
:
/home/andrea/tmp/cclqmr4a.o(.text+0x7): In function `conio_init':
: undefined reference to `initscr'

what thoes it means?
it means the linker cannot find the function "initscr" to link into the program. do you know where this function comes from ( is it one of your own)? if you declared a function called "Initscr" in a header somewhere and accidentally called it by "initscr" this error will happen. or if it's part of another library you are using and you accidentally misspelled it this will happen. if it's part of libnet you may need to explicitly declare it on the command line:

gcc -I/home/andrea/libnet/include -L/home/andrea/libnet/lib server.c -o server -lnet

if you pass the whole path to gcc ( the -I/home/an... and -L/home/an....) you shouldn't need to export CFLAGS and LDFLAGS. as with all things in *nix, there are multiple ways of doing what you want.
 
  


Reply



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
link using libuClibc-0.9.20.so library nignogeta Linux - Software 0 06-30-2005 06:27 AM
KDevelop, how to link a library?? Umanga Programming 1 01-10-2005 05:13 PM
how to static link a specific library? koodailar Programming 3 07-26-2004 03:35 PM
gcc 3.3.2, shared library luzi82 Debian 2 12-21-2003 09:35 AM
Want to get GCC library help TamArasan Linux - Software 1 08-14-2003 08:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:59 PM.

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
Open Source Consulting | Domain Registration