繁体   English   中英

cmake:添加要搜索头文件的目录

[英]cmake: add directory to be searched for header files

我正在编写C ++代码以在我编写的arduino项目上运行测试。 为了运行它,我需要它包含几个库的模型(原始库在c ++中不起作用)。 样机以及main.cpp中的测试都在我的src目录中。 依赖模型的文件位于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命令成功解析,但是在构建forTest.cpp时,make命令会遇到“严重错误:找不到parson.h”

我该如何解决?

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

编辑第二个:使用Gergely Nyiri建议的更改并将#include更改为#include“ /usr/include/string.h”解决了一些错误,但是引入了一个新错误:在make步骤中出现了“隐式实例化未定义模板”。

它在string.h模型文件中引用以下代码:

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

首先,我建议将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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM