如何让CMake在我的C库项目中找到测试?

时间:2019-11-11 11:44:59

标签: unit-testing cmake

我在这里有一个项目: https://github.com/edhartnett/ncglm

这是一个小的C库,用于从对地闪电映射器中读取netCDF数据文件。我有一个可以正常工作的自动工具版本,并且正在尝试添加CMake版本。

目录结构很简单,有一个主目录,一个src目录和一个测试目录。

在主目录中,我有:

# This is the main cmake file for ncglm, a library to help read the
# netCDF data files from the Global Lightning Mapper (GLM) instrument
# on GOES-16 and GOES-17.
#
# Ed Hartnett 11/10/19

# This will use any cmake between 3.1 and 3.15, preferint later
# versions with updated policies.
cmake_minimum_required(VERSION 3.1...3.15)
if (${CMAKE_VERSION} VERSION_LESS 3.12)
  cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()

# set the project name
project(ncglm VERSION 1.0)

#Add custom CMake Module
SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/"
  CACHE INTERNAL "Location of our custom CMake modules.")

# Find netCDF.
include(FindNetCDF)

include_directories("${NETCDF_INCLUDES}")

# Create a config.h.
configure_file(config.h.cmake.in config.h)

# Turn on testing.
enable_testing()
include(CTest)

# Build in this subdirectory.
add_subdirectory(src test)

在src子目录中,我有:

# This is the cmake file for the src directory of the ncglm library.

# Ed Hartnett 11/10/19

# Build the ncglm library.
add_library(ncglm glm_read.c goes_glm.h glm_data.h)

在测试目录中,我有:

# This is the cmake build file for the test directory of the ncglm library.
#
# Ed Hartnett 11/10/19

enable_testing()
add_test(NAME tst_glm_read COMMAND tst_glm_read)
build_test(tst_glm_read)
add_sh_test(nc_test4 run_glm_tests)

建造时,我得到:

ed@mikado:~/ncglm/build$ cmake -DNETCDF_INCLUDES=/usr/local/netcdf-c-4.7.2_hdf5-1.10.5/include -DNETCDF_LIBRARIES=/usr/local/netcdf-c-4.7.2_hdf5-1.10.5/lib .. && make test
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found NetCDF: /usr/local/netcdf-c-4.7.2_hdf5-1.10.5/lib  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ed/ncglm/build
Running tests...
Test project /home/ed/ncglm/build
No tests were found!!!

cmake为什么不尝试构建或运行测试?

1 个答案:

答案 0 :(得分:2)

  

add_subdirectory(src测试)

这是无效的/它没有按照您的想象做。您想做的是:

add_subdirectory(src)
add_subdirectory(test)

cmake网站现在关闭,我无法链接到它。 add_subdirectory(src test)从内存中读取src源目录中的CMakeLists.txt,但在test构建目录中构建源,test在{ {1}}。要添加两个源目录,必须使用两个add_subdirectory()