如何在 cpp 中安装头文件

How to install header files in cpp

本文关键字:安装 文件 cpp      更新时间:2023-10-16

这是一个类似于如何安装clang头文件的问题? 但我无法发表评论或提问,因为它需要50+声誉。请原谅重复。

我已经在我的MacOS上安装了clang(在/usr/bin/clang中),我认为默认情况下安装在Mac上,但是,当我尝试在cpp文件中包含clang头文件时,它说找不到它们

/Users/jzhu/go/src/github.com/codelingo/sandbox/test/cpp/main.cpp:7:10: fatal error: 'clang/AST/ASTConsumer.h' file not found #include <clang/AST/ASTConsumer.h> ^ 1 error generated

问题:当 clang 已经在 MacOS 系统上安装和构建时,是否有必要(并且可能,如果是,如何)安装头文件(或者 clang 本身是否需要在安装所有所需的开发工具包及其头文件的同时重新安装)?

#include <clang/AST/ASTConsumer.h>
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"

我已经使用了@numurat在如何安装clang头文件中建议的"<>",但它并不能解决问题。

clang 包可以在/Users/username/ccompiler/llvm/tools/clang/include/clang 中找到。

$ pwd
/Users/username/ccompiler/llvm/tools/clang/include/clang
$ ls 
ARCMigrate      Basic           Driver          FrontendTool        Rewrite         Tooling
AST         CMakeLists.txt      Edit            Index           Sema            module.modulemap
ASTMatchers     CodeGen         Format          Lex         Serialization
Analysis        Config          Frontend        Parse           StaticAnalyzer

我也尝试制作这个 clang 文件夹的副本并包含在我的项目中,但是当 clang/AST/RecursiveASTVisitor.h 有一行 #include"clang/AST/Attr.h"时,它最终会出现类似的错误

我试图遵循这个例子 http://clang.llvm.org/docs/RAVFrontendAction.html

在我的项目文件夹中,我有一个 main.cpp与上面链接中的教程内容相同,还有一个 CMakeList.txt它是由 CLION 自动创建的

cmake_minimum_required(VERSION 3.8)
project(cpp)
set(CMAKE_CXX_STANDARD 17)
set(SOURCE_FILES main.cpp)
add_executable(cpp ${SOURCE_FILES})
include_directories(${LLVM_INCLUDE_DIRS})

PS:我按照 https://clang.llvm.org/get_started.html 安装了叮当声。

$ clang --version
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

您似乎在某个时候自己安装了编译器,并且无法找到库存头文件。 使用-I标志告诉它在哪里可以找到它们:

-I/Users/username/ccompiler/llvm/tools/clang/include

https://clang.llvm.org/docs/CommandGuide/clang.html