简体   繁体   中英

No rule to make target for googletest

Scraping the rust off my C++ after many, many years. Lots of new things including CmakeLists.txt. I'm using CLION and googletest, both of which I'm also new to.

I followed the tutorial https://raymii.org/s/tutorials/Cpp_project_setup_with_cmake_and_unit_tests.html to set up unit testing for a basic project. Now I'm trying to adapt that project to my test project.

I have changed the library structure. The src directory still contains my source and tst my test, but I have created a lib directory and a git submodule for googletest there.

Here is my root CmakeLists.txt

cmake_minimum_required(VERSION 3.17)
project(processcontroller_test)

set(CMAKE_CXX_STANDARD 17)

include_directories(src)

add_subdirectory(src)
add_subdirectory(tst)
add_subdirectory(lib/googletest)

and here is the one for src

set(BINARY ${CMAKE_PROJECT_NAME})

file(GLOB_RECURSE SOURCES LIST_DIRECTORIES true *.h *.cpp)

set(SOURCES ${SOURCES})

add_executable(${BINARY}_run ${SOURCES})

add_library(${BINARY}_lib STATIC ${SOURCES})

and the one for tst

set(BINARY ${CMAKE_PROJECT_NAME}_tst)

file(GLOB_RECURSE TEST_SOURCES LIST_DIRECTORIES false *.h *.cpp)

set(SOURCES ${TEST_SOURCES})

add_executable(${BINARY} ${TEST_SOURCES})

add_test(NAME ${BINARY} COMMAND ${BINARY})

target_link_libraries(${BINARY} PUBLIC ${CMAKE_PROJECT_NAME}_lib gtest)

Now when I run the tests using tst/main.cpp I get:

====================[ Build | processcontroller_test_tst | Debug ]==============
/opt/clion/bin/cmake/linux/bin/cmake --build /home/thomas/CLionProjects/processcontroller-test/cmake-build-debug --target processcontroller_test_tst -- -j 12
gmake[3]: *** No rule to make target 'src/CMakeFiles/processcontroller_test_lib.dir/build'.  Stop.
gmake[2]: *** [CMakeFiles/Makefile2:227: src/CMakeFiles/processcontroller_test_lib.dir/all] Error 2
gmake[2]: *** Waiting for unfinished jobs....
[ 40%] Built target gtest
gmake[1]: *** [CMakeFiles/Makefile2:262: tst/CMakeFiles/processcontroller_test_tst.dir/rule] Error 2
gmake: *** [Makefile:210: processcontroller_test_tst] Error 2

I think the problem is in the last line of my tst/CMakeLists.txt file

target_link_libraries(${BINARY} PUBLIC ${CMAKE_PROJECT_NAME}_lib gtest)

But I can't see how. Tried reading from the docs about this but can't seem to make heads or tails of it.

I added message(STATUS "Source list:" ${SOURCES}) to the src make file and message(STATUS "Test source list:" ${SOURCES}) to the tst make and got these results.

-- Source list:/home/thomas/CLionProjects/processcontroller-test/src/water-dispenser.h
-- Test source list:/home/thomas/CLionProjects/processcontroller-test/tst/Arduino-mock.h/home/thomas/CLionProjects/processcontroller-test/tst/main.cpp/home/thomas/CLionProjects/processcontroller-test/tst/water-dispenser-test.cpp

I don't understand these things, but everything I tried failed. Finally I just dumped the project started a new one, recreated everything step by step with the same make files and everything's now working. Something jammed up in CLion.

You can tell that Clion is not JetBrains's flagship product. They need to focus on it a bit more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM