I'm working on a project where the CMake structure looks like this:
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
set(CMAKE_CXX_STANDARD 20)
project(overflow VERSION 0.1.0)
add_subdirectory(src)
enable_testing()
include(CTest)
add_subdirectory(test)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
src/CMakeLists.txt
find_package(Boost REQUIRED)
set(HEADER_FILES overflow.h)
include_directories(${Boost_INCLUDE_DIRS})
add_library(overflow ${HEADER_FILES})
link_libraries(overflow PRIVATE Boost::uuid) # Fails with `target_`
These work fine. However, I get an error when I set up GTest as shown below in my test directory.
test/CMakeLists.txt
## Setup GTest
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/e2239ee6043f73722e7aa812a459f54a28552929.zip
)
### For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_executable(overflowtest overflow.i.t.cpp)
target_link_libraries(overflowtest PRIVATE
overflow
gtest
gtest_main)
include(GoogleTest)
gtest_discover_tests(overflowtest)
CMake builds correctly, but when I run the test, the error in overflow.h is:
boost/uuid/uuid.hpp: No such file or directory
What I find strange is that if I change my settings so that test/CMakeLists.txt looks like:
test/CMakeLists.txt
find_package(GTest CONFIG REQUIRED)
add_executable(overflowtest overflow.i.t.cpp)
target_link_libraries(overflowtest PRIVATE
overflow
GTest::gtest
GTest::gtest_main)
There are no errors and it runs correctly. Can someone please explain why using the FetchContent method recommended by Google seems to break my project?
Related
How do I connect the libxlsxwriter library to my personal project using CMake.
I cloned the official library files from https://github.com/jmcnamara/libxlsxwriter.
Then I extracted it in my personal project and added a few lines of CMake:
cmake_minimum_required(VERSION 3.11.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
[enter image description here][1]
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG main # release-1.10.0
)
include(FetchContent)
FetchContent_Declare(
Bcrypt
GIT_REPOSITORY https://github.com/veltro123/Bcrypt.cpp
GIT_TAG master
)
FetchContent_MakeAvailable(Bcrypt)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
# Now simply link against gtest or gtest_main as needed. Eg
#ADDED
include_directories("libxlsxwriter/include")
set(XLSXWRITER_LIBRARY "libxlsxwriter/build/libxlsxwriter.a")
add_library(xlsxwriter STATIC IMPORTED)
set_target_properties(xlsxwriter PROPERTIES IMPORTED_LOCATION ${XLSXWRITER_LIBRARY})
target_link_libraries(${PROJECT_NAME} xlsxwriter)
#END
project(MONEYTRACKER)
enable_testing()
add_executable(${PROJECT_NAME} main.cpp Src/Transaction.cpp Src/Date.cpp Src/User.cpp)
add_executable(${PROJECT_NAME}-ut Testing/DateTests.cpp Testing/UserTests.cpp Src/Transaction.cpp Src/Date.cpp Src/User.cpp)
target_link_libraries(${PROJECT_NAME}-ut gtest_main)
target_link_libraries(${PROJECT_NAME} PRIVATE bcrypt)
include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME}-ut)
This is the error I get:
I'm trying to add google test to my project and keep getting
"[build] get_property could not find TARGET testcolor. Perhaps it has not yet been
"
This is my cmake file:
cmake_minimum_required(VERSION 3.13)
project(RAYTRACE)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
include(CTest)
enable_testing()#Redundent I know
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
include_directories("<...>/googletest-src/googletest/include")
##GTEST##
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
set(SOURCE_FILES
main.cpp
src/Utils/testcolor.cc
src/Utils/testcolor
)
add_executable(RAYTRACE ${SOURCE_FILES})
target_link_libraries(RAYTRACE
PRIVATE
testcolor
GTest::gtest_main
GTest::gtest
)
include(GoogleTest)
gtest_discover_tests(testcolor)
Without the last line gtest is built properly and the gtest.h header is included in "src/Utils/testcolor.cc" and detected.
yet I cant add the test.
I solved the problem by following the This
guide online which made me undersand my many many mistakes configuring CMake and GTest. Thanks!
I've build my unit test code on Ubuntu 21.10, CMake 3.18.4 and GTest 1.10.0.20201025-1.1.
I wrote CMakeList.txt file as this.
# The minimum version of CMake Required
cmake_minimum_required (VERSION 2.8.12)
# Any project name will suffice, this has connotaions when using advanced CMake Features
set(PROJECT_NAME tests)
project (${PROJECT_NAME})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Just in case someone had the include files in seperate directory
include_directories(../include)
include_directories(..)
# This uses the complete student's library
aux_source_directory(.. SRC_LIST)
list(REMOVE_ITEM SRC_LIST "../main.cpp")
message ( STATUS "Compiling test_lib with following files ${SRC_LIST}" )
add_library(test_lib ${SRC_LIST})
# Now we make the gtests
set(GTEST_ROOT "/usr/src/gtest" CACHE PATH "Path to googletest")
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
if(NOT GTEST_LIBRARY)
message("GTest library not found")
endif()
add_executable(rawTests test_rawdata.cpp)
target_link_libraries(rawTests ${GTEST_LIBRARIES} pthread)
target_link_libraries(rawTests test_lib)
add_executable(timeTests test_time.cpp)
target_link_libraries(timeTests ${GTEST_LIBRARIES} pthread)
target_link_libraries(timeTests test_lib)
It works properly on my end.
But when I deliver this to my friend who uses CMake 3.22.4, it throws error look like this
Error Image
It's kind of weird issue and I didn't ever faced this sort of issue before.
I wonder anybody who has deep knowledge for CMake and GTest can help me to handle this.
Thank you in advance.
This question already has answers here:
CMake link to external library
(6 answers)
Closed 1 year ago.
I'm a newbie in CMake, i know there are many StackOverflow post asking this, but none of those posts helped me fix this error, this is what my workspace looked like
project
.vscode
build
include\SDL2
lib\SDL2_x64
src
CMakeLists.txt
main.cpp
CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(project VERSION 0.1.0)
include(CTest)
enable_testing()
add_subdirectory(src)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
src/CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
enable_testing()
add_executable(${CMAKE_PROJECT_NAME} main.cpp)
get_filename_component(PARENT_DIRECTORY ../ ABSOLUTE)
find_library(
SDL2_X64_LIB
PATHS "${PARENT_DIRECTORY}/lib"
)
find_path(
INCLUDE_DIR
PATHS "${PARENT_DIRECTORY}/include"
)
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC "${SDL2_X64_LIB}")
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC "${INCLUDE_DIR}")
# For debugging
message(STATUS "SDL2_LIB => ${SDL2_X64_LIB}")
message(STATUS "include_DIR => ${INCLUDE_DIR}")
I'm trying to add SDL2 include and library folder to CMake but it didn't find the file path
-- SDL2_LIB => SDL2_X64_LIB-NOTFOUND
-- include_DIR => INCLUDE_DIR-NOTFOUND
-- Configuring done
I tried putting the path into set() but it still can't find SDL2/SDL.h, I'm using the latest version of CMake
The proper solution is to create an IMPORTED target from the files you downloaded and link to it. You grabbed the MinGW version, so I am focusing on that:
Add this to your CMakeLists.txt in the root:
add_library(SDL2 STATIC IMPORTED)
set_target_properties(SDL2 PROPERTIES
IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/lib/SDL2_x64/libSDL2.a"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include")
And then you link to that target in your other CMake file:
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC SDL2)
This is my first time attempting the Google Test API in C++ and one of my first experiences with CMake. If it's useful, I'm using CLion. My CMake file, shown below, should ostensibly allow for files to #include <gtest/gtest.h>:
cmake_minimum_required(VERSION 3.19)
project(chord)
# GoogleTest requires at least C++11
set(CMAKE_CXX_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread")
add_executable(
chord
main.cpp
src/node.h
src/node.cpp
src/log.h
src/log.cpp
src/sha1.h
src/sha1.cpp
src/messageParser.h
src/messageParser.cpp
src/fingerTable.h
src/fingerTable.cpp
src/key.h
src/key.cpp)
include(GoogleTest)
enable_testing()
add_executable(
unitTests
test/fingerTableTests.cc
test/keyTests.cc
test/logTests.cc
test/messageParserTests.cc
test/nodeTests.cc
test/messageParserTests.cc
)
target_link_libraries(
unitTests
gtest_main
)
include(GoogleTest)
gtest_discover_tests(unitTests)
However, when I attempt #include <gtest/gtest.h> from main.cpp, I receive the error gtest/gtest.h: No such file or directory. Can someone pinpoint the error in my CMake file? Thanks and sorry for the trouble.
Try to also link gtest:
target_link_libraries(
unitTests
gtest_main
gtest
)