Using fftw3 in CMakeLists in CLion

76 Views Asked by At

I am using CLion, and test the fftw3 just installed from http://www.fftw.org/download.html

I wrote a CMakeLists.txt

cmake_minimum_required(VERSION 3.27)
project(test)

set(CMAKE_CXX_STANDARD 14)

add_executable(test
        test.cpp)

target_link_libraries(test fftw3.h)

Also the source file is called test.cpp which contains some testing codes from ChatGPT3.5

#include <iostream>
#include <fftw3.h>
#include <complex>
 using namespace std;

int main() {
    // Size of input data
    const int N = 8;
    
    // Allocate input and output arrays
    double *in = (double*) fftw_malloc(sizeof(double) * N);
    fftw_complex *out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * (N/2 + 1));
    
    // Create a plan for forward FFT
    fftw_plan plan = fftw_plan_dft_r2c_1d(N, in, out, FFTW_ESTIMATE);
    
    // Initialize input data (example)
    for (int i = 0; i < N; ++i) {
        in[i] = i; // Example input data
    }
    
    // Execute FFT
    fftw_execute(plan);
    
    // Output FFT result
    for (int i = 0; i < N/2 + 1; ++i) {
        std::cout << "FFT[" << i << "] = " << out[i][0] << " + " << out[i][1] << "i" << std::endl;
    }
    
    // Clean up
    fftw_destroy_plan(plan);
    fftw_free(in);
    fftw_free(out);
}

When I ran the test program, it shows error message:

/Library/Developer/CommandLineTools/usr/bin/c++   -g -std=gnu++14 -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.2.sdk -fcolor-diagnostics -MD -MT CMakeFiles/test.dir/test.cpp.o -MF CMakeFiles/test.dir/test.cpp.o.d -o CMakeFiles/test.dir/test.cpp.o -c /Users/john/Desktop/test/test.cpp
/Users/john/Desktop/test/test.cpp:2:10: fatal error: 'fftw3.h' file not found
#include <fftw3.h>
         ^~~~~~~~~
1 error generated.
ninja: build stopped: subcommand failed.

But when I just manually compiled it in command with

g++ -o test test.cpp -lfftw3 -lm

It generates an test.exe file which works with correct outputs from std::cout ... in the source codes.

When I locate fftw3.h files using locate fftw3.h in commands, it shows several positions:

/Applications/MATLAB_R2023b.app/extern/include/fftw3.h
/Users/john/.conda/envs/eq1/include/fftw3.h
/Users/john/.conda/envs/eq2/include/fftw3.h
/Users/john/.conda/envs/eq3/include/fftw3.h
/Users/john/.conda/envs/eq4/include/fftw3.h
/opt/anaconda3/pkgs/fftw-3.3.9-h9ed2024_1/include/fftw3.h

I tried to change the CMakeLists.txt to

target_link_libraries(test PRIVATE /opt/anaconda3/pkgs/fftw-3.3.9-h9ed2024_1/include/fftw3.h)

Along with

#include </opt/anaconda3/pkgs/fftw-3.3.9-h9ed2024_1/include/fftw3.h> 

in test.cpp source file. But this time, the error message is

/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/bin/cmake --build /Users/john/Desktop/test/cmake-build-debug --target test -j 8
[0/1] Re-running CMake...
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/john/Desktop/test/cmake-build-debug
[2/2] Linking CXX executable test
FAILED: test 
: && /Library/Developer/CommandLineTools/usr/bin/c++ -g -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.2.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/test.dir/test.cpp.o -o test  /opt/anaconda3/pkgs/fftw-3.3.9-h9ed2024_1/include/fftw3.h && :
clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated [-Wdeprecated]
clang: error: cannot specify -o when generating multiple output files
ninja: build stopped: subcommand failed.

But once again, if I complied this test.cpp in commands then it still works with that path /opt/....

Indeed, I downloaded the FFTW package from their website and locate it in /users/john/Documents/fftw-3.3.10 with ./configure && make && sudo make install to install it. It shows in their GitHub webpage, https://github.com/FFTW/fftw3

``In particular, do not expect things to work by simply executing configure; make or cmake."

And my question is, how to write a correct CMakeLists in CLion to use fftw3. Since it might be non-trivial to use CMake for FFTW, and I don't have a solid computer background. I am looking for solutions for this.


UPDATE: using brew install

I tired to install fftw using brew install fftw And it is now in the path /opt/homebrew/Cellar/fftw/3.3.10_1 And I can find fftw3.h header file in /opt/homebrew/Cellar/fftw/3.3.10_1/include

Based on this, I changed my CMakeLists.txt to

cmake_minimum_required(VERSION 3.27 FATAL_ERROR)

project (test)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")

set(FFTW_INCLUDE_DIRS "/opt/homebrew/Cellar/fftw/3.3.10_1/include")
set(FFTW_LIBRARIES "/opt/homebrew/Cellar/fftw/3.3.10_1/lib/libfftw3.a")
target_include_directories(${FFTW_INCLUDE_DIRS})

add_executable (test test.cpp)
target_link_libraries(test PRIVATE ${FFTW_INCLUDE_DIRS})

But it shows the error message:

c++ /Users/john/Desktop/test/test.cpp -o test
ld: Undefined symbols:
  _fftw_destroy_plan, referenced from:
      _main in test-63a277.o
  _fftw_execute, referenced from:
      _main in test-63a277.o
  _fftw_plan_r2r_1d, referenced from:
      _main in test-63a277.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

But the good news is that there is no error in #include <fftw3.h>

since I have set the path to fftw3.h in CMakeLists.txt

I finally tried just mimicking How to include external library (boost) into CLion C++ project with CMake?

project(test)

message(STATUS "start running cmake...")

find_package(fftw 3.3.10_1 COMPONENTS system filesystem REQUIRED)

if(fftw_FOUND)

    message(STATUS "fftw_INCLUDE_DIRS: ${fftw_INCLUDE_DIRS}")
    message(STATUS "fftw_LIBRARIES: ${fftw_LIBRARIES}")
    message(STATUS "fftw_VERSION: ${fftw_VERSION}")

    include_directories(${fftw_INCLUDE_DIRS})

endif()

add_executable(test test.cpp)

if(fftw_FOUND)

    target_link_libraries(fftw ${fftw_LIBRARIES})

endif()

Still fail to work unfortunately...

0

There are 0 best solutions below