LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-06-2005, 10:10 AM   #1
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
compile/linker problem with simple libipq code


For some reason, my simply code is not linking, and I don't know why. The libipq library is there, it contains the functions which the linker says fail, so I'm at a loss. Hopefully it's something stupid I'm overlooking. (=

Quote:
~/c/ipk> findlib --all ipq
/usr/lib/libipq.a
~/c/ipk> nm -a /usr/lib/libipq.a
libipq.o:
... snip ...
00000224 T ipq_create_handle
000004a4 T ipq_ctl
00000334 T ipq_destroy_handle
... snip ...

~/c/ipk> cat ipk.c
Code:
#include <linux/netfilter.h>
#include <libipq.h>

int main(int argc, char **argv)
{
  struct ipq_handle *ipqh;

  //  create a handle then destroy it, very simple
  ipqh = ipq_create_handle(0, PF_INET);
  if(ipqh) ipq_destroy_handle(ipqh);

  return 0;
}
~/c/ipk> gcc -L/usr/lib -lipq -o ipk ipk.c
/tmp/ccCxaG2d.o(.text+0x18): In function `main':
: undefined reference to `ipq_create_handle'
/tmp/ccCxaG2d.o(.text+0x2f): In function `main':
: undefined reference to `ipq_destroy_handle'
collect2: ld returned 1 exit status
Nothing seems out of place.
Anyone have an idea?
 
Old 05-06-2005, 10:16 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
And how exactly are you building your test program? For the linking to be successful, two conditions must be met: The library containing the symbol must be in the directories searched by the linker and you must explicitly tell the linker to link the library.
 
Old 05-06-2005, 11:00 AM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Original Poster
Rep: Reputation: 33
If you'll look right below the segment of code, I showed the
gcc line I ran to attempt compilation/linking.

Although unnecessary, I included -L/usr/lib (where the
libipq library is housed).

The only thing that I am thinking is that maybe the linker is
looking for the shared library, which I don't have installed.

I guess I'll see about compiling it. Maybe that's the problem.

------ edit ------------

Looks like the install package only makes static libs.
Oh well, it was a thought.

Last edited by TheLinuxDuck; 05-06-2005 at 11:14 AM.
 
Old 05-06-2005, 11:15 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Nevermind, I see it now.

Try this makefile:
Code:
CC = gcc
CFLAGS = -Wall -W -c -o
LDFLAGS = -lipq -o $(EXEC)
OBJECTS = ipq_test.o
EXEC = ipq_test

all: $(OBJECTS)
\t$(CC) $^ $(LDFLAGS)

%.o: %.c
\t$(CC) $(CFLAGS) $@ $<
Replace \t with tabs and file names as appropriate

Last edited by Hivemind; 05-06-2005 at 11:20 AM.
 
Old 05-06-2005, 11:21 AM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
I was too quick to reply. Please see my edited version.
 
Old 05-06-2005, 11:34 AM   #6
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Original Poster
Rep: Reputation: 33
Thanks for the heads up and makefile example. However, I found my error. I knew that order was very important to gcc, but didn't realize that it would affect my prog like this.
Basically, -lipq needs to come AFTER the .c source. Don't ask me why, but when I compile it as:
Quote:
gcc -o ipk ipk.c -lipq
it works just fine...

::shaking head::
 
Old 05-06-2005, 11:39 AM   #7
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Yeah, that's what I suspected and therefore gave you a Makefile (I always use Makefiles) that I knew would work. In your previous command line you were passing the library option to the compiler, not the linker.
 
Old 05-06-2005, 01:28 PM   #8
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Original Poster
Rep: Reputation: 33
Quote:
Originally posted by Hivemind
In your previous command line you were passing the library option to the compiler, not the linker.
That's what -l is for, providing libraries to link in, which the compiler provides the linker.

Per gcc's man page:
Quote:
-l library
Search the library named library when linking
... snip ...
The linker searches a standard list of directories for the library, which is actually a file named libli-
brary.a. The linker then uses this file as if it had been specified precisely by name.
...snip...
Anyway, it worked. This order thing shows that I haven't coded in C for a while. (=
 
  


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
errors with simple C++ Code... openGL problem? poeta_boy Programming 4 11-08-2004 12:34 PM
LD linker code offset problem inuyaga Programming 0 09-25-2004 12:26 PM
LD linker doesn't remove unreachable code ? vips Programming 3 09-08-2004 08:12 AM
problem with trying the libipq example code enjoyzj Programming 4 12-07-2003 07:41 PM
Compile problem with souce code neilcpp Linux - Software 3 10-18-2003 01:53 PM

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

All times are GMT -5. The time now is 06:07 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