LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   compile/linker problem with simple libipq code (https://www.linuxquestions.org/questions/programming-9/compile-linker-problem-with-simple-libipq-code-320735/)

TheLinuxDuck 05-06-2005 10:10 AM

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?

Hivemind 05-06-2005 10:16 AM

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.

TheLinuxDuck 05-06-2005 11:00 AM

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.

Hivemind 05-06-2005 11:15 AM

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

Hivemind 05-06-2005 11:21 AM

I was too quick to reply. Please see my edited version.

TheLinuxDuck 05-06-2005 11:34 AM

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::

Hivemind 05-06-2005 11:39 AM

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.

TheLinuxDuck 05-06-2005 01:28 PM

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. (=


All times are GMT -5. The time now is 01:23 AM.