首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法读取文件的Clang CFE编译

无法读取文件的Clang CFE编译
EN

Stack Overflow用户
提问于 2022-07-05 10:37:02
回答 1查看 28关注 0票数 1

我试图用以下代码使用Compiler编译测试文件:

代码语言:javascript
复制
#include <string>
#include <vector>
#include <memory>
#include <iostream>
#include <llvm/Support/Host.h>
#include <llvm/Support/Program.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/VirtualFileSystem.h>
#include "clang/Basic/LLVM.h"
// #include <clang/Driver/ToolChain.h>
#include <clang/Driver/Driver.h>
#include <clang/Basic/Diagnostic.h>
#include <clang/Basic/DiagnosticIDs.h>
#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Driver/Compilation.h>

using namespace clang::driver;
using namespace clang;
std::string pwd = "/home/arch/Documents/CFE/Examples/Compile/";
std::string target_executable = pwd + "target";

int main()
{
  llvm::IntrusiveRefCntPtr<DiagnosticsEngine> DE(clang::CompilerInstance::createDiagnostics(new DiagnosticOptions));
  llvm::ErrorOr<std::string> clangPath = llvm::sys::findProgramByName("clang++");
  Driver D(StringRef(clangPath.get().c_str()), llvm::sys::getDefaultTargetTriple(), *DE, "clang LLVM compiler", InMemoryFileSystem);
  // D.setCheckInputsExist(false);
  std::vector<const char *> args;
  args.push_back(clangPath.get().c_str());
  args.push_back((target_executable + ".cpp").c_str());
  args.push_back("-cl-std=clc++2021");
  args.push_back(("-o " + target_executable + ".spv").c_str());
  ArrayRef<const char *> compileArgs(args);
  std::unique_ptr<Compilation> C(D.BuildCompilation(compileArgs));
  assert(C > 0);
  bool CallbackHasCalled = false;
  C->setPostCallback(
      [&](const Command &Cmd, int Ret)
      { std::cout << "postCallback return value: " << Ret << std::endl; 
      CallbackHasCalled = true; });

  const JobList &Jobs = C->getJobs();
  auto &CmdCompile = Jobs.getJobs().front();
  const Command *FailingCmd = nullptr;
  assert(C->ExecuteCommand(*CmdCompile, FailingCmd));
  assert(FailingCmd);
}

使用以下CMakeLists.txt:

代码语言:javascript
复制
cmake_minimum_required(VERSION 3.23)
project("CLANG_CFE")
find_package(Clang REQUIRED)
target_precompile_headers(clangBasic PUBLIC)
execute_process(COMMAND clang-config --libs OUTPUT_VARIABLE LLVM_LIBRARIES)
execute_process(COMMAND clang-config --cxx-flags OUTPUT_VARIABLE LLVM_CXX_FLAGS)
add_executable(compile_example main.cpp)
target_compile_options(compile_example PRIVATE ${LLVM_CXX_FLAGS})
set(CLANG_LIBS clangBasic clangDriver clangFrontend)
target_link_libraries(compile_example PRIVATE  ${CLANG_LIBS} LLVMSupport)

代码编译得很好,但无法读取target_executable,从而导致以下错误:

代码语言:javascript
复制
error: no such file or directory: 'P�ZUU'

这是读写权限错误吗?

EN

回答 1

Stack Overflow用户

发布于 2022-07-05 15:27:33

结果是绝对路径的使用出现了问题,如果文件路径是以Driver::DiagnoseInputExistence开头的,则方法"/"会自动拒绝它。

使用相对文件解决了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72867995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档