public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Link problems in g++ but not in gcc!!
@ 2004-08-31 12:09 L.Suresh
  2004-08-31 12:18 ` Eljay Love-Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: L.Suresh @ 2004-08-31 12:09 UTC (permalink / raw)
  To: gcc-help

Hi,
I'm having link problems when i compile my program iptables.cpp with
g++.

I have the following:
/usr/lib/libipq.a 
/usr/include/libipq.h 
/usr/share/man/man3/libipq.3.gz

I tried to compile like this.

g++ -o ipqtest iptables.cpp -lipq  <<--- Fails to compile.
g++ -o ipqtest iptables.cpp /usr/lib/libipq.a


gcc -o ipqtest iptables.c -lipq  <-- Compiles!!

I get the following error when i compile with g++. But when i change my
file name iptables.cpp to iptables.c and compile, it compiles without
any problems!!

Symbol list of libipq.a is:
libipq.o:
         U bind
         U close
         U __errno_location
         U fprintf
         U fputc
         U fputs
         U free
         U fwrite
         U getpid
00000260 T ipq_create_handle
00000550 T ipq_ctl
000003a0 T ipq_destroy_handle
00000000 D ipq_errmap
00000000 b ipq_errno
00000560 T ipq_errstr
00000460 T ipq_get_msgerr
00000470 T ipq_get_packet
00000450 T ipq_message_type
00000090 t ipq_netlink_recvfrom
00000050 t ipq_netlink_sendmsg
00000000 t ipq_netlink_sendto
00000580 T ipq_perror
00000440 T ipq_read
000003d0 T ipq_set_mode
00000480 T ipq_set_verdict
00000240 t ipq_strerror
         U malloc
         U recvfrom
         U select
         U sendmsg
         U sendto
         U socket
         U stderr
         U strerror


Error while compiling.

/tmp/ccEVoYgQ.o(.text+0xf): In function `die(ipq_handle*)':
: undefined reference to `ipq_perror(char const*)'
/tmp/ccEVoYgQ.o(.text+0x1d): In function `die(ipq_handle*)':
: undefined reference to `ipq_destroy_handle(ipq_handle*)'
/tmp/ccEVoYgQ.o(.text+0x37): In function `mains(int, char**)':
: undefined reference to `ipq_create_handle(unsigned, unsigned)'
/tmp/ccEVoYgQ.o(.text+0x6f): In function `mains(int, char**)':
: undefined reference to `ipq_set_mode(ipq_handle const*, unsigned char,
unsigned)'
/tmp/ccEVoYgQ.o(.text+0xa6): In function `mains(int, char**)':
: undefined reference to `ipq_read(ipq_handle const*, unsigned char*,
unsigned, int)'
/tmp/ccEVoYgQ.o(.text+0xd2): In function `mains(int, char**)':
: undefined reference to `ipq_message_type(unsigned char const*)'
/tmp/ccEVoYgQ.o(.text+0x104): In function `mains(int, char**)':
: undefined reference to `ipq_get_msgerr(unsigned char const*)'
/tmp/ccEVoYgQ.o(.text+0x12f): In function `mains(int, char**)':
: undefined reference to `ipq_get_packet(unsigned char const*)'
/tmp/ccEVoYgQ.o(.text+0x154): In function `mains(int, char**)':
: undefined reference to `ipq_set_verdict(ipq_handle const*, unsigned,
unsigned, unsigned, unsigned char*)'
collect2: ld returned 1 exit status


Any help would be highly appreciated.

Thanks,
L.Suresh

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Link problems in g++ but not in gcc!!
  2004-08-31 12:09 Link problems in g++ but not in gcc!! L.Suresh
@ 2004-08-31 12:18 ` Eljay Love-Jensen
  2004-08-31 20:35   ` Eljay Love-Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: Eljay Love-Jensen @ 2004-08-31 12:18 UTC (permalink / raw)
  To: suresh, gcc-help

Hi L.Suresh,

It appears that the libipq.a is a C ABI library, but the header file(s) for 
libipq.a are not specified as using the C ABI when compiled with C++.

For example, if you look at one of the those header files (I'm guessing 
"ipq.h") does it have this near the top (AFTER any included header files in 
the ipq.h):

#ifdef __cplusplus
extern "C" {
#endif

...and this near the bottom...

#ifdef __cplusplus
}
#endif

If it doesn't have those C++ guards, then to include the C++-ignorant C 
header file, you should do this in your iptables.cpp file (I'm just making 
them up off the top of my head):

#include "iptable.h"
extern "C" {
#include "ipq.h"
}
#include <iostream>
#include <cstddef>
#include <cassert>

Or what I've seen developers do is make a ipq.hpp file that only contains this:
extern "C" {
#include "ipq.h"
}

Then your C++ code doesn't look so polluted, and just contains this:
#include "iptable.h"
#include "ipq.hpp"
#include <iostream>
#include <cstddef>
#include <cassert>

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Link problems in g++ but not in gcc!!
  2004-08-31 12:18 ` Eljay Love-Jensen
@ 2004-08-31 20:35   ` Eljay Love-Jensen
  0 siblings, 0 replies; 3+ messages in thread
From: Eljay Love-Jensen @ 2004-08-31 20:35 UTC (permalink / raw)
  To: suresh, gcc-help

Hi L.Suresh,

CAVEAT!

If you make an "ipq.hpp" header file that only contains this...
extern "C" {
#include "ipq.h"
}

...that's just fine if ipq.h does not, itself, include any header files.

But if it does include any header files, your ipq.hpp should include those 
header files first.  And you should use the same guard that ipq.h uses -- 
but don't DEFINE it, just trigger off of it.
#ifndef ipq_h_once
#include "alpha.h"
#include <stdio.h>
extern "C" {
#include "ipq.h"
}
#endif

You don't want to compromise those other header files by extern "C"-ing them.

If the made-up "alpha.h" is, itself, in the same boat, then do this...
#ifndef ipq_h_once
#include "alpha.hpp" // "C" wrapper around alpha.h
#include <stdio.h>
extern "C" {
#include "ipq.h"
}
#endif

It can turn into a mighty be can-o'-worms if there are lots and lots of 
header files.  Might be easier to instrument (edit) the C header files with 
the...
#ifdef __cplusplus
extern "C" {
#endif

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-08-31 12:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-31 12:09 Link problems in g++ but not in gcc!! L.Suresh
2004-08-31 12:18 ` Eljay Love-Jensen
2004-08-31 20:35   ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).