如何使用cmake分别编译两组源文件?

How to compile the two set of source file separately using cmake?

我正在尝试使用 c++ 项目中的 gcov 和 lcov 生成覆盖率报告。 这是 cmake 文件 link。现在 test_src 有两种类型的文件。一个是单元测试文件。另一组是实际的应用程序源文件。现在我想要的是只编译带有附加 --coverage 标志的应用程序源文件,而不是其他单元测试文件。可能吗?

更新

我失败的尝试 1:

set_property(SOURCE [../agent/adapter.cpp [../agent/agent.cpp [../agent/checkpoint.cpp [../agent/component.cpp [../agent/component_event.cpp [../agent/change_observer.cpp [../agent/connector.cpp [../agent/cutting_tool.cpp [../agent/data_item.cpp [../agent/device.cpp [../agent/globals.cpp [../agent/options.cpp [../agent/xml_parser.cpp [../agent/xml_printer.cpp [../agent/config.cpp [../agent/service.cpp [../agent/ref_counted.cpp [../agent/asset.cpp [../agent/version.cpp [../agent/rolling_file_logger.cpp]]]]]]]]]]]]]]]]]]]] PROPERTY [${GCOV_COMPILE_FLAGS}])

我失败的尝试 2:

set_property(SOURCE ../agent/*.cpp PROPERTY [${GCOV_COMPILE_FLAGS}])

我也试过不带括号。

这是official Doc:

我错过了什么?

更新2

file(GLOB AGENT_SOURCES ../agent/*.cpp) set_property(SOURCE AGENT_SOURCES PROPERTY ${GCOV_COMPILE_FLAGS})

没用。尝试从 test_srcs 中删除这些文件,但它导致了错误。

我的文件

cmake_minimum_required (VERSION 2.6) 

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../agent/CMake;${CMAKE_MODULE_PATH}")

set(CMAKE_BUILD_TYPE DEBUG)
set(CMAKE_FIND_FRAMEWORK NEVER FORCE)
set(CMAKE_FIND_APPBUNDLE NEVER)

set(GCOV_COMPILE_FLAGS  "-fprofile-arcs -ftest-coverage -r")
set(GCOV_LINK_FLAGS "-lgcov")
if(WIN32)
  set(LibXML2_INCLUDE_DIRS ../win32/libxml2-2.9/include )

  if(CMAKE_CL_64)
    set(bits 64)
  else(CMAKE_CL_64)
    set(bits 32)
  endif(CMAKE_CL_64)

  file(GLOB LibXML2_LIBRARIES "../win32/libxml2-2.9/lib/libxml2_a_v120_${bits}.lib")
  file(GLOB LibXML2_DEBUG_LIBRARIES ../win32/libxml2-2.9/lib/libxml2d_a_v120_${bits}.lib)

  set(CPPUNIT_INCLUDE_DIR ../win32/cppunit-1.12.1/include)
  file(GLOB CPPUNIT_LIBRARY ../win32/cppunit-1.12.1/lib/cppunitd_v120_a.lib)
endif(WIN32)

if(UNIX)
  execute_process(COMMAND uname OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CMAKE_SYSTEM_NAME)
  if(CMAKE_SYSTEM_NAME MATCHES Linux)
    set(LINUX_LIBRARIES pthread)
  endif(CMAKE_SYSTEM_NAME MATCHES Linux)
endif(UNIX)

project (test)

set(test_srcs test.cpp
           adapter_test.cpp
           agent_test.cpp
           checkpoint_test.cpp
           config_test.cpp
           component_test.cpp
           component_event_test.cpp
           connector_test.cpp
           data_item_test.cpp
           device_test.cpp
           globals_test.cpp
           xml_parser_test.cpp
           test_globals.cpp
           xml_printer_test.cpp
           asset_test.cpp
           change_observer_test.cpp
           cutting_tool_test.cpp
           )

file(GLOB test_headers *.hpp ../agent/*.hpp)

include_directories(../lib ../agent .)

find_package(CppUnit REQUIRED)
find_package(LibXML2 REQUIRED)


add_definitions(-DDLIB_NO_GUI_SUPPORT ${LibXML2_DEFINITIONS})

set(AGENT_SOURCES ../agent/adapter.cpp 
           ../agent/agent.cpp 
           ../agent/checkpoint.cpp
           ../agent/component.cpp 
           ../agent/component_event.cpp 
           ../agent/change_observer.cpp
           ../agent/connector.cpp
           ../agent/cutting_tool.cpp
           ../agent/data_item.cpp 
           ../agent/device.cpp 
           ../agent/globals.cpp 
           ../agent/options.cpp
           ../agent/xml_parser.cpp 
           ../agent/xml_printer.cpp
           ../agent/config.cpp
           ../agent/service.cpp
           ../agent/ref_counted.cpp
           ../agent/asset.cpp
           ../agent/version.cpp
           ../agent/rolling_file_logger.cpp)

set_property(SOURCE ${AGENT_SOURCES}
 PROPERTY ${GCOV_COMPILE_FLAGS})

include_directories(${LibXML2_INCLUDE_DIRS} ${CPPUNIT_INCLUDE_DIR})

if(WIN32)
  set(WINVER "0x0501" CACHE STRING "Win32 API Target version (see http://msdn.microsoft.com/en-us/library/aa383745%28v=VS.85%29.aspx)")
  add_definitions("/DWINVER=${WINVER}" "/D_WIN32_WINNT=${WINVER}")

  foreach(flag_var  
          CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
          CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
     if(${flag_var} MATCHES "/MD")
        string(REGEX REPLACE "/MD[d]?" "/MTd" ${flag_var} "${${flag_var}}")
     endif(${flag_var} MATCHES "/MD")
  endforeach(flag_var)
endif(WIN32)

add_executable(agent_test ${test_srcs} ${AGENT_SOURCES} ${test_headers})
target_link_libraries(agent_test ${LibXML2_LIBRARIES} ${CPPUNIT_LIBRARY} ${LINUX_LIBRARIES} ${GCOV_LINK_FLAGS})  
set_target_properties(agent_test PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD_RELEASE 1)`

在您对 set_property 的呼叫中,您缺少 属性 姓名。您只有要附加到 属性.

的值

我相信你想要 COMPILE_FLAGS 作为 属性 的名字。

set_property(SOURCE ${AGENT_SOURCES} PROPERTY COMPILE_FLAGS ${GCOV_COMPILE_FLAGS})

除了此更改之外,您可能还想追加,因为您添加的是编译标志,而不是替换默认值。

set_property(SOURCE ${AGENT_SOURCES} APPEND PROPERTY COMPILE_FLAGS ${GCOV_COMPILE_FLAGS})