I want to include gtest to my C++ project. I am using Clion as IDE, which should work. Some tests are already working, but I cannot use any functions from B_RocChoice.h. It says that the function is not declared in this scope.
Can somebody tell me what I am doing wrong? How I must change my CMakeLists.txt files that it recogizes my methods?
This is my basic_tests.cpp, where my testcases will be written.
This is my Directory.
Here, the most outer CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(cli)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
set(SOURCE_FILES
include/A_WowbaggerChoice.h
include/AbstractChoice.h
include/B_RocChoice.h
include/C_CnnChoice.h
include/D_DetectorChoice.h
include/E_LearningChoice.h
include/Help.h
include/MyException.h
include/StartScreen.h
include/tinyxml.h
include/types.h
src/A_WowbaggerChoice.cpp
src/AbstractChoice.cpp
src/B_RocChoice.cpp
src/C_CnnChoice.cpp
src/D_DetectorChoice.cpp
src/E_LearningChoice.cpp
src/Help.cpp
src/main.cpp
src/MyException.cpp
src/StartScreen.cpp
tinyxml/tinystr.cpp
tinyxml/tinystr.h
tinyxml/tinyxml.cpp
tinyxml/tinyxml.h)
add_subdirectory(googletest)
add_executable(cli ${SOURCE_FILES})
target_link_libraries( cli ${OpenCV_LIBS} )
CMakeLists.txt for gtest.
cmake_minimum_required(VERSION 2.6.2)
project( googletest-distribution )
enable_testing()
option(BUILD_GTEST "Builds the googletest subproject" ON)
#Note that googlemock target already builds googletest
option(BUILD_GMOCK "Builds the googlemock subproject" OFF)
if(BUILD_GMOCK)
add_subdirectory( googlemock )
elseif(BUILD_GTEST)
add_subdirectory( googletest )
endif()
add_subdirectory(basic_tests)
CMakeLists.txt for basic_tests
include_directories($(gtest_SOURCE_DIR}/include
${getest_SOURCE_DIR}))
#include_directories(../../src/)
include_directories(../../include/)
add_executable(runBasicCli
basic_check.cpp)
target_link_libraries(runBasicCli gtest gtest_main)
#target_link_libraries(cli)
I'm assuming your compiler is complaining it can't find the B_RocChoices.h header? Your question seems to imply the compiler error is about not finding a function, but B_RocChoices is a header and not a function in your basic_tests.cpp file.
Assuming your problem is that the compiler isn't finding the B_RocChoices.h header, I expect that when you include_directories(../../include) you are wanting to make the directory where B_RocChoices.h resides part of the header search path. This is a relative path, so it depends where the compiler is being run from as to what path it means (it wouldn't work if you were doing out of source builds, for example). Try using either CMAKE_SOURCE_DIR or CMAKE_CURRENT_SOURCE_DIR to define the path unambiguously. For example:
include_directories(${CMAKE_SOURCE_DIR}/include)
If you are using CMake 2.8.11 or later, I'd recommend you consider using target_include_directories() instead and probably also read up on target_link_libraries(). Together, these allow you to make the header search paths and linked libraries specific to a target rather than global to all targets. Lastly, if you prefer to download GoogleTest as part of your build rather than embedding it directly in your project sources, you may find the following link useful:
https://crascit.com/2015/07/25/cmake-gtest/
Related
I want to be able to call my C++ code as a python package. To do this I am using pybind11 with CMakelists (following this example https://github.com/pybind/cmake_example). My problem is that I have to include GSL libraries in the compilation of the code, and these need an explicit linker -lgsl .
If I were just to compile and run the C++ without wrapping it with python, the following Cmakelists.txt file does the job
cmake_minimum_required(VERSION 3.0)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
project(myProject)
add_executable(
myexecutable
main.cpp
function1.cpp
)
find_package(GSL REQUIRED)
target_link_libraries(myexecutable GSL::gsl GSL::gslcblas)
but when using pybind11 the template I found doesn't allow the add_executable therefore target_link_libraries doesn't work.
I have trie this
project(myProject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES) # See below (1)
# Set source directory
set(SOURCE_DIR "project")
# Tell CMake that headers are also in SOURCE_DIR
include_directories(${SOURCE_DIR})
set(SOURCES "${SOURCE_DIR}/functions.cpp")
# Generate Python module
add_subdirectory(lib/pybind11)
pybind11_add_module(namr ${SOURCES} "${SOURCE_DIR}/bindings.cpp")
FIND_PACKAGE(GSL REQUIRED)
target_link_libraries(GSL::gsl GSL::gslcblas)
but this produces errors in the building.
Any idea ?
Function pybind11_add_module creates a library target, which can be used for link added module with other libraries:
pybind11_add_module(namr ${SOURCES} "${SOURCE_DIR}/bindings.cpp")
target_link_libraries(namr PUBLIC GSL::gsl GSL::gslcblas)
This is explicitely stated in documentation:
This function behaves very much like CMake’s builtin add_library (in fact, it’s a wrapper function around that command). It will add a library target called <name> to be built from the listed source files. In addition, it will take care of all the Python-specific compiler and linker flags as well as the OS- and Python-version-specific file extension. The produced target <name> can be further manipulated with regular CMake commands.
I'm using and like QtCreator to code and build my ROS projects written in c++.
Unfortunately the auto-completion for my own header files is not working: e.g. #include "LineTracker.hh"
Building the project works perfectly. And also the auto-completion for other external packages like ros or opencv is working.
Update 2.0: With QtCreator 3.6 the solution is not working
Update 1.0: Found a solution, see bottom!
Thats how my CMakeLists.txt looks:
cmake_minimum_required(VERSION 2.8.3)
project(line_tracking)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(catkin REQUIRED COMPONENTS
roscpp
tf
sensor_msgs
image_transport
cv_bridge
)
catkin_package()
include_directories(
include
${catkin_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}
)
add_executable(${PROJECT_NAME}
src/line_tracking.cpp
src/EDLineDetector.cpp
src/LineTracker.cpp
)
target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}
)
# --- QT CREATOR STUFF ---
#Add all files in subdirectories of the project in
# a dummy_target so qtcreator has access to all files
FILE(GLOB children ${CMAKE_SOURCE_DIR}/*)
FOREACH(child ${children})
IF(IS_DIRECTORY ${child})
file(GLOB_RECURSE dir_files "${child}/*")
LIST(APPEND extra_files ${dir_files})
ENDIF()
ENDFOREACH()
add_custom_target(dummy_${PROJECT_NAME} SOURCES ${extra_files})
#
The file/package structure looks standard like this:
CMakeLists.txt
|
+ -- src
|
+ -- include
How do I have to adapt my CMakeLists.txt that QtCreator finds my headers for autocompletion?
Thank you very much for your help!
Sidenote:
When I use the top CMakeLists.txt file of the catkin workspace in QtCeator and I include the header files under their package path like this: #include <packageName/include/headerFile.h> the auto-completion is working but the build is not working anymore. So this is only a bad and not userfriendly hack to get auto-completion during coding.
Update 1.0:
I found a solution which is working:
I create a library from all the (class) files which have header files, and link the library to the main file, instead of adding the files as executables. I posted it here as answer.
But I don't know why it is working like this and not without the way over the library. Any explanations?
Update 2.0:
I just upgraded to QtCreator 3.6 and there my solution with the library in not working anymore.
Does anybody know another solution?!
Update: This solution does NOT work with QtCreator 3.6
I found a solution to my own question. Not so much fun, but anyway, I spent a lot of time with that issue so here the solution, which is hopefully useful for others:
Auto-completion with CMakeLists.txt in QtCreator 3.5.1 for your own classes:
Create a library with all your classes: ADD_LIBRARY(myFilesLib src/class1.cpp ...)
Add your executable (main function): add_executable(${PROJECT_NAME} src/main.cpp)
Link your library to your executable: target_link_libraries(${PROJECT_NAME} myFilesLib)
With this way over the library, auto-completion is working in QtCreator!
For ROS (catkin) don't forget to link the ${catkin_LIBRARIES}.
Here the whole CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
project(example_project)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(catkin REQUIRED COMPONENTS
roscpp
)
catkin_package()
include_directories(
include
${catkin_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}
)
# Create a library with all your classes
add_library(myFilesLib
src/class1.cpp
src/class2.cpp
src/class3.cpp
)
target_link_libraries(myFilesLib
${catkin_LIBRARIES}
)
# add your executable
add_executable(${PROJECT_NAME}
src/main.cpp
)
# link the library with your classes to the executable
target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}
myFilesLib
)
I don't know why it is working only with the way over the library but it is working. Maybe somebody has an explanation?!
This is really annoying. Finally I figured out a solution, that works at Qt Creator 4.1.0, and probably works at other versions.
Write your include_directories correctly, so that compilation is OK.
Below the include_directories, add the following line. Note that the sequence matters, the following line must locate below the include_directories.
file(GLOB_RECURSE HEADERS */*.hpp */*.h)
Add ${HEADERS} # for qtcreator... in one of your add_executable. like:
add_executable(slam_pp_node
${HEADERS} # for qtcreator...
src/***.cpp
)
The above should be enough to solve the autocompletion problem. If not, add the following lines at the end of your CMakeList.txt:
file(GLOB_RECURSE EXTRA_FILES */*)
add_custom_target(${PROJECT_NAME}_OTHER_FILES ALL WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} SOURCES ${EXTRA_FILES})
Good luck!
So I'm getting undefined reference to testing::UnitTest::Run() along with some others with gtest. I've compiled the libraries (libgtest.a and libgtest_main.a) and placed them in my lib folder for MinGW and got no where.
Here is my CMakeList.txt:
cmake_minimum_required(VERSION 3.2)
project(proj_tests)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
include_directories("E:/Git/proj")
include_directories("D:/Development/Libraries/gtest-1.7.0/include")
find_package(gtest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
add_executable(proj_tests ${SOURCE_FILES} containers/proj_test.h)
target_link_libraries(proj_tests ${GTEST_LIBRARIES} pthread)
I have done make on the gtest sample and was able to run that without any issues.
I'm using CLion on Windows 7 for the project. How can I make this Google Test framework thing work properly?
When you build gtest, you should have the following three files:
include_fused/gtest/gtest.h
include_fused/gtest/gtest-all.cc
include_fused/gtest/gtest_main.cc
If you are providing your own main you just need the first two. If you want to use the gtest main, you need the third one too. The recommendation is to add these to each unit test project that you are building.
So after a lot of hurt I switched to Linux and tried it there by downloading and compiling the libgtest-dev and using the configuration from Erik Smistad's blog. It worked within the CLion project without issues meaning something weird was happening to my Windows compiled Google Test library.
For windows/MinGW: Here is the solution I reached for Windows..
I got pre-compiled libraries from Richard Pattis's UCI webpage on how to get the google test framework to work on eclipse.
the gtest folder in include was copied to the mingw32\include folder,
the gtest_main.a and libgtest.a files from the make folder files were copied to mingw32\lib folder.
The final working CMake configuration looks like this for me:
cmake_minimum_required(VERSION 3.2)
project(eadlib_tests)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
#path to project to test
include_directories("E:/Git/eadlib")
#Google test framework stuff
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
add_executable(eadlib_tests ${SOURCE_FILES} containers/eadlib_test.h)
target_link_libraries(eadlib_tests ${GTEST_LIBRARIES} pthread)
like this (here's a snippet from one of my library projects)
find_package(GTest REQUIRED)
target_link_libraries(cpputil_test cpputil ${CMAKE_THREAD_LIBS_INIT} ${GTEST_BOTH_LIBRARIES} ${Boost_LIBRARIES} )
add_test(NAME cpputil_test COMMAND cpputil_test)
Of course in my case the project is called cpputil_test. You'll need to replace it with your own.
Note also the use of CMAKE_THREAD_LIBS_INIT which allows your code to be portable across all host systems. It hides the dependency on pthreads when building for linux (OSX, iOS and Windows for example, do not have this dependency).
I already read and searched a lot (e.g. 1 2 3, several docs for CMake, similar projects, etc. to find a solution but I have not been able to solve my problem. I am relatively new to Cmake and Linux (Ubuntu 14.04).
I want to use libsbp (https://github.com/swift-nav/libsbp) to write a program in C++ to communicate with a GPS module. I cloned the repository and installed the C-Library. So now in /usr/local/lib there are two files: libsbp.so and libsbp-static.a and the headers are in /usr/local/include/libsbp
In my own project I include the headers with #include "libsbp/sbp.h" which also works.
Now the Problem: if I want to use a method from libsbp e.g. sbp_state_init(&s); I get undefined reference to "sbp_state_init(sbp_state_t*)"
The relevant part of my Cmake for my own project:
link_directories(/usr/local/lib)
add_executable(main ${QT_SOURCES} ${QT_HEADER_HPP})
target_link_libraries(main ${QT_LIBRARIES} ${catkin_LIBRARIES} sbp)
As I said before, I tried some things:
find_library(SBP_LIB sbp /usr/local/lib) -> same error
same goes for using libsbp in target_link_libraries or searching for it
link_directory(/usr/local/lib)
trying different paths, even moveing libsbp.so into the project directory and "finding" it with ${CMAKE_CURRENT_SOURCE_DIR}
Maybe you can help me!
edit:
this is the CMakeList.txt from the libsbp/c/src directory
if (NOT DEFINED BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS ON)
endif (NOT DEFINED BUILD_SHARED_LIBS)
file(GLOB libsbp_HEADERS "${PROJECT_SOURCE_DIR}/include/libsbp/*.h")
include_directories("${PROJECT_SOURCE_DIR}/CBLAS/include")
include_directories("${PROJECT_SOURCE_DIR}/clapack-3.2.1-CMAKE/INCLUDE")
include_directories("${PROJECT_SOURCE_DIR}/lapacke/include")
include_directories("${PROJECT_SOURCE_DIR}/include/libsbp")
set(libsbp_SRCS
edc.c
sbp.c
)
add_library(sbp-static STATIC ${libsbp_SRCS})
install(TARGETS sbp-static DESTINATION lib${LIB_SUFFIX})
if(BUILD_SHARED_LIBS)
add_library(sbp SHARED ${libsbp_SRCS})
install(TARGETS sbp DESTINATION lib${LIB_SUFFIX})
else(BUILD_SHARED_LIBS)
message(STATUS "Not building shared libraries")
endif(BUILD_SHARED_LIBS)
install(FILES ${libsbp_HEADERS} DESTINATION include/libsbp)
this is the CMakeList.txt from /libsbp/c/
cmake_minimum_required(VERSION 2.8.9)
project(libsbp)
# Setup flags for Code Coverage build mode
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} --coverage" CACHE STRING
"Flags used by the C++ compiler for building with code coverage."
FORCE )
set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_DEBUG} --coverage" CACHE STRING
"Flags used by the C compiler for building with code coverage."
FORCE )
SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE
"${CMAKE_EXE_LINKER_FLAGS_DEBUG} --coverage" CACHE STRING
"Flags used for linking binaries with code coverage."
FORCE )
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
"${CMAKE_SHARED_LINKER_FLAGS_DEBUG} --coverage" CACHE STRING
"Flags used by the shared libraries linker during builds with code coverage."
FORCE )
mark_as_advanced(
CMAKE_CXX_FLAGS_COVERAGE
CMAKE_C_FLAGS_COVERAGE
CMAKE_EXE_LINKER_FLAGS_COVERAGE
CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
# Update the documentation string of CMAKE_BUILD_TYPE for GUIs
set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage."
FORCE )
# Set project version using Git tag and hash.
execute_process(
COMMAND git describe --dirty --tags --always
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
RESULT_VARIABLE GIT_VERSION_FOUND
ERROR_QUIET
OUTPUT_VARIABLE GIT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (GIT_VERSION_FOUND)
set(VERSION "unknown")
else (GIT_VERSION_FOUND)
set(VERSION ${GIT_VERSION})
endif (GIT_VERSION_FOUND)
# Set project version explicitly for release tarballs.
#set(VERSION foo)
message(STATUS "libsbp version: ${VERSION}")
cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
# Some compiler options used globally
set(CMAKE_C_FLAGS "-Wall -Wextra -Wno-strict-prototypes -Wno-unknown-warning-option -Werror -std=gnu99 ${CMAKE_C_FLAGS}")
add_subdirectory(src)
add_subdirectory(docs)
add_subdirectory(test)
It seems that your program uses C++ and the library is written in C.
Symbols in C and C++ are encoded differently (mangled). When including C headers from C++ you need to tell the compiler. This can be done by declaring the symbols extern "C".
extern "C" {
#include <libsbp/sbp.h>
}
Some libraries already include this in their headers, but not sbp.
You have (at least) two possibilities:
Installing the library (this is what you did)
Integrating the library in your CMake project
When installing the library, the target_link_libraries command needs to be modified slightly:
find_library(SBP_LIB sbp /usr/local/lib)
target_link_libraries(main ${QT_LIBRARIES} ${catkin_LIBRARIES} ${SBP_LIB})
When you integrate the library in your CMake project, you can directly use the following command without using find_library. This works, because the library is known to CMake since it is built within the current project.
target_link_libraries(main ${QT_LIBRARIES} ${catkin_LIBRARIES} sbp)
Here's a link to my project, just in case.
So, here's my CMakeLists.txt file:
cmake_minimum_required (VERSION 2.6)
enable_testing()
project (IeiuniumTela)
set (IeiuniumTela_VERSION_MAJOR 1)
set (IeiuniumTela_VERSION_MINOR 0)
set (IeiuniumTela_VERSION_PATCH 0)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/bin)
# TODO: Make conditional for OS X compilation
set(CMAKE_CXX_COMPILER g++-4.7)
set(CMAKE_CC_COMPILER gcc-4.7)
# TODO: Require gcc >= 4.7
set(SOURCES "${SOURCES}" "${CMAKE_BINARY_DIR}/src/http/request.cpp")
set(SOURCES "${SOURCES}" "${CMAKE_BINARY_DIR}/src/http/response.cpp")
set(SOURCES "${SOURCES}" "${CMAKE_BINARY_DIR}/src/http/server.cpp")
set(SOURCES "${SOURCES}" "${CMAKE_BINARY_DIR}/src/log.cpp")
set(SOURCES "${SOURCES}" "${CMAKE_BINARY_DIR}/src/main.cpp")
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "-Wall ${CMAKE_CXX_FLAGS}")
add_executable(ieiunium_tela.srv ${SOURCES})
target_link_libraries(ieiunium_tela.srv yaml-cpp)
target_link_libraries(ieiunium_tela.srv boost_system-mt)
set(GTEST_DIR "${CMAKE_BINARY_DIR}/../gtest-1.6.0")
include_directories(SYSTEM "${GTEST_DIR}/include")
find_library(GTEST_LIBRARY
NAMES gtest
libgtest
libgtest.a
PATHS "${GTEST_DIR}")
find_library(GTEST_LIBRARY_MAIN
NAMES gtest_main
libgtest_main
libgtest.a
PATHS "${GTEST_DIR}")
set(GMOCK_DIR "${CMAKE_BINARY_DIR}/../gmock-1.6.0")
include_directories(SYSTEM "${GMOCK_DIR}/include")
include_directories(SYSTEM "${GMOCK_DIR}/gtest/include")
find_library(GMOCK_LIBRARY
NAMES gmock
libgmock
libgmock.a
PATHS "${GMOCK_DIR}")
find_library(GMOCK_LIBRARY_MAIN
NAMES gmock_main
libgmock_main
libgmock_main.a
PATHS "${GMOCK_DIR}")
set(TEST_SOURCES "${TEST_SOURCES}" "${CMAKE_BINARY_DIR}/test/unit/http/test_request.cpp")
set(TEST_SOURCES "${TEST_SOURCES}" "${CMAKE_BINARY_DIR}/test/unit/http/test_response.cpp")
set(TEST_SOURCES "${TEST_SOURCES}" "${CMAKE_BINARY_DIR}/test/unit/http/test_server.cpp")
set(TEST_SOURCES "${TEST_SOURCES}" "${CMAKE_BINARY_DIR}/test/unit/test_log.cpp")
set(TEST_SOURCES "${TEST_SOURCES}" "${CMAKE_BINARY_DIR}/test/unit/test_main.cpp")
add_executable(ieiunium_tela.srv_TEST ${TEST_SOURCES})
target_link_libraries(ieiunium_tela.srv_TEST ${GTEST_LIBRARY} ${GTEST_LIBRARY_MAIN})
target_link_libraries(ieiunium_tela.srv_TEST ${GMOCK_LIBRARY} ${GMOCK_LIBRARY_MAIN})
And, here's my question: Why isn't cmake finding my gtest/gmock libs?
I've gone into their respective directories and built them using ./configure && make -j4 and a brief find for the shared libraries shows them there.
Another option would be to build gtest and gmock as part of your main CMake build rather than building them separately and using find_package() or find_library(). This comes with some advantages, including:
They are built with the same compiler and settings as your main build, so there won't be issues with mismatched symbols, etc.
No manual steps required before the build and no need to tell CMake where to find things in your main CMakeLists.txt.
I recently wrote an article about how to download and build gtest as part of your build. Unlike the more commonly suggested approaches, my article shows how to download gtest during configure time (i.e. when CMake is run), which then allows you to bring it into your project using add_subdirectory(). This makes the gtest and gtest_main CMake targets directly available to your project, so you can just link against them like any other library. The approach is general and may work for mock too. You can find that article here:
https://crascit.com/2015/07/25/cmake-gtest/
Update: This approach is now also part of the googletest documentation.
The gtest/gmock libraries aren't built to the actual root of these projects. I believe on Linux they're built to "lib" inside the root.
To tell CMake to search in "lib" but without modifying your GTEST_DIR or GMOCK_DIR variables, you can use the PATH_SUFFIXES argument to the find_library command:
find_library(GTEST_LIBRARY
NAMES gtest
libgtest
libgtest.a
PATHS "${GTEST_DIR}"
PATH_SUFFIXES lib)
You can add a list of suffixes to give values appropriate for Windows builds too if required.
There is a standard CMake module provided to find gtest (FindGTest). You could look at the code in there for ideas, or actually use it instead of your own code. Unfortunately, there isn't a FindGMock module.
I have a couple of other minor points.
It might be worth considering allowing the GTEST_DIR and GMOCK_DIR variables to be set by the user via the -D flag to allow these to exist anywhere relative to your project's root. In that case, you'd only set them inside the CMakeLists.txt if they weren't already set.
Another minor point is that GMock comes with GTest included. You could just allow users to build GMock, and then find and use the version inside the GMock tree.