2

I have used libftdi in the past and compiled using the command:

gcc -lftdi -o i2csend i2csend.c

Everything went fine. Today, on Ubuntu 12.10 I get many errors such as undefined reference toftdi_init'`

I understand that libftdi was renamed to libftdi1 so I tried the same command with -lftdi1 and got error:

/usr/bin/ld: cannot find -lftdi1 collect2: error: ld returned 1 exit status

Can anyone explain why?

Aditi
  • 1,188
  • 2
  • 16
  • 44
Ori Idan
  • 153
  • 1
  • 9

1 Answers1

5

You should typically not directly specify external package's library names.

It's better to use the packaging system's help program, i.e. pkg-config, like so:

$ gcc -o i2csend i2csend.c $(pkg-config --cflags --libs libftdi1)

Note that this assumes that the package name is libftdi1 in pkg-config's database; I'm not sure how to verify this portably. You can run pkg-config --list-all | grep ftdi to find out.

It's generally a good idea to keep the libraries part (-l option) at the end of the command line, which the above is doing. It's somewhat cleaner to factor out the CFLAGS part, but that requires repeating the command:

$ gcc $(pkg-config --cflags libftdi1)  -o i2csend  i2csend.c  $(pkg-config --libs libftdi1)

Here, I've used double spaces to separate the logical parts of the command line for improved clarity.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • you have to **always** warn people not to move their sources to the end of the command line (I learned it the hard way here on SO). They **always** tend to do it for some fscking aesthetical reasons. – Anton Kovalenko Feb 14 '13 at 10:45