jlangr / c2 Goto Github PK
View Code? Open in Web Editor NEWChapter First Example
Chapter First Example
Hi!
The new repo of GoogleTest and GoogleMock is here:
https://github.com/google/googletest
Note that the structure is now
repo/googletest
repo/googlemock
In particular, the following does not exist anymore
repo/googletest/gtest
(So GoogleTest is no longer nested within GoogleMock).
But your book's CMakeLists.txt
depends on it:
In this link for example, we can see that your CMakeLists.txt
assumes the existence of $ENV{GMOCK_HOME}/gtest/include
and $ENV{GMOCK_HOME}/gtest/mybuild
Under Linux we can fix this with a symbolic link:
repo/googlemock/gtest
as a symbolic link to repo/googletest
I'd like to perhaps help in this respect.
Here follows a setup-script for GNU/Linux that
repo/googlemock/gtest
pointing correctlyload_testing.sh
, which when sourcing with . load_testing.sh
will automatically set GMOCK_HOME
correctly.#!/usr/bin/env bash
SHARED_LIB_ON_OFF=OFF # ON
git clone https://github.com/google/googletest.git
REPODIR=$(readlink -f googletest)
## compile googletest
mkdir $REPODIR/googletest/mybuild
cd $REPODIR/googletest/mybuild
cmake -DBUILD_SHARED_LIBS=$SHARED_LIB_ON_OFF ..
make
## compile googlemock
mkdir $REPODIR/googlemock/mybuild
cd $REPODIR/googlemock/mybuild
cmake -DBUILD_SHARED_LIBS=$SHARED_LIB_ON_OFF ..
make
## create link googlemock/gtest
ln -s $(readlink -f $REPODIR/googletest) $REPODIR/googlemock/gtest
## create a load-script testing.sh
SCRIPT_NAME=load_testing.sh
LOAD_SCRIPT=$REPODIR/../$SCRIPT_NAME
cat <<EOF > $LOAD_SCRIPT
# "Run" this script with: . $SCRIPT_NAME
# (this is called "sourcing" the script)
export GMOCK_HOME=$REPODIR/googlemock
EOF
chmod +x $LOAD_SCRIPT
echo
echo "FINISHED"
echo
echo "Also generated file $SCRIPT_NAME"
echo "Recommendation:"
echo "Copy the script $SCRIPT_NAME ... to ~/bin"
echo "Run the script with: . $SCRIPT_NAME"
echo "(this is called \"sourcing\" the script. thereafter the cmake scripts should work nicely)"
You can try this. It should make your own code-examples' cmake stuff work again.
Note also:
If, at the top of the above script... you set SHARED_LIB_ON_OFF=ON
, (and clear any previously generated googletest
directory that may have been cloned by a previous run of the script); and then run the script with SHARED_LIB_ON_OFF=ON
, you'll find that your CMakeLists.txt
(for example c2/2/CMakeLists.txt) will generate a binary, that when run, actually ends with Error in invalid pointer: ...
or Double free
.
The reason is that you link against both gmock
and gtest
(ref) ...
target_link_libraries(test gmock)
target_link_libraries(test gtest)
... which is double (!), since gmock library already includes the same stuff as gtest anyway... (ref gmock, ref gtest)... namely googletest/src/gtest-all.cc. I believe this should be linked only once (esp. during shared linking).
Thus I recommend changing c2/2/CMakeLists.txt (and others) to
project(chapterFirstExample)
cmake_minimum_required(VERSION 2.6)
include_directories($ENV{GMOCK_HOME}/include $ENV{GMOCK_HOME}/gtest/include)
link_directories($ENV{GMOCK_HOME}/mybuild $ENV{GMOCK_HOME}/gtest/mybuild)
add_definitions(-std=c++0x)
set(CMAKE_CXX_FLAGS "${CMAXE_CXX_FLAGS} -Wall")
set(sources
main.cpp
SoundexTest.cpp)
add_executable(tester ${sources})
target_link_libraries(tester pthread)
target_link_libraries(tester gmock)
## target_link_libraries(tester gtest) ## leave this one out!
No more error or double delete!
(Above test
was also renamed to tester
, to avoid warnings on new version of cmake (ref))
Anyway... just started your book. Looks interesting!
Hi,
this test was forgotten and fails:
TEST_F(SoundexEncoding, ForgetMeNot)
{
ASSERT_THAT(soundex.encode("bahb"), Eq("B100"));
}
https://www.wolframalpha.com/input/?i=Soundex+bahb
It's not enough to just check the last character (ref).
Then there are also special corner cases, which according to Wolfram Soundex are:
TEST_F(SoundexEncoding, Corner)
{
ASSERT_THAT(soundex.encode("Aaabwhwb"), Eq("A110"));
ASSERT_THAT(soundex.encode("Aaabhwhb"), Eq("A110"));
}
https://www.wolframalpha.com/input/?i=Soundex+Aaabwhwb
https://www.wolframalpha.com/input/?i=Soundex+Aaabhwhb
For anyone else who encounters this when compiling googletest from GitHub on Windows you need to create the project with the -Dgtest_force_shared_crt=ON
argument to cmake.
Without this you can get a lot of compilation errors such as:
gtest.lib(gtest-all.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug'
See the googletest readme for more information: https://github.com/google/googletest/blob/master/googletest/README.md
Additionally the googletest directory structure has changed, and the example c2 CMakeLists.txt file links to pthread (for googletest) but isn't required for this test, so it can be removed.
My CMakeLists.txt file which works is:
# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)
project ("Soundex1")
include_directories($ENV{GMOCK_HOME}/googletest/include $ENV{GMOCK_HOME}/googlemock/include)
link_directories($ENV{GMOCK_HOME}/mybuild/lib/MinSizeRel)
add_definitions(-std=c++0x)
set(CMAKE_CXX_FLAGS "${CMAXE_CXX_FLAGS} -Wall")
set(sources
main.cpp
SoundexTest.cpp)
add_executable(test ${sources})
target_link_libraries(test gmock)
target_link_libraries(test gtest)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.