The contens of CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(Tutorial VERSION 1.0)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# should we use our own math functions
option(USE_MYMATH "Use tutorial provided math implementation" ON)
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
# add the MathFunctions library
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
endif()
# add the executable
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
DESTINATION include
)
enable_testing()
# does the application run
add_test(NAME Runs COMMAND Tutorial 25)
# does the usage message work?
add_test(NAME Usage COMMAND Tutorial)
set_tests_properties(Usage
PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
)
# define a function to simplify adding tests
function(do_test target arg result)
add_test(NAME Comp${arg} COMMAND ${target} ${arg})
set_tests_properties(Comp${arg}
PROPERTIES PASS_REGULAR_EXPRESSION ${result}
)
endfunction()
# do a bunch of result based tests
do_test(Tutorial 4 "4 is 2")
do_test(Tutorial 9 "9 is 3")
do_test(Tutorial 5 "5 is 2.236")
do_test(Tutorial 7 "7 is 2.645")
do_test(Tutorial 25 "25 is 5")
do_test(Tutorial -25 "-25 is (-nan|nan|0)")
do_test(Tutorial 0.0001 "0.0001 is 0.01")
Rebuild the application and then cd to the binary directory and run the ctest executable: ctest -N and ctest -VV. For multi-config generators (e.g. Visual Studio), the configuration type must be specified with the -C <mode> flag. For example, to run tests in Debug mode use ctest -C Debug -VV from the binary directory (not the Debug subdirectory!). Release mode would be executed from the same location but with a -C Release. Alternatively, build the RUN_TESTS target from the IDE.
I run the cmake commands like above, but it can not run successfully.
I Rebuild the application and cd to the bin directory, and run
ctest -C Debug -VV
the rusult is
No tests were found!!!
What did I miss?
Should be run from the build directory, it's not clear in the tutorial, but if you go to the documentation you can see it.
https://cmake.org/cmake/help/book/mastering-cmake/chapter/Testing%20With%20CMake%20and%20CTest.html
I am in the same position. However I found I could run the tests from my /build dir.
ctest -n ../install/bin
Related
Colleagues,
I have 2 executable and 2 shell scripts for test. In Cmake I have:
add_executable(test1 test1.cpp)
add_executable(test2 test2.cpp)
add_test(NAME test_bin COMMAND test1 test2)
and
add_test(NAME test_sh
COMMAND s1.sh e1.sh)
set_tests_properties(test_sh PROPERTIES TIMEOUT 1.1)
how to do the same in Bazel BUILD? I cannot use separate command line because the tests are the part of the project and I cannot change cmd arguments
i am testing my cpp Application with googletest and these part runs very good.
To build and start the Unit-Tests i run a small Powershell Skript.
cmake -G "Unix Makefiles" -S . -B ${BUILD_PATH}
cmake --build $BUILD_PATH
Set-Location $BUILD_PATH
./UnitTests
The CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.14)
project(exampleProject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS --coverage)
# GoogleTest
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
# Testsources
file(GLOB_RECURSE TEST_SOURCES "./test/*.cc")
# Linking
# Add here the "inc" and "src" folder from the features
include_directories(
"./Example/inc"
"./Example/src"
"./test/Example/mock"
)
add_executable(
unittests
${TEST_SOURCES}
)
target_link_libraries(
unittests
gtest_main
gmock_main
)
include(GoogleTest)
# Run
gtest_discover_tests(unittests)
The following Line of Code in my .sh Script works on MacOs perfectly.
lcov --directory ${TEST_SOURCE_PATH} --capture --output-file ${LCOV_PATH_INFO} -rc lcov_branch_coverage=1
The same i will do on the Windows Machine with this Code in a another Powershell Script:
$LCOV = "C:\ProgramData\chocolatey\lib\lcov\tools\bin\lcov"
perl $LCOV --capture --directory ${TEST_SOURCE_PATH} --output-file ${LCOV_PATH_INFO}
When i run these Script then the .info File has strage Path at the SF-Tag For Example:
SF:C:\dev\exampleProject\build\test\CMakeFiles\unittests.dir\test\Example\C:/Strawberry/c/lib/gcc/x86_64-w64-mingw32/8.3.0/include/c++/ext/alloc_traits.h
The geninfo at the Powershell says me that he cant open these Files, which is logical.
So why i became these stange Paths?
Thank you. Best Guards
Daniel
I read many stuff about this but i found no Solution.
So I found a Solution with a Powershell Script.
At first I create a lcov.info file.
After that the created File will be clean about the File Extensions.
At last I clean the strange Path with the replace Command.
So it works well.
I have followed the CMake Tutorial - Step 4: Installing and Testing
and everything worked fine until I needed to pass the --prefix argument to my script.
I use Ubuntu 20.01 and Visual Studio Code with the CMakeTool extension.
If I config build and install my app from the CMakeTool extension shortcut command by invoking Ctrl+P Visual Studio Studio Code options, everything works as expected, but if I repeat same command in Visual Studio Code's terminal with --prefix/-target options, it points to another location and I get errors.
/usr/bin/cmake --build /mnt/Pliki_Mic/C++/Ncurses/build --config Debug --target "/home/mic/Ncurses" install -j 10 --
error: unknown target '/home/mic/Ncurses'
And another:
cmake --install . --prefix "/home/mic/Ncurses"
-- Install configuration: "Debug"
-- Up-to-date: /mnt/Pliki_Mic/C++/Ncurses/build/bin/ncurses
-- Up-to-date: /mnt/Pliki_Mic/C++/Ncurses/build/bin/include/TutorialConfig.h
-- Up-to-date: /mnt/Pliki_Mic/C++/Ncurses/build/bin/lib/libMathFunctions.a
-- Up-to-date: /mnt/Pliki_Mic/C++/Ncurses/build/bin/include/MathFunction.h
But it wont produce a binary in the expected 'prefixed' directory.
And here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(ncurses VERSION 1.0)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# should we use our own math functions
option(USE_MYMATH "Use ncurses provided math implementation" ON)
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
set(CMAKE_INSTALL_PREFIX ${PROJECT_BINARY_DIR}/bin)
# add the MathFunctions library
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
endif()
# add the executable
add_executable(ncurses tutorial.cxx)
target_link_libraries(ncurses PUBLIC ${EXTRA_LIBS})
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(ncurses PUBLIC
"${PROJECT_BINARY_DIR}"
)
install(TARGETS ncurses DESTINATION ${CMAKE_INSTALL_PREFIX})
#install(TARGETS ncurses DESTINATION /home/mic/Ncurses)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
DESTINATION ${PROJECT_BINARY_DIR}/bin/include
)
enable_testing()
# does the application run
add_test(NAME Runs COMMAND ncurses 25)
# does the usage message work?
add_test(NAME Usage COMMAND ncurses)
set_tests_properties(Usage
PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
)
# define a function to simplify adding tests
function(do_test target arg result)
add_test(NAME Comp${arg} COMMAND ${target} ${arg})
set_tests_properties(Comp${arg}
PROPERTIES PASS_REGULAR_EXPRESSION ${result}
)
endfunction(do_test)
# do a bunch of result based tests
do_test(ncurses 4 "4 is 2")
do_test(ncurses 9 "9 is 3")
do_test(ncurses 5 "5 is 2.236")
do_test(ncurses 7 "7 is 2.645")
do_test(ncurses 25 "25 is 5")
do_test(ncurses -25 "-25 is [-nan|nan|0]")
do_test(ncurses 0.0001 "0.0001 is 0.01")
Remove:
set(CMAKE_INSTALL_PREFIX ${PROJECT_BINARY_DIR}/bin)
install(TARGETS ncurses DESTINATION ${CMAKE_INSTALL_PREFIX})
Prefix comes from the user. You do not know it.
Use relative paths.
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
DESTINATION bin/include
)
Will install at <prefix>/bin/include. Read install documentation https://cmake.org/cmake/help/latest/command/install.html .
So I am still pretty new to linux and CMake, I want to add an X package as Library to a currently existing program. The instructions say Now, link the X library to your project by extending the target_link_libraries command of your executable:
target_link_libraries(target-name X package [other-libraries])
What does the executable mean? Does it mean the name of the executable file .sh ? or the project name in CMakeLists.txt as adding the latter gives this cmake error:
CMake Error at CMakeLists.txt:79 (target_link_libraries):
Cannot specify link libraries for target "Simulation" which is not built by
this project.
Thank you for any advice!
EDIT 1: CMakeList.txt as requested
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
cmake_policy(SET CMP0079 NEW)
# Set project name:
project(Simulation)
########## The project ##########
# Traverse source folder and find extra includes.
file(GLOB_RECURSE files ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR} *.h *.hh)
option(BUILD_SHARED_LIBS "Build package with shared libraries." OFF)
set(last_include_path "")
foreach(file ${files} )
get_filename_component(path ${file} PATH)
# Check if the path has not been processed before.
string(COMPARE EQUAL "${path}" "${last_include_path}" already_included)
if(NOT ${already_included})
# Adapt last_include_path.
set(last_include_path "${path}")
# Add include path if include subdirectory exits.
if(IS_DIRECTORY ${path})
message(STATUS "Added include: ${path}")
include_directories(${path})
endif()
endif()
endforeach()
# make documentation (after all subproject (which add dependencies to the documentation) are build):
add_custom_target(documentation ALL
COMMAND echo Creating documentation
COMMAND rm -rf ../documentation
COMMAND mkdir -p ../documentation
COMMAND doxygen GODDeSS.dox > /dev/null
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
# add_custom_command(TARGET documentation
# POST_BUILD
# COMMAND echo ARGS Creating documentation pdf-files
# COMMAND sed ARGS -e "s#makeindex#makeindex -q#" -i Makefile
# COMMAND make all > /dev/null
# COMMAND cp refman.pdf ../documentation.pdf
# COMMAND touch ../documentation.html
# COMMAND echo ARGS \"<html> <head> <meta http-equiv=\\\"refresh\\\" content=\\\"0\\; URL=html/index.html\\\"/> </head> </html>\" > ../documentation.html
# WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../documentation/latex)
# Add subdirectories which contain CMakeLists, too (they will be executed after this project or, if this project depends on them, before this project):
message(STATUS "Added subproject: ${CMAKE_CURRENT_SOURCE_DIR}/Simulation")
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Simulation)
message(STATUS "Added subproject: ${CMAKE_CURRENT_SOURCE_DIR}/G4BasicObjects")
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/G4BasicObjects)
#added G4Sipm
add_subdirectory(externals/g4sipm)
include_directories(externals/g4sipm/g4sipm/include)
include_directories(externals/g4sipm/externals/gtest/include)
target_link_libraries(Simulation g4sipm boost_date_time)
I have built a CERN's ROOT script which based on c++ and i write(edit an example) CMakeList.txt. I am so rokie at CMake btw.
When I command to compile with cmake .., it done correctly -i think- with no errors. But .exe file what i want to produce does not appear.
My directory orders
/Project
../TLV.cpp
../CMakeLists.txt
../build
There is my CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(TLV)
#Set CXX flags to compile with c++11 and error warnings quiet
set(CMAKE_CXX_FLAGS "-O3 -fPIC -Wall -Wextra -std=c++11 -m64")
#Load root
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} $ENV{ROOTSYS}/etc/cmake)
#print conf
message(STATUS "Environmental CMAKE_MODULE_PATH is $ENV{ROOTSYS}")
#find the package with extra libraries needed
find_package(ROOT MODULE REQUIRED Cling TreePlayer Tree Rint Postscript Matrix RIO Core Foam RooStats RooFit RooFitCore Gpad Graf3d Graf Hist Net TMVA XMLIO MLP)
#include ROOT stuff
include(${ROOT_USE_FILE})
message(STATUS "Environmental ROOTSYS is $ENV{ROOTSYS}")
message(STATUS "found root at: ${ROOT_USE_FILE}")
message(STATUS "ROOT_LIBRARIES=${ROOT_LIBRARIES}")
#produce executables in bin path
set(EXECUTABLE_OUTPUT_PATH bin)
#include_directories(./../Framework Headers)
#${FROM_OTHERS_INCLUDE})
#defines all .cpp support class with corresponding Headers
#file(GLOB SRCS Sources/*.cpp Headers/*.hpp )
#${FROM_OTHERS_HEADER} ${FROM_OTHERS_SOURCE})
#add executable
add_executable( TLV TLV.cpp )
#link the executable with the root libraries
target_link_libraries(TLV ${ROOT_LIBRARIES})
I do not get it where I am wrong.
A typical scenario on a project which uses cmake is
cd src_directory # for example cd ~/src/root-6.08.06/
mkdir build # skip this if dir build already exists
cd build
cmake .. # the .. just says your source home dir is up a dir
cmake-gui .. # (optional) skip this unless you need a GUI alternative to cmake where you can edit settings
cmake --build # if you have a quad core CPU could use: make -j8 ... or make -j $(nproc) # on linux
then launch binary and confirm its OK then if desired install it using
sudo make install