Gtest (Google test) compilation (static library *. A and dynamic library *. So) and use

Source: Internet
Author: User

I haven't written a blog for a long time. I didn't write much before graduation. After graduation, I just came to my company and became familiar with the environment. Now I want to resume my blog writing habits. I will announce my strong return. 2013-07-19

Gtest is a unit test tool. The gtest framework is deployed on different platforms (Linux, Mac OS X, windows, cygwin, and Windows ).
Ce and Symbian. It is a test framework based on the xunit architecture and supports automatic discovery and testing, rich assertion sets, user-defined assertion, death testing, fatal and non-fatal failures, type parameterized testing, various test options and XML test reports ..

After decompression, In the make folder, there is a MAKEFILE file, directly make, will generate *. o file and a static library file gtest-main.a. This is not what we want. We need to modify the MAKEFILE file:

# A sample Makefile for building Google Test and using it in user# tests.  Please tweak it to suit your environment and project.  You# may want to move it to your project's root directory.## SYNOPSIS:##   make [all]  - makes everything.#   make TARGET - makes the given target.#   make clean  - removes all files generated by make.# Please tweak the following variable definitions as needed by your# project, except GTEST_HEADERS, which you can use in your own targets# but shouldn't modify.# Points to the root of Google Test, relative to where this file is.# Remember to tweak this if you move this file.GTEST_DIR = ..# Where to find user code.USER_DIR = ../samples# Flags passed to the preprocessor.CPPFLAGS += -I$(GTEST_DIR)/include# Flags passed to the C++ compiler.CXXFLAGS += -g -Wall -Wextra -fPIC# All tests produced by this Makefile.  Remember to add new tests you# created to the list.TESTS = sample1_unittest# All Google Test headers.  Usually you shouldn't change this# definition.GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \                $(GTEST_DIR)/include/gtest/internal/*.hSTATICLIB_GTEST = libgtest.aDYLIB_GTEST = libgtest.so.6.0# House-keeping build targets.all : $(TESTS) $(STATICLIB_GTEST) $(DYLIB_GTEST)clean :rm -f $(TESTS) $(STATICLIB_GTEST) $(DYLIB_GTEST) gtest_main.a *.o# Builds gtest.a and gtest_main.a.# Usually you shouldn't tweak such internal variables, indicated by a# trailing _.GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)# For simplicity and to avoid depending on Google Test's# implementation details, the dependencies specified below are# conservative and not optimized.  This is fine as Google Test# compiles fast and for ordinary users its source rarely changes.gtest-all.o : $(GTEST_SRCS_)$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \            $(GTEST_DIR)/src/gtest-all.ccgtest_main.o : $(GTEST_SRCS_) $(STATICLIB_GTEST)$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \            $(GTEST_DIR)/src/gtest_main.cc$(STATICLIB_GTEST) : gtest-all.o$(AR) $(ARFLAGS) $@ $^gtest_main.a : gtest-all.o gtest_main.o$(AR) $(ARFLAGS) $@ $^$(DYLIB_GTEST) : gtest-all.o$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) -shared -o $@ $^# Builds a sample test.  A test should link with either gtest.a or# gtest_main.a, depending on whether it defines its own main()# function.sample1.o : $(USER_DIR)/sample1.cc $(USER_DIR)/sample1.h $(GTEST_HEADERS)$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1.ccsample1_unittest.o : $(USER_DIR)/sample1_unittest.cc \                     $(USER_DIR)/sample1.h $(GTEST_HEADERS)$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1_unittest.ccsample1_unittest : sample1.o sample1_unittest.o gtest_main.a$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

Running the makefile above will directly generate the dynamic library libgtest. so.6.0 and the static library libgtest.. Note: The red color must be modified or added. Added-FPIC when compiling the dynamic library. For more information, see http://blog.csdn.net/laoyi19861011/article/details/9404259.

Before talking about how to use gtest, let's talk about the connection problem of the Dynamic Link Library:

The default search paths for Linux dynamic libraries are/lib and/usr/lib. After a dynamic library is created, it is generally copied to these two directories. When a dynamic library is required for program execution and the dynamic library is not loaded into the memory, the system will automatically go to the two default search paths to find the corresponding dynamic library file, then load the file to the memory, so that the program can use the function numbers in the dynamic library and other resources of the dynamic library. In Linux, in addition to the default search path, the search path of a dynamic library can be specified in the following three methods:
Method 1: Specify the dynamic library search path in the configuration file/etc/lD. So. conf. After editing the file, you must run the ldconfig command to make the modified configuration take effect. We use Example 1 to describe this method.
Example 1:
Run the following command to use the source program pos_conf.c (see program 1) to create the dynamic library libpos. So,
# Gcc-FPIC-shared-O libpos. So pos_conf.c
Run the following command to compile main. C (see Program 2) to generate the POS of the target program.
# Gcc-o pos main. C-L.-LPOS
Method 2: Use the environment variable LD_LIBRARY_PATH to specify the dynamic library search path.
Method 3: Specify the dynamic library search path of the program when compiling the target code.
You can also specify the dynamic library search path of the program when compiling the target code. -Wl, indicating that the following parameters will be passed to the link program LD (because GCC may automatically call LD ). Here, the GCC parameter "-wl,-rpath," is specified (as shown in Example 3 ). When multiple dynamic library search paths are specified, the paths are separated by colons.

Example 3

Run the following command to create the dynamic library libpos. So using the source pos. C (see program 4.

# Gcc-C pos. c
# Gcc-shared-fic-O libpos. So pos. o
#
Because we need to specify the dynamic library search path of the executable file when compiling the target code, we need to use the GCC command to re-compile the source program main. C (see Program 2) to generate the executable file POS.
# Gcc-o pos main. C-L.-LPOS-wl,-rpath =...:.: Lib

Search for the sequence of dynamic Databases

Dynamic library search path specified when compiling the target code

LD_LIBRARY_PATH

/Etc/lD. So. Cache

Default path/lib, And then/usr/lib.

Note the following points when using gtest:

CFLAGS= -g -I"."CXXFLAGS = -g -I"." -I"./gtest/include" -L"./gtest/lib" -Wl,-rpath="./gtest/lib/"CC= gccCXX= g++LIBS= -lgtest -pthreadTARGETS= gentestsgentests: $(COBJS) $(GTOBJS)$(CXX) $(CXXFLAGS) -Wall -Wshadow -o $@ $^ $(LIBS)

Therefore, during compilation, I use the third method. during compilation, pay attention to the red annotation and add-ptread to avoid errors. -L ". /gtest/lib "is the path for compiling and searching for libraries, while-wl,-rpath = ". /gtest/lib/"is the path to find the dynamic library when the program is running. There are also-WL and-rpath. There is no space between them and there is only a comma. W must be in upper case, and l must be in lower case. Pay attention to this. The generated dynamic library is libgtest. so.6.0. You also need to perform one step: ln-s libgtest. So libgtest. so.6.0 to connect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.