LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to link a library to gcc? (https://www.linuxquestions.org/questions/programming-9/how-to-link-a-library-to-gcc-319962/)

Andrea_81 05-04-2005 05:11 AM

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

__J 05-04-2005 05:32 AM

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

Andrea_81 05-04-2005 06:08 AM

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

__J 05-04-2005 06:13 AM

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

Andrea_81 05-05-2005 04:10 AM

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?

freegianghu 05-05-2005 05:16 AM

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

__J 05-05-2005 05:22 AM

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.


All times are GMT -5. The time now is 08:23 PM.