# 搭建开发环境

Linux development with C++ in Visual Studio 一文介绍了使用VS跨平台特新需要做的准备工作。你也可以参考下面的视频：

## 准备实验代码

sample.cpp

#include "sample.h"

// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}

return result;
}

// Returns true iff n is a prime number.
bool IsPrime(int n) {
// Trivial case 1: small numbers
if (n <= 1) return false;

// Trivial case 2: even numbers
if (n % 2 == 0) return n == 2;

// Now, we have that n is odd and n >= 3.

// Try to divide n by every odd number i, starting from 3
for (int i = 3; ; i += 2) {
// We only have to try i up to the square root of n
if (i > n / i) break;

// Now, we have i <= n/i < n.
// If n is divisible by i, n is not prime.
if (n % i == 0) return false;
}

// n has no integer factor in the range (1, n), and thus is prime.
return true;
}
View Code

sample.h

#ifndef _SAMPLE_H_
#define _SAMPLE_H_

// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n);

// Returns true iff n is a prime number.
bool IsPrime(int n);

#endif
View Code

sample_unittest.cpp

#include <limits.h>
#include "sample.h"
#include "gtest/gtest.h"
namespace {

TEST(FactorialTest, Negative) {
// This test is named "Negative", and belongs to the "FactorialTest"
// test case.
EXPECT_EQ(1, Factorial(-5));
EXPECT_EQ(1, Factorial(-1));
EXPECT_GT(Factorial(-10), 0);
}

TEST(FactorialTest, Zero) {
EXPECT_EQ(1, Factorial(0));
}

TEST(FactorialTest, Positive) {
EXPECT_EQ(1, Factorial(1));
EXPECT_EQ(2, Factorial(2));
EXPECT_EQ(6, Factorial(3));
EXPECT_EQ(40320, Factorial(8));
}

// Tests IsPrime()
TEST(IsPrimeTest, Negative) {
EXPECT_FALSE(IsPrime(-1));
EXPECT_FALSE(IsPrime(-2));
EXPECT_FALSE(IsPrime(INT_MIN));
}

TEST(IsPrimeTest, Trivial) {
EXPECT_FALSE(IsPrime(0));
EXPECT_FALSE(IsPrime(1));
EXPECT_TRUE(IsPrime(2));
EXPECT_TRUE(IsPrime(3));
}

TEST(IsPrimeTest, Positive) {
EXPECT_FALSE(IsPrime(4));
EXPECT_TRUE(IsPrime(5));
EXPECT_FALSE(IsPrime(6));
EXPECT_TRUE(IsPrime(23));
}
}  // namespace
View Code

1. \\<Board IP Address>\lib dir into Project->Configuration Properties->VC++ Directories->Library Directories

2. lib file into  Project->Configuration Properties->->Linker->Input

## 补充一点

main.cpp

#include <cstdio>
#include <gtest/gtest.h>
int main(int argc, char** argv) {
return RUN_ALL_TESTS();
}
View Code

Build->Build Solution，Debug->Start Debug输出结果与上面Linux Console Window输出一样，不在截图。

# 方式二：使用NuGet Packages

posted @ 2019-01-03 17:03  克拉默与矩阵  阅读(187)  评论(0编辑  收藏