使用PlatformIO+GoogleTest运行测试时.cpp文件链接失败问题
问题解决方法
通过在platformio.ini中添加以下配置项,问题已解决:
build_src_filter = +<**/*.cpp> -<main.cpp> test_build_src = yes
原始问题
这是一个新手问题。我有一个简单的PlatformIO Arduino项目,目录结构如下:
project_root/ ├── platformio.ini ├── src/ │ ├── Foo.cpp │ ├── Foo.h │ └── main.cpp ├── test/ │ └── test_desktop/ │ ├── bootstrap.cpp │ └── foo_test.cpp
项目配置文件(platformio.ini)
[env:nanoatmega328new] platform = atmelavr board = nanoatmega328new framework = arduino test_ignore = test_desktop [env:native] platform = native test_ignore = test_embedded lib_compat_mode = off lib_deps = google/googletest@^1.15.2 build_flags = -std=c++14
源文件代码
src/main.cpp
#include <Arduino.h> #include "Foo.h" Foo foo; void setup() { Serial.begin(9600); } void loop() { Serial.println(foo.bar()); }
src/Foo.h
#ifndef FOO_H #define FOO_H class Foo { public: int bar(); }; #endif //FOO_H
src/Foo.cpp
#include "Foo.h" int Foo::bar() { return 0; }
测试代码
test/test_desktop/bootstrap.cpp
#include <gtest/gtest.h> int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
test/test_desktop/foo_test.cpp
#include <gtest/gtest.h> #include "Foo.h" TEST(Foo, test_bar) { Foo foo; ASSERT_EQ(foo.bar(), 0); }
运行测试时的错误
执行pio test命令后出现以下错误:
Processing test_desktop in native environment -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Building... Undefined symbols for architecture arm64: "Foo::bar()", referenced from: Foo_test_bar_Test::TestBody() in foo_test.o ld: symbol(s) not found for architecture arm64 clang++: error: linker command failed with exit code 1 (use -v to see invocation) *** [.pio/build/native/program] Error 1 Building stage has failed, see errors above. Use `pio test -vvv` option to enable verbose output.
问题表现为链接器无法找到Foo::bar()的实现,无法完成链接。
内容的提问来源于stack exchange,提问作者volodymyrkoval
相关产品推荐
相关产品推荐

