Код: Выделить всё
project_root/
├── platformio.ini
├── src/
│ ├── Foo.cpp
│ └── Foo.h
│ └── main.cpp
├── test/
│ └── test_desktop/
│ └── bootstrap.cpp
│ └── foo_test.cpp
< /code>
// 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
< /code>
// src/main.cpp
#include
#include "Foo.h"
Foo foo;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(foo.bar());
}
< /code>
// src/Foo.h
#ifndef FOO_H
#define FOO_H
class Foo {
public:
int bar();
};
#endif //FOO_H
< /code>
// src/Foo.cpp
#include "Foo.h"
int Foo::bar() {
return 0;
}
< /code>
My goal is to be able to write unit tests using googletest and googlmock. My test setup is:
// test/test_desktop/bootstrap.cpp
#include
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
< /code>
and the first test:
// test/test_desktop/foo_test.cpp
#include
#include "Foo.h"
TEST(Foo, test_bar) {
Foo foo;
ASSERT_EQ(foo.bar(), 0);
}
< /code>
Then, when I run it with 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.
< /code>
It seem like linker can't link ".h" and ".cpp" file.
Appreciate any help with that.
Подробнее здесь: https://stackoverflow.com/questions/793 ... googletest