【问题标题】:How to compile the two set of source file separately using cmake?如何使用cmake分别编译这两组源文件?
【发布时间】:2015-01-13 12:47:33
【问题描述】:

我正在尝试使用来自 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)`

【问题讨论】:

  • @EtanReisner:这不是重复的。您提供的链接说它有两个不同的目标,因此他在不同的目标上附加了不同的标志。我只有一个目标。
  • 查看关于将标志附加到源文件的评论。
  • 嗨@EtanReisner,是的,评论是相关的。但不工作。我需要一个一个添加源文件吗?我需要从 test_src 中删除这些文件吗?
  • 在帖子中包含您尝试过的内容以及它如何不起作用可能对真正了解 cmake 的人有所帮助。如果没有别的,它可以让不(像我)不喜欢的人开始寻找你可能错过/做错的地方。

标签: c++ makefile cmake


【解决方案1】:

在调用 set_property 时,您缺少 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})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2015-10-21
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 2016-03-27
    相关资源
    最近更新 更多