0

I want to use fftw3 library. Header and dll lies in fftw3 folder.

I've read that to use a dll you should make a CMakeLists.txt inside that folder with this content:

cmake_minimum_required(VERSION 3.20)
message("### Linking FFTW")
add_library(fftw3lib libfftw3-3.dll)
set_target_properties(fftw3lib PROPERTIES LINKER_LANGUAGE CXX)

In my top-level CMakeLists.txt I have:

cmake_minimum_required(VERSION 3.20)
project(abc)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)

include_directories("${PROJECT_SOURCE_DIR}/fftw3")
add_subdirectory(fftw3)

add_executable(abc main.cpp)
target_link_libraries(abc fftw3lib)

main.cpp:

#include <iostream>
#include "fftw3/fftw3.h"

using namespace std;

int main() {
    int n = 1024;
    int m = 1024;

    fftw_complex* inMtx = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * n * m);
    fftw_complex* outMtx = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * n * m);

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            inMtx[i * n + j][0] = 1;
            inMtx[i * n + j][1] = 1;
        }
    }

    fftw_init_threads();
    auto plan = fftw_plan_dft_2d(n, m,
                                 inMtx, outMtx,
                                 FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_execute(plan);

    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < 10; ++j) {
            cout << outMtx[i * n + j][0] << "\t";
        }
        cout << endl;
    }

    fftw_destroy_plan(plan);
    fftw_cleanup_threads();

    fftw_free(inMtx);
    fftw_free(outMtx);


    return 0;
}

When I run it in CLion the output says:

CMakeFiles\abc.dir/objects.a(main.cpp.obj): In function `main':
./abc/main.cpp:15: undefined reference to `__imp_fftw_malloc'
./abc/main.cpp:16: undefined reference to `__imp_fftw_malloc'
./abc/main.cpp:25: undefined reference to `__imp_fftw_init_threads'
./abc/main.cpp:26: undefined reference to `__imp_fftw_plan_dft_2d'
./abc/main.cpp:29: undefined reference to `__imp_fftw_execute'
./abc/main.cpp:38: undefined reference to `__imp_fftw_destroy_plan'
./abc/main.cpp:39: undefined reference to `__imp_fftw_cleanup_threads'
./abc/main.cpp:41: undefined reference to `__imp_fftw_free'
./abc/main.cpp:42: undefined reference to `__imp_fftw_free'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\abc.dir\build.make:99: abc.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:102: CMakeFiles/abc .dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:109: CMakeFiles/abc .dir/rule] Error 2
mingw32-make.exe: *** [Makefile:126: abc] Error 2

When I compile it in cmd by hands like that: (and copy dll manually in same folder as a.exe)

g++ -I ./fftw3/ -L ./fftw3/ -lfftw3-3 main.cpp

The program runs fine. I tried some things also but it's too long to tell everything.

While True
  • 413
  • 2
  • 15
  • 2
    I think you want to create an IMPORTED library: [https://cmake.org/cmake/help/latest/command/add_library.html#imported-libraries](https://cmake.org/cmake/help/latest/command/add_library.html#imported-libraries) – drescherjm Sep 08 '21 at 13:37

0 Answers0