构建Google测试项目时对'getcwd'和'mkdir'的未定义引用

问题描述 投票:0回答:1

即使我一直严格遵守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] = { '\0' };
  return FilePath(_getcwd(cwd, sizeof(cwd)) == nullptr ? "" : cwd);
#else
  char cwd[GTEST_PATH_MAX_ + 1] = { '\0' };
  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。

c++ eclipse googletest
1个回答
0
投票

AFAIK,getcwd()和mkdir()与平台有关。似乎其他库也存在类似问题:https://github.com/purduesigbots/pros/issues/176

如上所述,您可以尝试为缺少的符号定义存根。

在我的工作平台上,getcwd()和mkdir()甚至从标题中删除。在这种情况下,您可以直接编辑gtest,例如:

FilePath FilePath::GetCurrentDir() {
#if GTEST_OS_CUSTOM_PLATFORM
  return kCurrentDirectoryString;
...
© www.soinside.com 2019 - 2024. All rights reserved.