我想用 CMake 编译并运行 GoogleTest。 我完全按照本教程的说明进行操作
这是我得到的错误输出(myproject 是教程中提到的 CMakeLists.txt 所在的目录)
imelf@DESKTOP-CFHKUQA MINGW32 /d/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject
$ mkdir buildmingw
imelf@DESKTOP-CFHKUQA MINGW32 /d/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject
$ cd buildmingw
imelf@DESKTOP-CFHKUQA MINGW32 /d/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw
$ cmake .. -G "MinGW Makefiles"
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: D:/Software/MSYS2/usr/bin/cc.exe
-- Check for working C compiler: D:/Software/MSYS2/usr/bin/cc.exe - works
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: D:/Software/MSYS2/usr/bin/c++.exe
-- Check for working CXX compiler: D:/Software/MSYS2/usr/bin/c++.exe - works
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) at D:/Software/MSYS2/mingw64/share/cmake/Modules/FetchContent.cmake:1279 (message):
The DOWNLOAD_EXTRACT_TIMESTAMP option was not given and policy CMP0135 is
not set. The policy's OLD behavior will be used. When using a URL
download, the timestamps of extracted files should preferably be that of
the time of extraction, otherwise code that depends on the extracted
contents might not be rebuilt if the URL changes. The OLD behavior
preserves the timestamps from the archive instead, but this is usually not
what you want. Update your project to the NEW behavior or specify the
DOWNLOAD_EXTRACT_TIMESTAMP option with a value of true to avoid this
robustness issue.
Call Stack (most recent call first):
CMakeLists.txt:9 (FetchContent_Declare)
This warning is for project developers. Use -Wno-dev to suppress it.
CMake Warning (dev) at D:/Software/MSYS2/mingw64/share/cmake/Modules/GNUInstallDirs.cmake:243 (message):
Unable to determine default CMAKE_INSTALL_LIBDIR directory because no
target architecture is known. Please enable at least one language before
including GNUInstallDirs.
Call Stack (most recent call first):
buildmingw/_deps/googletest-src/CMakeLists.txt:28 (include)
This warning is for project developers. Use -Wno-dev to suppress it.
-- Found Python: D:/Software/Python/python.exe (found version "3.11.1") found components: Interpreter
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw
imelf@DESKTOP-CFHKUQA MINGW32 /d/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw
$ mingw32-make
[ 10%] Building CXX object _deps/googletest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj
In file included from D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw/_deps/googletest-src/googletest/include/gtest/gtest-message.h:55,
from D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw/_deps/googletest-src/googletest/include/gtest/gtest-assertion-result.h:46,
from D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw/_deps/googletest-src/googletest/include/gtest/gtest.h:59,
from D:\Informatik\NachhilfeInfoUni\KadalaSchmittC++\poose24test\poose24test\myproject\buildmingw\_deps\googletest-src\googletest\src\gtest-all.cc:38:
D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function ‘int testing::internal::posix::FileNo(FILE*)’:
D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2018:40: error: ‘fileno’ was not declared in this scope; did you mean ‘FileNo’?
2018 | inline int FileNo(FILE* file) { return fileno(file); }
| ^~~~~~
| FileNo
D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function ‘char* testing::internal::posix::StrDup(const char*)’:
D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2024:47: error: ‘strdup’ was not declared in this scope; did you mean ‘StrDup’?
2024 | inline char* StrDup(const char* src) { return strdup(src); }
| ^~~~~~
| StrDup
D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function ‘FILE* testing::internal::posix::FDOpen(int, const char*)’:
D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2074:56: error: ‘fdopen’ was not declared in this scope; did you mean ‘fopen’?
2074 | inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
| ^~~~~~
| fopen
In file included from D:\Informatik\NachhilfeInfoUni\KadalaSchmittC++\poose24test\poose24test\myproject\buildmingw\_deps\googletest-src\googletest\src\gtest-all.cc:45:
D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw/_deps/googletest-src/googletest/src/gtest-port.cc: In constructor ‘testing::internal::CapturedStream::CapturedStream(int)’:
D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/poose24test/poose24test/myproject/buildmingw/_deps/googletest-src/googletest/src/gtest-port.cc:1102:31: error: ‘::mkstemp’ has not been declared; did you mean ‘mkdtemp’?
1102 | const int captured_fd = ::mkstemp(const_cast<char*>(name_template.data()));
| ^~~~~~~
| mkdtemp
mingw32-make[2]: *** [_deps\googletest-build\googletest\CMakeFiles\gtest.dir\build.make:76: _deps/googletest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:215: _deps/googletest-build/googletest/CMakeFiles/gtest.dir/all] Error 2
mingw32-make: *** [Makefile:145: all] Error 2
我做错了什么?
我还尝试用Find_Library替换CMakeLists.txt中的“FetchContent Stuff”,之前已经安装了GoogleTest
pacman -S mingw-w64-x86_64-gtest
cmake_minimum_required(VERSION 3.14)
project(my_project)
# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_library(GTest gtest)
enable_testing()
add_executable(
hello_test
hello_test.cc
)
target_link_libraries(
hello_test
gtest gtest_main
)
include(GoogleTest)
gtest_discover_tests(hello_test)
然后我收到以下错误:
D:\Informatik\NachhilfeInfoUni\KadalaSchmittC++\poose24test\poose24test\myproject\hello_test.cc:1:10: fatal error: gtest/gtest.h: No such file or directory
1 | #include <gtest/gtest.h>
| ^~~~~~~~~~~~~~~
compilation terminated.
mingw32-make[2]: *** [CMakeFiles\hello_test.dir\build.make:75: CMakeFiles/hello_test.dir/hello_test.cc.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/hello_test.dir/all] Error 2
mingw32-make: *** [Makefile:100: all] Error 2
我设法通过放线来解决问题
set(CMAKE_CXX_STANDARD 17)
包含 GoogleTest 后