gpt4 book ai didi

c++ - 通过 CMake 连接 fftw3 库

转载 作者:行者123 更新时间:2023-12-02 10:03:18 50 4
gpt4 key购买 nike

该项目提供了一个带有测试的静态库。我添加 fftw3库将其作为子模块,但 Microsoft Visual Studio 2017 找不到该库。

项目结构:

    └───fftw3
└───googletest
└───output_64
└───src
└───tests
└───CMakeLists.txt
└───tests.cpp
└───CMakeLists.txt
└───FFT_lib.cpp
└───FFT_lib.h
└───.gitignore
└───.gitmodules
└───CMakeLists.txt
└───config_folder_and_run_cmake.bat

config_folder_and_run_cmake.bat
git submodule init
git submodule update

"c:\Program Files\CMake\bin\cmake.exe" ^
-H. ^
-Boutput_64 ^
-G"Visual Studio 15 2017 Win64" ^
%*
pause

CMakeLists.txt
cmake_minimum_required(VERSION 3.9.3)

#set the project name
project(FFT)

#set the directories into which libraries and executable files will be collected
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

if(MSVC)
add_compile_options(
$<$<CONFIG:>:/MT>
$<$<CONFIG:Debug>:/MTd>
$<$<CONFIG:Release>:/MT>
)
endif()

set(CMAKE_CXX_STANDARD 14)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

set(SRC_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/src)

add_subdirectory(src)
add_subdirectory(googletest)
add_subdirectory(fftw3)

.gitmodules
[submodule "googletest"]
path = googletest
url = https://github.com/google/googletest
[submodule "fftw3"]
path = fftw3
url = https://github.com/FFTW/fftw3

src\CMakeLists.txt
set(SOURCES
FFT_lib.h
FFT_lib.cpp
)

add_library(FFT_lib ${SOURCES})

target_include_directories(FFT_lib PUBLIC ${SRC_ROOT_DIR})

source_group(TREE ${CMAKE_CURRENT_LIST_DIR} FILES ${SOURCES})

#in solution, this library is located in the FFT_lib folder
set_property(TARGET FFT_lib PROPERTY FOLDER "FFT_lib")

add_subdirectory(tests)

src\tests\CMakeLists.txt
set(SOURCES
tests.cpp
)

add_executable(FFT_test ${SOURCES})

# 1. Test library
# 2. gest
# 3. fftw3
target_link_libraries(FFT_test PUBLIC FFT_lib)
target_link_libraries(FFT_test PUBLIC gtest_main)
target_link_libraries(FFT_test PUBLIC fftw3)

set_property(TARGET FFT_test PROPERTY FOLDER "tests/FFT_test")

src\tests\tests.cpp
#include <gtest/gtest.h>


#include "FFT_lib.h"
#include <fftw3.h> //cannot open source file

#include <complex>
#include <vector>


TEST(simple_test, test)
{
using namespace std;
vector<complex<double> > data(64, 1.);

fftw_plan plan = fftw_plan_dft_1d(data.size(), (fftw_complex*)&data[0], (fftw_complex*)&data[0], FFTW_FORWARD, FFTW_ESTIMATE);

fftw_execute(plan);
fftw_destroy_plan(plan);

EXPECT_TRUE(true);
}


int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();
}

链接器错误:
1>------ Build started: Project: fftw3, Configuration: Debug x64 ------
2>------ Build started: Project: FFT_test (tests\FFT_test\FFT_test), Configuration: Debug x64 ------
2>tests.cpp
2>C:\Work\...\Task-2.2\src\tests\tests.cpp(5): fatal error C1083: Cannot open include file: 'fftw3.h': No such file or directory
2>Done building project "FFT_test.vcxproj" -- FAILED.
1> Creating library C:/Work/.../Task-2.2/output_64/fftw3/Debug/fftw3.lib and object C:/Work/.../Task-2.2/output_64/fftw3/Debug/fftw3.exp
1>conf.c.obj : error LNK2001: unresolved external symbol fftw_solvtab_dft_standard
1>conf.c.obj : error LNK2001: unresolved external symbol fftw_solvtab_rdft_r2cf
1>conf.c.obj : error LNK2001: unresolved external symbol fftw_solvtab_rdft_r2cb
1>conf.c.obj : error LNK2001: unresolved external symbol fftw_solvtab_rdft_r2r
1>C:\Work\...\Task-2.2\output_64\bin\Debug\fftw3.dll : fatal error LNK1120: 4 unresolved externals
1>Done building project "fftw3.vcxproj" -- FAILED.
========== Build: 0 succeeded, 2 failed, 8 up-to-date, 0 skipped ==========

conf.c.obj 在 fftw3 库中。

最佳答案

在您的顶级 CMake 文件中,您可以深入了解 src首先是目录,然后添加 googletestfftw3子目录。因此,当您引用 gtest_mainfftw3 src\tests\CMakeLists.txt 中的目标文件,它们是未定义的:

target_link_libraries(FFT_test PUBLIC FFT_lib)
# The targets linked here are not yet defined.
target_link_libraries(FFT_test PUBLIC gtest_main)
target_link_libraries(FFT_test PUBLIC fftw3)

您应该重新排序您的顶级 CMake 文件,以便 CMake 可以首先找到依赖项,然后添加 src子目录:
add_subdirectory(googletest)
add_subdirectory(fftw3)
add_subdirectory(src)

您可能还需要为 fftw3 header 添加包含目录(包含 fftw3.h 的路径),如下所示:
add_executable(FFT_test ${SOURCES})
target_include_directories(FFT_test PRIVATE /path/to/fftw3-headers)

关于c++ - 通过 CMake 连接 fftw3 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61478589/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com