title: How to run gtest using scons
body: <p>I have a precompiled version of gtest (I know Google advises against it, but that's how our project will use it), and I want to write a very simple test and build it using scons. Assume I have super simple test called test.cpp, and gtest is installed in /opt/gtest . I am not that great using Scons, and would like to know how my SConstruct should look like.</p>
  
  <pre><code>//test.cpp
  #include "gtest/gtest.h"
  
  TEST(MyTest, Test) {
     ASSERT_TRUE(true);
  }
  
  int main(int argc, char** argv) {
   ::testing::InitGoogleTest(&amp;argc, argv);
    return RUN_ALL_TESTS();
  }
  </code></pre>
  
  <p>My current SConstruct looks like this (but does not work)</p>
  
  <pre><code>env = Environment()
  
  LIBS =''
  
  common_libs = ['pthread', 'gtest']
  env.Append( LIBS = common_libs )
  
  Program('test.cpp', LIBS, LIBPATH='/opt/gtest/lib')
  </code></pre>
  
  <p>I get the following message when I run scons</p>
  
  <pre><code>scons: Reading SConscript files ...
  IndexError: list index out of range:
    File "/home/user/testing/SConstruct", line 8:
      Program('test.cpp', LIBS, LIBPATH='/opt/gtest/lib')
    File "/usr/lib/scons/SCons/Script/SConscript.py", line 614:
      return method(*args, **kw)
    File "/usr/lib/scons/SCons/Environment.py", line 258:
      return MethodWrapper.__call__(self, target, source, *args, **kw)
    File "/usr/lib/scons/SCons/Environment.py", line 222:
      return self.method(*nargs, **kwargs)
    File "/usr/lib/scons/SCons/Builder.py", line 632:
      return self._execute(env, target, source, OverrideWarner(kw), ekw)
    File "/usr/lib/scons/SCons/Builder.py", line 540:
      source = self.src_builder_sources(env, source, overwarn)
    File "/usr/lib/scons/SCons/Builder.py", line 736:
      s = self._adjustixes(s, None, src_suf)[0]
  </code></pre>
  
  <p>Thank you!</p>
  
  <p><em><strong>EDIT:</em></strong></p>
  
  <p>After changing </p>
  
  <pre><code>Program('test.cpp', LIBS, LIBPATH='/opt/gtest/lib')
  </code></pre>
  
  <p>to</p>
  
  <pre><code>Program('test', 'test.cpp', LIBS, LIBPATH='/opt/gtest/lib')
  </code></pre>
  
  <p>I get the following errors</p>
  
  <pre><code>scons: Reading SConscript files ...
  scons: done reading SConscript files.
  scons: Building targets ...
  g++ -o test test.o -L/opt/gtest/lib
  test.o: In function `MyTest_Test_Test::TestBody()':
  test.cpp:(.text+0x5f): undefined reference to `testing::internal::GetBoolAssertionFailureMessage(testing::AssertionResult const&amp;, char const*, char const*, char const*)'
  test.cpp:(.text+0x8c): undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
  test.cpp:(.text+0x9f): undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&amp;) const'
  test.cpp:(.text+0xb2): undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
  test.cpp:(.text+0xc6): undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
  test.o: In function `main':
  test.cpp:(.text+0x16a): undefined reference to `testing::InitGoogleTest(int*, char**)'
  test.cpp:(.text+0x16f): undefined reference to `testing::UnitTest::GetInstance()'
  test.cpp:(.text+0x177): undefined reference to `testing::UnitTest::Run()'
  test.o: In function `__static_initialization_and_destruction_0(int, int)':
  test.cpp:(.text+0x1df): undefined reference to `testing::internal::GetTestTypeId()'
  test.cpp:(.text+0x20e): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
  test.o: In function `MyTest_Test_Test::MyTest_Test_Test()':
  test.cpp:(.text._ZN16MyTest_Test_TestC2Ev[_ZN16MyTest_Test_TestC5Ev]+0x14): undefined reference to `testing::Test::Test()'
  test.o: In function `testing::internal::scoped_ptr&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::reset(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;*)':
  test.cpp:(.text._ZN7testing8internal10scoped_ptrISsE5resetEPSs[testing::internal::scoped_ptr&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::reset(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;*)]+0x24): undefined reference to `testing::internal::IsTrue(bool)'
  test.o: In function `testing::internal::scoped_ptr&lt;std::basic_stringstream&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::reset(std::basic_stringstream&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;*)':
  test.cpp:(.text._ZN7testing8internal10scoped_ptrISt18basic_stringstreamIcSt11char_traitsIcESaIcEEE5resetEPS6_[testing::internal::scoped_ptr&lt;std::basic_stringstream&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::reset(std::basic_stringstream&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;*)]+0x23): undefined reference to `testing::internal::IsTrue(bool)'
  test.o:(.rodata._ZTV16MyTest_Test_Test[vtable for MyTest_Test_Test]+0x20): undefined reference to `testing::Test::SetUp()'
  test.o:(.rodata._ZTV16MyTest_Test_Test[vtable for MyTest_Test_Test]+0x28): undefined reference to `testing::Test::TearDown()'
  test.o:(.rodata._ZTI16MyTest_Test_Test[typeinfo for MyTest_Test_Test]+0x10): undefined reference to `typeinfo for testing::Test'
  test.o: In function `MyTest_Test_Test::~MyTest_Test_Test()':
  test.cpp:(.text._ZN16MyTest_Test_TestD2Ev[_ZN16MyTest_Test_TestD5Ev]+0x1f): undefined reference to `testing::Test::~Test()'
  collect2: ld returned 1 exit status
  scons: *** [test] Error 1
  scons: building terminated because of errors.
  </code></pre>