C++ 未定义对'的引用;getcwd';和';mkdir';在构建Google测试项目时
即使我一直严格遵循的教程,我不能让我的谷歌测试演示程序编译。 我在Windows10x64上使用Eclipse和ARMGCC嵌入式工具链来编译我的代码,因为我最终需要在嵌入式设备上运行单元测试。 我的问题是,当我尝试构建项目时,会出现以下错误:C++ 未定义对'的引用;getcwd';和';mkdir';在构建Google测试项目时,c++,eclipse,googletest,C++,Eclipse,Googletest,即使我一直严格遵循的教程,我不能让我的谷歌测试演示程序编译。 我在Windows10x64上使用Eclipse和ARMGCC嵌入式工具链来编译我的代码,因为我最终需要在嵌入式设备上运行单元测试。 我的问题是,当我尝试构建项目时,会出现以下错误: 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/
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。AFAIK、getcwd()和mkdir()依赖于平台。
其他图书馆似乎也存在类似问题:
正如上面的链接,您可以尝试为缺少的符号定义存根
在我的工作平台中,getcwd()和mkdir()甚至可以从标题中删除。
在这种情况下,您可以直接编辑gtest,例如:
FilePath FilePath::GetCurrentDir() {
#if GTEST_OS_CUSTOM_PLATFORM
return kCurrentDirectoryString;
...