构建 Google 测试项目时未定义对 'getcwd' 和 'mkdir' 的引用
Undefined reference to 'getcwd' and 'mkdir' when building Google test project
即使我严格按照 this post 中的教程进行操作,我也无法编译我的 google 测试演示程序。
我在 Windows 10 x64 上使用 Eclipse 和 ARM GCC 嵌入式工具链来编译我的代码,因为我最终需要在嵌入式设备上进行 运行 单元测试。
我的问题是,当我尝试构建项目时,出现了这些错误:
c:/program files (x86)/gnu tools arm embedded/9 2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: ./contrib/gtest/gtest-all.o: in function `testing::internal::FilePath::GetCurrentDir()':
C:\Users\Hugo\eclipse\eclipse-workspace\test_gtest\Debug/../contrib/gtest/gtest-all.cc:9598: undefined reference to `getcwd'
c:/program files (x86)/gnu tools arm embedded/9 2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: ./contrib/gtest/gtest-all.o: in function `testing::internal::FilePath::CreateFolder() const':
C:\Users\Hugo\eclipse\eclipse-workspace\test_gtest\Debug/../contrib/gtest/gtest-all.cc:9823: undefined reference to `mkdir'
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:61: test_gtest.elf] Error 1
它更准确地来自 gtest_all.cc 文件中的那些代码行:
对于 'getcwd' 的未定义引用
FilePath FilePath::GetCurrentDir() {
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \
GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_ESP32
// These platforms do not have a current directory, so we just return
// something reasonable.
return FilePath(kCurrentDirectoryString);
#elif GTEST_OS_WINDOWS
char cwd[GTEST_PATH_MAX_ + 1] = { '[=14=]' };
return FilePath(_getcwd(cwd, sizeof(cwd)) == nullptr ? "" : cwd);
#else
char cwd[GTEST_PATH_MAX_ + 1] = { '[=14=]' };
char* result = getcwd(cwd, sizeof(cwd));
# if GTEST_OS_NACL
// getcwd will likely fail in NaCl due to the sandbox, so return something
// reasonable. The user may have provided a shim implementation for getcwd,
// however, so fallback only when failure is detected.
return FilePath(result == nullptr ? kCurrentDirectoryString : cwd);
# endif // GTEST_OS_NACL
return FilePath(result == nullptr ? "" : cwd);
#endif // GTEST_OS_WINDOWS_MOBILE
}
对于 'mkdir' 的未定义引用:
bool FilePath::CreateFolder() const {
#if GTEST_OS_WINDOWS_MOBILE
FilePath removed_sep(this->RemoveTrailingPathSeparator());
LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());
int result = CreateDirectory(unicode, nullptr) ? 0 : -1;
delete [] unicode;
#elif GTEST_OS_WINDOWS
int result = _mkdir(pathname_.c_str());
#elif GTEST_OS_ESP8266
// do nothing
int result = 0;
#else
int result = mkdir(pathname_.c_str(), 0777);
#endif // GTEST_OS_WINDOWS_MOBILE
if (result == -1) {
return this->DirectoryExists(); // An error is OK if the directory exists.
}
return true; // No error.
}
我检查过 unistd.h 已包含在内。我一直在搜索,但似乎找不到与我的类似的错误。我能找到的最接近的已经被使用 CMake 编译它的人解决了,但我在这里根本没有使用 CMake。
据我所知,getcwd() 和 mkdir() 依赖于平台。
其他图书馆似乎也有类似的问题:
https://github.com/purduesigbots/pros/issues/176
如上link,你可以尝试为缺失的符号定义存根
在我的工作平台中,getcwd() 和 mkdir() 甚至从 header 中删除。
在这种情况下,您可以直接编辑 gtest,例如:
FilePath FilePath::GetCurrentDir() {
#if GTEST_OS_CUSTOM_PLATFORM
return kCurrentDirectoryString;
...
即使我严格按照 this post 中的教程进行操作,我也无法编译我的 google 测试演示程序。 我在 Windows 10 x64 上使用 Eclipse 和 ARM GCC 嵌入式工具链来编译我的代码,因为我最终需要在嵌入式设备上进行 运行 单元测试。 我的问题是,当我尝试构建项目时,出现了这些错误:
c:/program files (x86)/gnu tools arm embedded/9 2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: ./contrib/gtest/gtest-all.o: in function `testing::internal::FilePath::GetCurrentDir()':
C:\Users\Hugo\eclipse\eclipse-workspace\test_gtest\Debug/../contrib/gtest/gtest-all.cc:9598: undefined reference to `getcwd'
c:/program files (x86)/gnu tools arm embedded/9 2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: ./contrib/gtest/gtest-all.o: in function `testing::internal::FilePath::CreateFolder() const':
C:\Users\Hugo\eclipse\eclipse-workspace\test_gtest\Debug/../contrib/gtest/gtest-all.cc:9823: undefined reference to `mkdir'
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:61: test_gtest.elf] Error 1
它更准确地来自 gtest_all.cc 文件中的那些代码行:
对于 'getcwd' 的未定义引用
FilePath FilePath::GetCurrentDir() {
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \
GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_ESP32
// These platforms do not have a current directory, so we just return
// something reasonable.
return FilePath(kCurrentDirectoryString);
#elif GTEST_OS_WINDOWS
char cwd[GTEST_PATH_MAX_ + 1] = { '[=14=]' };
return FilePath(_getcwd(cwd, sizeof(cwd)) == nullptr ? "" : cwd);
#else
char cwd[GTEST_PATH_MAX_ + 1] = { '[=14=]' };
char* result = getcwd(cwd, sizeof(cwd));
# if GTEST_OS_NACL
// getcwd will likely fail in NaCl due to the sandbox, so return something
// reasonable. The user may have provided a shim implementation for getcwd,
// however, so fallback only when failure is detected.
return FilePath(result == nullptr ? kCurrentDirectoryString : cwd);
# endif // GTEST_OS_NACL
return FilePath(result == nullptr ? "" : cwd);
#endif // GTEST_OS_WINDOWS_MOBILE
}
对于 'mkdir' 的未定义引用:
bool FilePath::CreateFolder() const {
#if GTEST_OS_WINDOWS_MOBILE
FilePath removed_sep(this->RemoveTrailingPathSeparator());
LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());
int result = CreateDirectory(unicode, nullptr) ? 0 : -1;
delete [] unicode;
#elif GTEST_OS_WINDOWS
int result = _mkdir(pathname_.c_str());
#elif GTEST_OS_ESP8266
// do nothing
int result = 0;
#else
int result = mkdir(pathname_.c_str(), 0777);
#endif // GTEST_OS_WINDOWS_MOBILE
if (result == -1) {
return this->DirectoryExists(); // An error is OK if the directory exists.
}
return true; // No error.
}
我检查过 unistd.h 已包含在内。我一直在搜索,但似乎找不到与我的类似的错误。我能找到的最接近的已经被使用 CMake 编译它的人解决了,但我在这里根本没有使用 CMake。
据我所知,getcwd() 和 mkdir() 依赖于平台。 其他图书馆似乎也有类似的问题: https://github.com/purduesigbots/pros/issues/176
如上link,你可以尝试为缺失的符号定义存根
在我的工作平台中,getcwd() 和 mkdir() 甚至从 header 中删除。 在这种情况下,您可以直接编辑 gtest,例如:
FilePath FilePath::GetCurrentDir() {
#if GTEST_OS_CUSTOM_PLATFORM
return kCurrentDirectoryString;
...