C++ and gtest setup
The neat thing about C and C++ on Linux, is that it's very much a native language. It means that compilers, tools and libraries are part of the core repositories, and easily installed. The following includes the basic setup to get started with C++ development.
apt-get install gcc g++ gdb cmake make libgtest-dev
Unit tests are great, and the Google Test (gtest) framework makes it straight forward to write and run. The gtest Debian / Ubuntu package requires a bit of manual installation as well, as described here. The crux of it:
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
sudo cp *.a /usr/lib
The following is a minimal example, which includes the test code and method under test in the same file for the sake of simplicity. Normally, the tests and code would of course be split.
#include "gtest/gtest.h"
int multiply(int a, int b) {
return a * b;
}
TEST(HelloTest, Multiply) {
EXPECT_EQ(2, multiply(1, 2));
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
To compile the code above, take care to include all required arguments. The default executable binary output is "a.out" (or use the -o option to set a custom name), which can be executed, and the test result output should show.
g++ -pthread hello_test.c -lgtest_main -lgtest
./a.out