Showing posts with label MacOS. Show all posts
Showing posts with label MacOS. Show all posts

Sep 21, 2011

How to compile googletest with gcc and CMake on Mac OS X SnowLeopard

I often found the way with Visual Studio C++ or XCode on web but I don't use them on my laptop. So I researched the way without IDE.

My settings
  • Mac OS X 10.6.8(Snow Leopard)
  •  gcc 4.5.4
  • cmake 2.8.5
According to googletest's README, you need more than 2.6.4 of cmake but the version of gcc is not clear.

Process
  1. Get source code of google test from svn repository
  2. Build googletest with cmake
  3. Create source file that has main function
  4. Create test code
  5. Build test code with g++
  6. Run the test
(1) Get source code of google test from svn repository

Place the source where you want.
svn checkout http://googletest.googlecode.com/svn/trunk/ gtest-svn

(2) Build googletest with cmake
Make a directory that cmake runs, which is good for anywhere.  {GTEST_DIR} is where theres is googletest. It is success when you get libgtest.a and libgtest_main.a after build.

mkdir mybuild
cd mybuild
cmake ${GTEST_DIR}
make

(3) Create source file that has main function

Create source file that has main function. You can find sample as below link.

You have to call below functions in main function.
  • testing::InitGoogleTest(&argc, argv); 
  • RUN_ALL_TESTS();

#include <iostream>
#include "gtest/gtest.h"

GTEST_API_ int main(int argc, char **argv) {
  std::cout << "Running main() from testmain.cc\n";

  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
(4) Craeate test code
#include "gtest/gtest.h"

TEST(firstTest, abs)
{
  EXPECT_EQ(1, abs( -1 ));
  EXPECT_EQ(1, abs( 1 ));
}

(5) Build test code with g++
You have to add the directory where there is googletest's header file into include path. You also have to build test code with static library of google test that you made at (2).

As below, testmain.cc is a file that has main function and mytest.cc is a file that has test code.


g++ -I{GTEST_DIR}/include testmain.cc mytest.cc libgtest.a libgtest_main.a -o mytes

(6) Run the test
Run the test. It is success if you see the test result as below.




Jul 4, 2011

repo init throws import readline error

While I was attempting to use the repo init command for downloading Android source code, it threw an error about importing the readline module like this:
Traceback (most recent call last):
File "/Users/xxx/bin/repo", line 91, in <module> import readline
ImportError: No module named readline
I found the same problem on Issue 1032 of Android open source project.

This happend because the python readline module was not included as part of OSX. To fix this problem, you have to install the python readline module.
sudo port install py25-readline
If you are using Python that is contained in OSX, you have to change it to MacPort's Python.
sudo port select python python25
I am using Mac OS X as a UNIX with better UI. I like Mac OS BUT Mac OS X has a lot of problem for programming environment as I mentioned before ><

Dec 28, 2010

How to install gcc on MaxOS X Snow Leopard with macport

You can find available version of gcc with macport like this:
sudo port search gcc
In my case gcc 4.5.1 was latest. So, I install it with this:
sudo port install gcc45
Now you can use gcc.

Dec 12, 2010

How to install Git on Mac OS X Snow Leopard

You just input on terminal like this:
$ sudo port install git-core
Please be careful. That is "git-core" not "git"