0

I'm making a program that needs to be linked to a third party SDK which comes in the form of header files and precompiled shared libraries.

The program itself is simple, it just calls one function from the library:

#include <dhnetsdk.h>

int main() {
    CLIENT_Init(nullptr, 0);
}

Then I compile it with

g++ -std=c++17 -O3 -ldhnetsdk -lavnetsdk -ldhconfigsdk -lInfra -lNetFramework -lStream -lStreamSvr trigger/src/main.cpp -Lsdk/bin -Isdk/include

sdk/bin contains the .so files and sdk/include contains the dhnetsdk.h file where CLIENT_Init is declared as extern "C" int CLIENT_Init(void(*)(long long, char*, long), long);

I have checked with nm and sdk/bin/libdhnetsdk.so indeed exports the CLIENT_Init symbol, but for some reason the linker is not able to find it and fails with:

/usr/bin/ld: /tmp/ccovgfxa.o: in function `main':
main.cpp:(.text.startup+0x9): undefined reference to `CLIENT_Init'
collect2: error: ld returned 1 exit status

What am I doing wrong here?

Ðаn
  • 10,934
  • 11
  • 59
  • 95
hoz
  • 176
  • 1
  • 7
  • 1
    Five bucks says the answers in that link, but 10 bucks says you won't find the correct answer in there unless you try each answer one by one or already know what the problem is. Darhuuuk's probably right and it's ordering. I'd definitely start there. – user4581301 Mar 18 '20 at 16:26
  • Yeah it's ordering. Drag `trigger/src/main.cpp` to the beginning. – user4581301 Mar 18 '20 at 16:28

2 Answers2

0

To order you provide the libraries to link with is probably wrong. See e.g. this answer: https://stackoverflow.com/a/24675715.

Now, you can either try to figure out what order the -l arguments should be in. Or let ld figure it out by creating a group of libraries. ld will search within this group until it finds the necessary function (or determines that it's not available). You do this using ld's --start-group and --end-group options:

g++ -std=c++17 -O3 -Lsdk/bin -Isdk/include trigger/src/main.cpp \
  -Wl,--start-group -ldhnetsdk -lavnetsdk -ldhconfigsdk -lInfra \
    -lNetFramework -lStream -lStreamSvr -Wl,--end-group

Edit: Fixed so the source file comes before the libraries.

AVH
  • 11,349
  • 4
  • 34
  • 43
0

In the end I found out it was all about ordering and I needed to put src/trigger/main.cpp before all the libraries which solved the problem.

hoz
  • 176
  • 1
  • 7