CMake:找不到头文件 [英] CMake: can't find header files

查看:1213
本文介绍了CMake:找不到头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主目录,其中包含以下子目录:
A,B,C,D,测试。

I have a directory main, which has the following subdirectories: A, B, C, D, Test.

在测试中,我有一个CMakeLists文件具有以下内容:

In Test, I have a CMakeLists file with the following content:

cmake_minimum_required(VERSION 2.8)
enable_testing()
set(TEST_EXE_NAME test)
add_executable(${TEST_EXE_NAME} test.cpp)
add_test(NAME "testDatabase" COMMAND ${TEST_EXE_NAME})
target_include_directories(Test PUBLIC ./)
target_include_directories(Test A B C D)
target_link_libraries(Test A B C D)

在测试中,我有一个可执行文件,其中包括

In Test, I have an executable which #includes several header files from A, B, C, and D.

但是,在执行make之后,我得到了消息,即cmake无法从A,B找到这些头文件,

However, after doing make, I get the message that cmake cannot find these header files from A, B, C, and D.

我该如何解决?

推荐答案

根据您的问题,很难确切地知道出了什么问题。这就是为什么我要描述如何解决整个问题。

From your question, it is hard to see exactly what is going wrong. This is why I am going to describe how I would tackle the whole problem.

在这里必须有一个 CMakeLists.txt 才能在测试中将CMake目标用于AD。看起来像这样:

It is necessary to have a CMakeLists.txt here to be able to use the CMake targets for A-D in Test. It would look like this:

cmake_minimum_required(VERSION 2.8)
enable_testing()
add_subdirectory(A)
add_subdirectory(B)
add_subdirectory(C)
add_subdirectory(D)
add_subdirectory(Test)

请注意,我们在此处调用 enable_testing()。这样一来,您以后就可以直接在根构建目录中调用进行测试

Note that we call enable_testing() here. This will enable you to call make test in your root build directory directly later on.

在那里,您创建了AD库。
例如,对于A,您将这样写:

There, you create libraries for A-D. For A, for instance, you would write:

add_library(A STATIC [... source files for A ...]) # or SHARED instead of STATIC
target_include_directories(A PUBLIC ./)

请注意,通过使用 target_include_directories ,您可以告诉CMake稍后自动包括库的目录。

Note that by using target_include_directories, you tell CMake to include the directories for the libraries automatically later on. This will be useful below.

现在,这变得非常容易:

Now this becomes quite easy:

set(TEST_EXE_NAME test)
add_executable(${TEST_EXE_NAME} test.cpp)
target_include_directories(${TEST_EXE_NAME} PUBLIC ./)
target_link_libraries(${TEST_EXE_NAME} A B C D)
add_test(NAME "testDatabase" COMMAND ${TEST_EXE_NAME})

请注意,这里不需要为AD设置包含目录,因为CMake早就知道需要它们了!

Note that there is no need to set the include directories for A-D here, since CMake already knows from before that they are needed!

这篇关于CMake:找不到头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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