Skip to content

CMakeLists.txt incompatible with new repo of GoogleTest #3

@ajneu

Description

@ajneu

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

  • clones googletest (and googlemock)
  • compiles them
  • sets up a symbolic link of repo/googlemock/gtest pointing correctly
  • creates a load_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.

Shared Library Problems

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions