Search code examples
ccompiler-errorsmcrypt

Cannot compile program on Ubuntu that uses libmcrypt


I created a C program that uses libmcrypt. I compiled it for many times in a CentOS machine, but now, trying to do the same thing in Ubuntu, I get this:

/tmp/ccM2ugbq.o: In function `encrypt': 
util.c:(.text+0xa1): undefined reference to `mcrypt_module_open'
util.c:(.text+0xc6): undefined reference to `mcrypt_enc_get_iv_size'
util.c:(.text+0x134): undefined reference to `mcrypt_generic_init'
util.c:(.text+0x1dc): undefined reference to `mcrypt_generic'
util.c:(.text+0x1fa): undefined reference to `mcrypt_module_close'
/tmp/ccM2ugbq.o: In function `decrypt':
util.c:(.text+0x288): undefined reference to `mcrypt_module_open'
util.c:(.text+0x296): undefined reference to `mcrypt_enc_get_iv_size'
util.c:(.text+0x304): undefined reference to `mcrypt_generic_init'
util.c:(.text+0x375): undefined reference to `mdecrypt_generic'
util.c:(.text+0x380): undefined reference to `mcrypt_module_close'

, although I have installed libmcrypt and have spacified the flags properly when compile (-I/usr/include -L/usr/lib -lmcrypt)

Can anyone guide me on how to get the compilation work properly with libmcrypt?


EDIT: man pages says: Compile as "cc prog.c -lmcrypt", or "cc prog.c -lmcrypt -lltdl" depending on your installation. I also installed libltdl-dev, but it does not work.


EDIT 1: System information: version (running uname -r):

3.5.0-25-generic

release (running lsb_release -a):

No LSB modules are available.

Distributor ID: Ubuntu

Description: Ubuntu 12.10

Release: 12.10

Codename: quantal


EDIT 2:

Because @teppic requested me to show the compile command, I solved my issue, but I need some explanations. Bellow I'll detail the topic:

the command that I used and failed:

cc -Wall -I../path/to/include -lmcrypt ./a_file.c ./some/other/file.c ./some/other/files.c ./main/file.c -I/some/include/path -lother_lib -o ./bin/out

After that, I only put the -lmcrypt at the end of command and deleted it from the first place, and it worked. In my initial question, I told you that I did compile it before in CentOS and everything worked fine. That's true, even if in CentOS the compile command were the former one (probably some files differ in order or some libraries).

Can anybody explain me how the flags order influences the compilation success or failure?


Solution

  • In general you should put the library dependency after the source file that references it in the command line. In older versions of gcc, this wasn't always the case.