该项目提供了一个带有测试的静态库。我将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
目录,然后再添加googletest
和fftw3
子目录。因此,当您在gtest_main
文件中引用fftw3
和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
的路径)添加include目录,如下所示: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/