为什么CMake忽略源文件? [英] Why does CMake neglects source files?

查看:241
本文介绍了为什么CMake忽略源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我编写的 CMakeLists.txt 文件仅为头文件和测试脚本创建了覆盖率统计信息,而没有为源文件创建覆盖率统计信息。但是,我想了解源文件的覆盖率信息。我在做什么错了?

The CMakeLists.txt file I wrote, unfortunately, create coverage statistics solely for header files and test scripts, but not for the source files. I would, however, like to heave coverage information for the source files. What am I doing wrong?

例如,以下是头文件: class.h

As an example, here is the header file: class.h

#include <string>
#include <vector>
#include <iostream>
class StrVec{
    public:
    StrVec(const std::string&);
    void print() {std::cout << vec[0] << std::endl;}

    private:
    std::vector<std::string> vec;
};

源文件为 class.cpp

#include "class.h"

StrVec::StrVec(const std::string& s): vec({s}) {}


测试文件为 main.cpp

#include "class.h"

int main() {
    std::string s("test");
    StrVec str_vec(s);
    str_vec.print();
}

CmakeLists.txt 我写的文件是:

cmake_minimum_required (VERSION 3.5)

project (StrVec)
set(LIBRARY_TARGET_NAME ${PROJECT_NAME})
SET (CMAKE_CXX_COMPILER             "/usr/bin/g++")

set(${LIBRARY_TARGET_NAME}_SRC
    class.cpp
)

set(${LIBRARY_TARGET_NAME}_HDR
    class.h
)

add_library(${LIBRARY_TARGET_NAME} SHARED ${${LIBRARY_TARGET_NAME}_SRC})
add_compile_options(--coverage -O0)

add_executable(main main.cpp)
target_link_libraries(main StrVec --coverage)

当我编译代码并运行程序时, lcov 仅找到 main.cpp.gcda 而不是 class.cpp 。因此,覆盖率统计信息仅包括头文件 class.h main.cpp ,但不包括 class.cpp 。如何修改 CMakeList.txt 以获得 class.cpp 的覆盖率统计信息?

When I compile the code and run the program, lcov only finds main.cpp.gcda and not class.cpp. The coverage statistics thus include only the header file class.h and main.cpp but not class.cpp. How can I modify CMakeList.txt to obtain coverage statistics for class.cpp?

我阅读了几个 cmake gcov 文档,有一种印象,我特别需要请求 _SRC 文件的承保范围。但是,我不知道该怎么做。有人可以指出我可以做什么吗?

I read several cmake and gcov documents and I had the impression that I specifically need to request coverage for the _SRC files. However, I could not figure out how to do that. Can somebody kindly point out what I can do?

推荐答案

感谢@squareskitties的帮助,我设法解决了这个问题。我只是没有通过所有必需的编译选项。以下 CMakeLists.txt 起作用:

Thanks for @squareskitties help, I managed to solve the problem. I just didn't pass all required compile options. The following CMakeLists.txt worked:

c++
cmake_minimum_required (VERSION 3.5)

project (StrVec)
set(LIBRARY_TARGET_NAME ${PROJECT_NAME})
SET (CMAKE_CXX_COMPILER             "/usr/bin/g++")

set(${LIBRARY_TARGET_NAME}_SRC
    class.cpp
)

set(${LIBRARY_TARGET_NAME}_HDR
    class.h
)

add_library(${LIBRARY_TARGET_NAME} SHARED ${${LIBRARY_TARGET_NAME}_SRC})
SET(CMAKE_CXX_FLAGS "-g -O0 --coverage -fprofile-arcs -ftest-coverage")
add_executable(main main.cpp)
target_link_libraries(main StrVec --coverage)

这篇关于为什么CMake忽略源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆