我正在编写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 命令解析成功,但在构建 for Test 时,make 命令遇到"致命错误:找不到 parson.h".cpp
我该如何解决这个问题?
编辑:如果这有一个明显的答案,我很抱歉,我对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)