Clang CFE编译无法读取文件

发布于 2025-02-13 01:17:46 字数 2786 浏览 1 评论 0原文

我正在尝试使用以下代码使用Clang编译器前端编译测试文件:

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

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,导致以下错误

error: no such file or directory: 'P�ZUU'

:这是读/写许可吗?

I'm trying to compile test files using the Clang Compiler FrontEnd with the following code:

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

With the following CMakeLists.txt:

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)

The code compiles fine but fails to read the target_executable, resulting in the following error:

error: no such file or directory: 'P�ZUU'

Is this a read/write permission-error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟若柳尘 2025-02-20 01:17:46

事实证明,绝对路径的使用是一个问题,方法驱动程序:: DiagnoseInputeXistence如果文件路径以>>>>>“/”/“”而自动拒绝。

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

It turned out to be a problem with the usage of absolute paths, the method Driver::DiagnoseInputExistence automatically rejects the file path if it starts with "/".

Using a relative filepath solved this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文