undefined reference to `crypt'

Posted by Benodach on Wed 07 Dec 2011 02:34 PM — 3 posts, 21,852 views.

#0
Should be an easy one...but it's tripping me up. I am running ROM 2.4 on Ubuntu. The crypt() calls have never given me a problem since I always link with -lcrypt in the Makefile.

However, the most recent OS update, which I believe included a gcc/glibc update, is now giving me the "undefined reference to 'crypt'" errors when it comes time to link.

My calls to crypt() in comm.c and act_info.c remain unchanged from stock code and I have not messed with the prototypes anywhere.

I'm running the latest version of Ubuntu which uses Linux 3.0, gcc 4.6.1 and glibc 2.13.

As I said above, crypt() has never given me problems as long as I link with -lcrypt but now it is. In the past, I have not needed to include crypt.h or unistd.h but when I tried including these it didn't fix the errors (in fact, the read() and write() prototypes in unistd.h cause further problems with conflicts in comm.c).

Any insight would be greatly appreciated. Thanks!
#1
I went back to the basics and (re)read the gcc man page...something I haven't done in well over 10 years. This paid off, as I noticed that within the command line syntax of gcc, it matters where you place the -l argument.

Thus, I adjusted my Makefile to reflect placing -lcrypt at the end of the command line and voila - problem solved. While my previous syntax was fine for earlier versions of gcc, it appears that is no longer the case.

So I presume gcc was now linking the crypt library after the object files in question and thus the call to crypt() was undefined.

I eventually figured it out but I'm re-posting here in the event someone else stumbles on this in the future.
Australia Forum Administrator #2
Oh yeah, sorry I didn't reply sooner. The positional linking of libraries can trip you up a bit. It's because if you put it too early on the (linker) command line the linker sees no need to include it, so you need to put the "main" project early, and then things it needs later.