0

I'm getting an "undefined reference" error when trying to compile and link a very simple app that has a dependency on a third-party library.

My main.c looks like this:

#include "ss7cp.h"

/*
extern "C" {
  void EINSS7CpMain_CpInit(void);
}
*/

int main() {
  EINSS7CpMain_CpInit();
}

The third-party header file has:

#if defined (__cplusplus) || defined (c_plusplus)
extern "C" {
#endif

...

void EINSS7CpMain_CpInit(void);

The definition of this function is in an archive:

$ nm -g /path/to/lib/libsign_64_uthr.a | grep EINSS7CpMain_CpInit
0000000000005ae0 T EINSS7CpMain_CpInit
                 U EINSS7CpMain_CpInit
                 U EINSS7CpMain_CpInit

By the "T" above, the function must be defined in the text/code section of one of the libs in the archive.

I'm currently not using a Makefile, but just trying to quickly build this simple app entirely from the command line:

g++ -I/path/to/include -L/path/to/lib -lsign_64_uthr -D__EXTENSIONS__ -DLINUX main.c

(The documentation told me to define both __EXTENSIONS__ and LINUX). The rest of the above is pretty straightforward. What I'm getting is:

/tmp/ccvgmIJ8.o: In function `main':
main.c:(.text+0x5): undefined reference to `EINSS7CpMain_CpInit'
collect2: error: ld returned 1 exit status

I've tried both as a C file (main.c) and C++ file (main.cpp, enabling the extern "C" block), but no difference. (Can I assume g++ decides on C versus C++ just by the file extension?)

I've even just compiled the file (with -c) and took a look at the contents of the resultant main.o object file and saw the text "EINSS7CpMain_CpInit" as it is, unmangled (or maybe that's just a debug symbol?)

What are some diagnostics steps I can take to see what I'm missing? Do I actually need to create a Makefile and split the compile and link steps?

It's been ages since I last did any C/C++ and even back when I did it, I usually wasn't the one who had to create the Makefiles from scratch, so I'm probably missing some very fundamental stuff here...

Ates Goral
  • 137,716
  • 26
  • 137
  • 190

1 Answers1

2

My best guess would be parameter ordering, the lib should come after the source file:

g++ -I/path/to/include -D__EXTENSIONS__ -DLINUX main.c -L/path/to/lib -lsign_64_uthr
Csq
  • 5,775
  • 6
  • 26
  • 39