Why does CMake neglects source files?

萝らか妹 提交于 2019-12-24 07:56:23

问题


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?

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;
};

The source file is class.cpp:

#include "class.h"

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


And the "test" file is main.cpp:

#include "class.h"

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

The CmakeLists.txt file I wrote is:

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)

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?

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?


回答1:


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)


来源:https://stackoverflow.com/questions/57330684/why-does-cmake-neglects-source-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!