我一直在努力了解 CMake,最近我偶然发现了 CLion ide。到目前为止,它有点帮助。 在尝试将 google test 与 CLion 集成时,我克隆了 this github repo.
但是,我不明白保存测试方法的 .cpp 文件如何在错误的相对路径中“看到”库的头文件!
Calendar_check.cpp:
//
// Created by AK on 13/10/15.
//
#include "gtest/gtest.h"
#include "GregorianCalendar.h"
#include "JulianCalendar.h"
///Code that runs tests, not important for this question
看项目是怎么设置的,calendar_check.cpp
文件的#include
语句是不是应该这样读
#include "../../calendars/GregorianCalendar.h"
#include "../../calendars/JulianCalendar.h"
而不是?
我知道静态库没有头文件,所以我不明白 calendar_test.cpp
文件如何知道这些头文件在哪里使用它们使用的路径。
这是 CMakeLists.txt 文件:
根:
cmake_minimum_required(VERSION 3.1)
project(Calendar)
#add_definitions(-std=c++11 --target=x86_64-linux-gnu)
#set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(calendar_run ${SOURCE_FILES})
include_directories(calendars)
add_subdirectory(calendars)
add_subdirectory(calendars_tests)
target_link_libraries(calendar_run calendars)
根/日历:
project(calendars)
set(HEADER_FILES
calendar_defs.h
General.h
GregorianCalendar.h
JulianCalendar.h
IslamicCalendar.h
HebrewCalendar.h
)
set(SOURCE_FILES
calendar_defs.cpp
Calendar.cpp
General.cpp
GregorianCalendar.cpp
JulianCalendar.cpp
IslamicCalendar.cpp
HebrewCalendar.cpp
)
add_library(calendars STATIC ${SOURCE_FILES} ${HEADER_FILES})
root/calendar_tests:
project(calendars_tests)
add_subdirectory(lib/gtest-1.7.0)
add_subdirectory(basic_tests)
root/calendar_tests/basic_tests:
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
add_executable(runBasicCalendarTests
basic_check.cpp
calendar_check.cpp)
target_link_libraries(runBasicCalendarTests gtest gtest_main)
target_link_libraries(runBasicCalendarTests calendars)
最佳答案
你必须阅读这个answer来自这个问题:What is the difference between #include < filename> and #include “filename”?
特别是,当 piCookie
说:
A preprocessing directive of the form
#include "q-char-sequence" new-line
causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read
#include <h-char-sequence> new-line
with the identical contained sequence (including > characters, if any) from the original directive.
阅读完后请注意 CMakeLists.txt 中的这一行:
include_directories(calendars)
您要求 cmake
包含 calendars
目录以包含搜索路径。
关于c++ - 怎么才能看到这个header?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38317413/