我正在编写 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)
然后我使用
从命令行在构建目录中构建它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"解决了几个错误,但引入了一个新错误:"implicit instantiation of undefined template"在迈出一步。
引用了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)
关于c++ - cmake:添加要搜索头文件的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52999277/