Home > Software engineering >  Inlcude fftw3.h with cmake in a linux environment
Inlcude fftw3.h with cmake in a linux environment

Time:09-24

I want to use fftw library.

In my cpp file I included as #include <fftw3.h>

I downloaded the library and I saved it in ./fftw Then I typed:

cd fftw
./configure
make
make install
cd ..

In my project I am already using OpenCV, so my CMakeLists.txt is:

cmake_minimum_required(VERSION 2.8) # CMake version check
project( main ) 
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( main main.cpp )
# set(CMAKE_BUILD_TYPE Debug)
target_link_libraries( main ${OpenCV_LIBS} )
target_link_libraries( main ./fftw)

Finally I execute cmake CMakeLists.txt but when I try to make my executable, it complains that fftw3.h: No such file or directory.

How could I make it work?

Thanks.

CodePudding user response:

You can also use ./configure --prefix /where/you/want/to/install to set the directory you want the library to be installed in then make sure that you run make install inside ./fftw directory.

You may need to use sudo make install depending on the path and permissions.

Change your CMakeLists.txt to include the following:

cmake_minimum_required(VERSION 2.8) # CMake version check
project( main ) 
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( main main.cpp )
# set(CMAKE_BUILD_TYPE Debug)
target_link_libraries( main ${OpenCV_LIBS} )
target_link_libraries( main fftw3 )

Then execute cmake CMakeLists.txt and finally make


The above build's correctly on the following platform:

OS: Ubuntu Linux 21.04

Kernel: 5.11.0-34-generic

cmake Version: 3.18.4

g : 10.3.0


Example main.cpp used:

#include <fftw3.h>

#define NUM_POINTS 64


#include <stdio.h>
#include <math.h>

#define REAL 0
#define IMAG 1

void acquire_from_somewhere(fftw_complex* signal) {
    /* Generate two sine waves of different frequencies and
     * amplitudes.
     */

    int i;
    for (i = 0; i < NUM_POINTS;   i) {
        double theta = (double)i / (double)NUM_POINTS * M_PI;

        signal[i][REAL] = 1.0 * cos(10.0 * theta)  
                          0.5 * cos(25.0 * theta);

        signal[i][IMAG] = 1.0 * sin(10.0 * theta)  
                          0.5 * sin(25.0 * theta);
    }
}

void do_something_with(fftw_complex* result) {
    int i;
    for (i = 0; i < NUM_POINTS;   i) {
        double mag = sqrt(result[i][REAL] * result[i][REAL]  
                          result[i][IMAG] * result[i][IMAG]);

        printf("%g\n", mag);
    }
}


int main() {
    fftw_complex signal[NUM_POINTS];
    fftw_complex result[NUM_POINTS];

    fftw_plan plan = fftw_plan_dft_1d(NUM_POINTS,
                                      signal,
                                      result,
                                      FFTW_FORWARD,
                                      FFTW_ESTIMATE);

    acquire_from_somewhere(signal);
    fftw_execute(plan);
    do_something_with(result);

    fftw_destroy_plan(plan);

    return 0;
}
  • Related