title: How to call Google Tests from another main function in C++
body: <p>I am trying to create a stack and test it using Google tests in an experiment with the C++ Google Test framework. I have set up the structure so I have a stack.h and stack.cpp for the implementation then I have a tests.cpp with the following code. I have several questions: first, is it possible to call my test functions from the main as I have done? Also, am I including everything correctly? Why are the build errors happening? Sorry, I am new to C++ and thanks for the help in advance. Here is the code and errors:</p>
  
  <pre><code>#include "stack.h"
  #include "gtest/gtest.h"
  
  TEST (StackTest, PushAndPeek) {
      Stack&lt;int&gt; intStack;
      int a = 12;
      int b = 15;
      EXPECT_EQ (12, intStack.push(a));
      EXPECT_EQ (15, intStack.push(b));
      EXPECT_EQ (15, intStack.peek()); //make sure adding in LIFO Order
      EXPECT_EQ (15, intStack.peek()); //Should still be there
  }
  
  TEST (StackTest, PushAndPop) {
      Stack&lt;int&gt; intStack;
      int a = 12;
      int b = 15;
      EXPECT_EQ (12, intStack.push(a));
      EXPECT_EQ (15, intStack.push(b));
      EXPECT_EQ (15, intStack.pop()); //make sure adding in LIFO Order
      EXPECT_EQ (12, intStack.pop()); //Should have removed 15, then removed 12
      EXPECT_EQ (-1, intStack.pop()); //Should return -1 because there is nothing on the stack
  }
  </code></pre>
  
  <p>Then in my main.cpp I have:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "gtest/gtest.h"
  #include "stack.h"
  using namespace std;
  
  int main(int argc, char * argv[])
  {
      string input;
      cout &lt;&lt; "Hey there! If you wanna run the tests, type in tests. \nOther wise just hit enter to continue...\n";
      getline (cin, input);
      if(input == "tests"){
          ::testing::InitGoogleTest(&amp;argc, argv);
          return RUN_ALL_TESTS();
      }
  
      return 0;
  }
  </code></pre>
  
  <p>However, I am compiling using XCode and it is failing to build with the following reasons:</p>
  
  <pre><code>Undefined symbols for architecture x86_64:
    "Stack&lt;int&gt;::pop()", referenced from:
        StackTest_PushAndPop_Test::TestBody() in tests.o
    "Stack&lt;int&gt;::peek()", referenced from:
        StackTest_PushAndPeek_Test::TestBody() in tests.o
    "Stack&lt;int&gt;::push(int&amp;)", referenced from:
        StackTest_PushAndPeek_Test::TestBody() in tests.o
        StackTest_PushAndPop_Test::TestBody() in tests.o
    "Stack&lt;int&gt;::Stack()", referenced from:
        StackTest_PushAndPeek_Test::TestBody() in tests.o
        StackTest_PushAndPop_Test::TestBody() in tests.o
    "Stack&lt;int&gt;::~Stack()", referenced from:
        StackTest_PushAndPeek_Test::TestBody() in tests.o
        StackTest_PushAndPop_Test::TestBody() in tests.o
    "testing::internal::EqFailure(char const*, char const*, std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, bool)", referenced from:
        testing::AssertionResult testing::internal::CmpHelperEQ&lt;int, int&gt;(char const*, char const*, int const&amp;, int const&amp;) in tests.o
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>EDIT:
  By including the stack.cpp I resolved the errors related to that, but I still have the following errors:</p>
  
  <pre><code>Undefined symbols for architecture x86_64:
    "testing::internal::EqFailure(char const*, char const*, std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, bool)", referenced from:
        testing::AssertionResult testing::internal::CmpHelperEQ&lt;int, int&gt;(char const*, char const*, int const&amp;, int const&amp;) in tests.o
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>