mgiuffrida
mgiuffrida

Reputation: 3579

gcc fails with "undefined reference" in Cygwin, but the reference exists in a linked library

I've built and installed libmarpa in Cygwin with the end result being in /usr/local/lib/libmarpa.a.

I have a simple file:

#include "libmarpa/dist/marpa.h"

int main() {
  return marpa_check_version(8, 3, 0);
}

But the linker fails to find marpa_check_version:

$ gcc test.cc -L/usr/local/lib -lmarpa
/tmp/ccdYM1vV.o:test.cc:(.text+0x1e): undefined reference to `marpa_check_version(int, int, int)'
/tmp/ccdYM1vV.o:test.cc:(.text+0x1e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `marpa_check_version(int, int, int)'
collect2: error: ld returned 1 exit status

But the symbol exists as a function:

$ nm /usr/local/lib/libmarpa.a | grep marpa_check_version
0000000000002780 T marpa_check_version

So what's happening here? Is there a problem trying to do this within Cygwin, or am I invoking gcc incorrectly?

Upvotes: 2

Views: 3724

Answers (1)

Employed Russian
Employed Russian

Reputation: 213935

This symbol:

undefined reference to `marpa_check_version(int, int, int)'

is C++ mangled. This symbol is not:

0000000000002780 T marpa_check_version

The problem is that marpa.h developers did not expect their code to be used by C++, and have not put in proper guards for this. You can fix the problem like so:

extern "C" {
#include "libmarpa/dist/marpa.h"
}

int main() { ...as before ...

P.S. You should also change your command line to use g++ instead of gcc. Contrary to popular belief, they are not the same thing.

Upvotes: 3

Related Questions