简体   繁体   中英

undefined reference to `ftdi_init'

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 to ftdi_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?

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

It's better to use the packaging system's help program, ie 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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM