/usr/bin/ld cannot find -lfftw3?
Categories:
Resolving '/usr/bin/ld: cannot find -lfftw3' Linker Errors

Encountering 'cannot find -lfftw3' during compilation indicates a common linker issue with the FFTW library. This guide provides comprehensive solutions for C/C++ projects using Makefiles.
When compiling C or C++ programs that utilize the Fast Fourier Transform in the West (FFTW) library, you might encounter a linker error similar to /usr/bin/ld: cannot find -lfftw3
. This error signifies that the linker (ld
) is unable to locate the FFTW library files required to resolve external symbols. This article will walk you through the common causes of this issue and provide practical solutions, focusing on proper library installation, linker path configuration, and Makefile adjustments.
Understanding the Linker Error
The -lfftw3
flag tells the linker to search for a library named libfftw3.so
(shared library) or libfftw3.a
(static library) in its configured search paths. When this error occurs, it typically means one of the following:
- FFTW is not installed: The library files simply don't exist on your system.
- FFTW is installed, but not in a standard linker path: The library files are present, but the linker doesn't know where to look for them.
- Incorrect library name or version: You might be requesting
fftw3
but havefftw3f
(single precision) orfftw3l
(long double precision) installed, or a different version. - 64-bit vs. 32-bit mismatch: On some systems, 32-bit libraries are in a different path than 64-bit ones, and your compilation might be targeting the wrong architecture.
flowchart TD A[Start Compilation] --> B{Linker Error: cannot find -lfftw3?} B -->|Yes| C{Is FFTW installed?} C -->|No| D[Install FFTW Library] C -->|Yes| E{Is FFTW in standard path?} E -->|No| F[Add Library Path to Linker] E -->|Yes| G{Correct library name/version?} G -->|No| H[Verify Library Name/Version] G -->|Yes| I{Architecture mismatch?} I -->|No| J[Check Makefile/Compiler Flags] I -->|Yes| K[Specify Correct Architecture Flags] D --> L[Recompile] F --> L H --> L K --> L J --> L L --> M[Success!]
Troubleshooting Flow for 'cannot find -lfftw3' Error
Solution 1: Install FFTW Library
The most straightforward solution is to ensure FFTW is installed on your system. Use your distribution's package manager. For development, you'll typically need the development headers and libraries.
# Debian/Ubuntu
sudo apt-get update
sudo apt-get install libfftw3-dev
# Fedora/RHEL/CentOS
sudo dnf install fftw-devel
# Arch Linux
sudo pacman -S fftw
# macOS (using Homebrew)
brew install fftw
Installing FFTW development packages on various Linux distributions and macOS.
Solution 2: Specify Library Path (LDFLAGS)
If FFTW is installed but not in a directory the linker automatically searches (e.g., /usr/local/lib
, /usr/lib
), you need to tell the linker where to find it. This is done using the -L
flag, typically passed via the LDFLAGS
variable in a Makefile or directly to the compiler command.
First, locate your FFTW library files. They are usually named libfftw3.so
, libfftw3.a
, libfftw3f.so
, etc. You can use find
or locate
:
sudo find / -name "libfftw3*.so" 2>/dev/null
# Example output: /opt/fftw/lib/libfftw3.so
Finding FFTW shared library files.
Once you have the path (e.g., /opt/fftw/lib
), add it to your compilation command or Makefile:
CC = gcc
CFLAGS = -Wall -I/opt/fftw/include
LDFLAGS = -L/opt/fftw/lib
LDLIBS = -lfftw3 -lm
myprogram: myprogram.o
$(CC) $(LDFLAGS) myprogram.o -o myprogram $(LDLIBS)
Example Makefile snippet adding custom FFTW library path.
LD_LIBRARY_PATH
environment variable (for runtime) or configure /etc/ld.so.conf.d/
(for system-wide linker search paths, requires sudo ldconfig
). However, modifying LDFLAGS
in the Makefile is generally preferred for project-specific dependencies.Solution 3: Verify Library Name and Precision
FFTW offers different versions for various data types:
-lfftw3
: Double precision (default)-lfftw3f
: Single precision (float)-lfftw3l
: Long double precision
Ensure that the library you are linking against (-lfftw3
, -lfftw3f
, or -lfftw3l
) matches the FFTW library installed on your system and the precision used in your C/C++ code. If you're using float
types for FFTW functions, you likely need to link with -lfftw3f
.
#include <fftw3.h>
int main() {
// For double precision (default)
fftw_complex *in_double, *out_double;
fftw_plan plan_double;
// For single precision (float)
fftwf_complex *in_float, *out_float;
fftwf_plan plan_float;
// For long double precision
fftwl_complex *in_long_double, *out_long_double;
fftwl_plan plan_long_double;
return 0;
}
FFTW function prefixes indicate the required library precision.
Solution 4: Address Architecture Mismatches
On 64-bit systems, 32-bit libraries might be installed in a different directory (e.g., /usr/lib32
or /usr/lib/i386-linux-gnu
). If you are compiling a 32-bit application on a 64-bit system, you might need to explicitly tell the linker to look in these paths and ensure you have the 32-bit FFTW libraries installed.
# For 32-bit compilation on a 64-bit system
gcc -m32 myprogram.c -o myprogram -L/usr/lib32 -lfftw3 -lm
# Install 32-bit development libraries (Debian/Ubuntu example)
sudo apt-get install libfftw3-dev:i386
Compiling for 32-bit architecture and installing 32-bit FFTW libraries.
Final Checks and Best Practices
After trying the above solutions, always clean your build directory and recompile. A common mistake is that previous build artifacts might be causing issues.
- Clean Build: Use
make clean
or manually remove.o
files and the executable. - Verbose Output: Add
-v
to yourgcc
org++
command to see detailed compilation and linking steps, which can help identify the exact paths being searched. - pkg-config: For more complex projects,
pkg-config
is an excellent tool to manage compiler and linker flags automatically. If FFTW is installed correctly, you can use it like this:gcc myprogram.c -o myprogram $(pkg-config --cflags --libs fftw3)
CC = gcc
CFLAGS = -Wall
# Use pkg-config if available
FFTW_CFLAGS = $(shell pkg-config --cflags fftw3 2>/dev/null)
FFTW_LIBS = $(shell pkg-config --libs fftw3 2>/dev/null)
# Fallback if pkg-config is not found or FFTW not configured for it
ifndef FFTW_LIBS
FFTW_CFLAGS = -I/usr/local/include
FFTW_LIBS = -L/usr/local/lib -lfftw3 -lm
endif
myprogram: myprogram.o
$(CC) $(CFLAGS) $(FFTW_CFLAGS) myprogram.o -o myprogram $(FFTW_LIBS)
myprogram.o: myprogram.c
$(CC) $(CFLAGS) $(FFTW_CFLAGS) -c myprogram.c
clean:
rm -f myprogram myprogram.o
Advanced Makefile using pkg-config for FFTW, with a fallback.