I'm doing my first C project in, maybe, 15 years, and have gotten stuck.

I'm trying to link a 3rd party library into my project. I've installed the headers in ./include/ft and the libs in ./lib/ft

Here's my compiler command and associated output:

[root@wayne tele]# g++ -g  lib/ft/libmbusmaster.a   -lm -lz -o client  client.o
client.o: In function `mbOpenTCP(char*)':
/home/tele/tele/client.c:49: undefined reference to `mbusMaster_createTcpProtocol'
/home/tele/tele/client.c:50: undefined reference to `mbusMaster_openTcpProtocol'
/home/tele/tele/client.c:54: undefined reference to `mbusMaster_getErrorText'
collect2: ld returned 1 exit status

I have the right library, based on:

[root@wayne tele]# strings lib/ft/libmbusmaster.a | grep mbusMaster_createTcpProtocol
mbusMaster_createTcpProtocol
mbusMaster_createTcpProtocol

I include it via: #include "include/ft/MbusMasterCexports.h" I've gone through all the standard PATH checks, and case-sensitive checks. The environment is a Fedora 8 box.

The third party library is one I'm considering buying, but was trying to get a sample project compiled before I commit. No tech support from the third party until I purchase, obviously.

Hmm this seems strange but after a little testing with my own code samples I managed to produce the same error (obviously with different files) when I intentionally failed to link with a library that I needed. Perhaps the library that you are linking with has other dependencies. Either that or you aren't linking correctly; although I can't see anything wrong with the commands that you are using.

Ah, thanks. It's good to know I didn't make a dumb mistake.

How do I know what other libs a third party lib requires, if none is explicitly mentioned in the docs?

Perhaps I should just commit to buying this lib and rely on their tech support, since i have frightfully little time to complete this project.

Instead of lib/ft/libmbusmaster.a on the g++ command line try -llib/ft/libmbusmaster.a or -l lib/ft/libmbusmaster.a

Those two suggestions result in a Not Found situation, but adding back in the path ad switching to the libXXX.a form, I get this same result:

[root@wayne tele]# g++ -g -L lib/ft  -lmbusmaster   -lm -lz -o client  client.o
client.o: In function `mbOpenTCP(char*)':
/home/tele/tele/client.c:49: undefined reference to `mbusMaster_createTcpProtocol'
/home/tele/tele/client.c:50: undefined reference to `mbusMaster_openTcpProtocol'
/home/tele/tele/client.c:54: undefined reference to `mbusMaster_getErrorText'
collect2: ld returned 1 exit status

I would have thought that any necessary dependencies would have been stated. I have a colleague at work who has a similar problem to this and he said he found some software online that checks for dependencies against a database. This didn't work for him however as the database had no information on the libraries that he was dealing with. I do not know if he has resolved his problems yet.

I got it to work. This is my compile and link commands:

gcc -g -O2 -c -Iinclude/ft -o client.o client.c
	g++ -o client client.o lib/ft/libmbusmaster.a -lrt

I got that from reverse engineering the included sample Makefile.

I confess I don't know WHY it now works, as it's very similar to what I had initially. Any experienced eye care to shed some light on this for me?

I know why (I think), IIRC the linker does a single pass of the files, when you have lib/ft/libmbusmaster.a before client.o it looks in lib/ft/libmbusmaster.a and because it hasn't seen anything yet that uses it discards all of it. Then it tries to link client.o and finds lots of unresolved externals.

The other way round in looks in client.o first, finds lots of unresolved externals then looks in lib/ft/libmbusmaster.a and resolves them.

Your deduction sounds logical to me. It's easy to understand why you had problems, it's not the first thing that springs to mind when you see those kind of errors :-/
You should mark this thread as solved so that others can reference it. I'll show my colleague actually as this may well solve his problem.

Edited 6 Years Ago by Valaraukar: n/a

Order is Important!!

This works: g++ -o client client.o lib/ft/libmbusmaster.a -lrt This does not: g++ -o client lib/ft/libmbusmaster.a client.o -lrt

This question has already been answered. Start a new discussion instead.