【问题标题】:cmake: add directory to be searched for header filescmake:添加要搜索头文件的目录
【发布时间】:2018-10-25 23:04:57
【问题描述】:

我正在编写 c++ 代码来在我编写的 arduino 项目上运行测试。为了运行它,我需要它包含几个库的模型(原始库在 c++ 中不起作用)。模型在我的 src 目录中,以及在 main.cpp 中的测试。依赖于模型的文件位于 src 的父目录中,我无法修改它们以引用 src,而不会破坏它们在上传到 arduino 时的运行能力。我也无法将模型或 main.cpp 添加到父目录,因为这会干扰 arduino 代码的操作。

所以,我需要将带有模型的子目录添加到编译父目录中的文件时搜索的目录中。

目录大纲:

parent
    forTest.h
    forTest.cpp
    src
        parson.h
        parson.c
        String.h
        String.cpp
        main.cpp
        CMakeLists.txt
    build

在这种情况下,main.cpp 有“#include ../forTest.cpp”,forTest.cpp 有“#include parson.h”和“#include String.h”

我目前在 CMakeLists.txt 中运行以下代码:

cmake_minimum_required (VERSION 2.6)
project(makeTest)

include_directories(../ )

set (makeTest_VERSION_MAJOR 1)
set (makeTest_VERSION_MINOR 0)

add_executable(makeTest main.cpp ../forTest.cpp String.cpp parson.c)

然后我使用

从命令行在 build 目录中构建它
cmake -G "Unix Makefiles" ../src
make

cmake命令解析成功,但是make命令在构建forTest.cpp时遇到“fatal error: parson.h not found”

我该如何解决这个问题?

编辑:如果这有一个明显的答案,我很抱歉,我对 Cmake 很陌生

编辑第二个:使用 Gergely Nyiri 建议的更改并将 #include 更改为 #include "/usr/include/string.h" 解决了几个错误,但在迈出一步。

在string.h mockup文件中引用如下代码:

#ifndef STRING_H
#define STRING_H

#include "/usr/include/string.h"
using namespace std;

class String {
private:
    std::string thisString;
    char chars[];
...
#endif

返回错误:

In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:61:
/Users/douglas/Desktop/parts/makeHolder/testMake/src/string.h:9:14: error: 
implicit instantiation of undefined template 'std::__1::basic_string<char,
  std::__1::char_traits<char>, std::__1::allocator<char> >'
    std::string thisString; 

接下来是:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iosfwd:193:32: note: 
    template is declared here
    class _LIBCPP_TEMPLATE_VIS basic_string;

【问题讨论】:

    标签: c++ cmake arduino


    【解决方案1】:

    首先,我建议将 CMakeLists.txt 放在您的源代码根目录中。那么:

    cmake_minimum_required (VERSION 2.6)
    project(makeTest)
    
    include_directories(src)
    
    set (makeTest_VERSION_MAJOR 1)
    set (makeTest_VERSION_MINOR 0)
    
    add_executable(makeTest src/main.cpp forTest.cpp src/String.cpp src/parson.c)
    

    配置和构建:

    cd build
    cmake ../
    make
    

    如果您使用 target_include_directories 而不是 include_directories,那就更好了:

    ..
    add_executable(makeTest src/main.cpp forTest.cpp src/String.cpp src/parson.c)
    target_include_directories(makeTest PRIVATE src)
    

    【讨论】:

    • 我试过了,它确实解决了我在查找文件时遇到的问题。不幸的是,它还列出了大约 20 个错误,例如:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:82:9: error: no member named 'strchr'在全局命名空间中使用 ::strchr;据我所知,听起来 cmake 包含来自内核而不是标准的 string.h。这正常吗?
    • 如何包含 string.h?在 cpp 你应该包括 并使用 std::strchr ...
    • 我只是在文件顶部使用#include using namespace std。我实际上并没有在我的文件中的任何地方使用像 strchr 这样的缺失成员,这就是为什么我认为 cmake 包含来自错误位置的文件的原因,它留下了很多缺少它们定义的文件的函数跨度>
    • 我能够通过将#include 更改为#include "/usr/include/string.h" 来解决这20 个错误。这会导致一个新错误:未定义模板的隐式实例化 'std::1::basic_string, std::__1::allocator 这指的是其中的一行我声明“std::string thisString;”它说模板在这里声明“_LIBCPP_TEMPLATE_VIS basic_string”
    • 接受这个作为答案,因为它解决了最初的问题,我认为出现的其他问题是单独的问题。
    猜你喜欢
    • 2018-04-08
    • 2013-07-13
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多