gpt4 book ai didi

c++ - 使用 g++ 的 clang/llvm 链接失败

转载 作者:行者123 更新时间:2023-11-30 04:30:38 25 4
gpt4 key购买 nike

我正在尝试编译这个 .ccp 文件。我从https://github.com/loarabia/Clang-tutorial得到的.

// This code is licensed under the New BSD license.
// See LICENSE.txt for more details.
#include <iostream>

#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Host.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"

#include "clang/Frontend/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"

#include "clang/Basic/LangOptions.h"
#include "clang/Basic/FileSystemOptions.h"

#include "clang/Basic/SourceManager.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Basic/FileManager.h"

#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"

#include "clang/Lex/Preprocessor.h"
#include "clang/Frontend/CompilerInstance.h"


int main()
{
clang::DiagnosticOptions diagnosticOptions;
clang::TextDiagnosticPrinter *pTextDiagnosticPrinter =
new clang::TextDiagnosticPrinter(
llvm::outs(),
diagnosticOptions);
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs;
//clang::DiagnosticIDs diagIDs;

clang::DiagnosticsEngine *pDiagnosticsEngine =
new clang::DiagnosticsEngine(pDiagIDs, pTextDiagnosticPrinter);

clang::LangOptions languageOptions;
clang::FileSystemOptions fileSystemOptions;
clang::FileManager fileManager(fileSystemOptions);
clang::SourceManager sourceManager(
*pDiagnosticsEngine,
fileManager);
clang::HeaderSearch headerSearch(fileManager, *pDiagnosticsEngine);

clang::TargetOptions targetOptions;
targetOptions.Triple = llvm::sys::getDefaultTargetTriple();

clang::TargetInfo *pTargetInfo =
clang::TargetInfo::CreateTargetInfo(
*pDiagnosticsEngine,
targetOptions);
clang::CompilerInstance compInst;

clang::Preprocessor preprocessor(
*pDiagnosticsEngine,
languageOptions,
pTargetInfo,
sourceManager,
headerSearch,
compInst);

return 0;
}

我像这样使用 g++ 编译行:

  g++ tutorial1.cpp -g -fno-rtti `llvm-config --cxxflags --ldflags --libs` -lclangBasic
-lclangLex -lclangDriver -lclangFrontend -lclangParse -lclangAST -lclangSema
-lclangAnalysis -I ../llvm/tools/clang/include/
-I/Users/scher/Documents/Projects/task/C/last_clang/llvm/include
-I/Users/scher/Documents/Projects/task/C/last_clang/build/include
-I/Users/scher/Documents/Projects/task/C/last_clang/build/tools/clang/include

但它失败并出现以下报告:

Undefined symbols for architecture x86_64:
"clang::serialization::ModuleManager::lookup(llvm::StringRef)", referenced from:
clang::ASTUnit::TranslateStoredDiagnostics(clang::ASTReader*, llvm::StringRef, clang::SourceManager&, llvm::SmallVectorImpl<clang::StoredDiagnostic> const&, llvm::SmallVectorImpl<clang::StoredDiagnostic>&)in libclangFrontend.a(ASTUnit.o)
"clang::ASTWriter::ASTWriter(llvm::BitstreamWriter&)", referenced from:
clang::ASTUnit::serialize(llvm::raw_ostream&) in libclangFrontend.a(ASTUnit.o)
"clang::ASTWriter::WriteAST(clang::Sema&, clang::MemorizeStatCalls*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, clang::Module*, llvm::StringRef)", referenced from:
clang::ASTUnit::serialize(llvm::raw_ostream&) in libclangFrontend.a(ASTUnit.o)
"clang::ASTWriter::~ASTWriter()", referenced from:
clang::ASTUnit::serialize(llvm::raw_ostream&) in libclangFrontend.a(ASTUnit.o)
"clang::PCHGenerator::PCHGenerator(clang::Preprocessor const&, llvm::StringRef, clang::Module*, llvm::StringRef, llvm::raw_ostream*)", referenced from:
(anonymous namespace)::PrecompilePreambleConsumer::PrecompilePreambleConsumer(clang::ASTUnit&, clang::Preprocessor const&, llvm::StringRef, llvm::raw_ostream*)in libclangFrontend.a(ASTUnit.o)
"clang::PCHGenerator::HandleTranslationUnit(clang::ASTContext&)", referenced from:
(anonymous namespace)::PrecompilePreambleConsumer::HandleTranslationUnit(clang::ASTContext&)in libclangFrontend.a(ASTUnit.o)
"clang::ASTWriter::getDeclID(clang::Decl const*)", referenced from:
(anonymous namespace)::PrecompilePreambleConsumer::HandleTranslationUnit(clang::ASTContext&)in libclangFrontend.a(ASTUnit.o)
"clang::PCHGenerator::~PCHGenerator()", referenced from:
(anonymous namespace)::PrecompilePreambleConsumer::~PrecompilePreambleConsumer()in libclangFrontend.a(ASTUnit.o)
"vtable for clang::ASTReaderListener", referenced from:
clang::ASTReaderListener::ASTReaderListener()in libclangFrontend.a(ASTUnit.o)
"clang::ASTReaderListener::~ASTReaderListener()", referenced from:
(anonymous namespace)::ASTInfoCollector::~ASTInfoCollector()in libclangFrontend.a(ASTUnit.o)
"clang::PCHGenerator::GetASTMutationListener()", referenced from:
vtable for (anonymous namespace)::PrecompilePreambleConsumerin libclangFrontend.a(ASTUnit.o)
"clang::PCHGenerator::GetASTDeserializationListener()", referenced from:
vtable for (anonymous namespace)::PrecompilePreambleConsumerin libclangFrontend.a(ASTUnit.o)
"clang::serialization::ModuleManager::addInMemoryBuffer(llvm::StringRef, llvm::MemoryBuffer*)", referenced from:
clang::ASTReader::addInMemoryBuffer(llvm::StringRef&, llvm::MemoryBuffer*) in libclangFrontend.a(ChainedIncludesSource.o)
"clang::ASTReader::ASTReader(clang::Preprocessor&, clang::ASTContext&, llvm::StringRef, bool, bool)", referenced from:
clang::CompilerInstance::createPCHExternalASTSource(llvm::StringRef, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, bool, clang::Preprocessor&, clang::ASTContext&, void*, bool)in libclangFrontend.a(CompilerInstance.o)
clang::CompilerInstance::loadModule(clang::SourceLocation, llvm::ArrayRef<std::pair<clang::IdentifierInfo*, clang::SourceLocation> >, clang::Module::NameVisibilityKind, bool)in libclangFrontend.a(CompilerInstance.o)
clang::ASTUnit::LoadFromASTFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine>, clang::FileSystemOptions const&, bool, std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, llvm::PointerUnion<char const*, llvm::MemoryBuffer const*> >*, unsigned int, bool)in libclangFrontend.a(ASTUnit.o)
createASTReader(clang::CompilerInstance&, llvm::StringRef, llvm::SmallVector<llvm::MemoryBuffer*, 4u>&, llvm::SmallVector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 4u>&, clang::ASTDeserializationListener*)in libclangFrontend.a(ChainedIncludesSource.o)
"clang::ASTReader::setDeserializationListener(clang::ASTDeserializationListener*)", referenced from:
clang::CompilerInstance::createPCHExternalASTSource(llvm::StringRef, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, bool, clang::Preprocessor&, clang::ASTContext&, void*, bool)in libclangFrontend.a(CompilerInstance.o)
clang::CompilerInstance::loadModule(clang::SourceLocation, llvm::ArrayRef<std::pair<clang::IdentifierInfo*, clang::SourceLocation> >, clang::Module::NameVisibilityKind, bool)in libclangFrontend.a(CompilerInstance.o)
createASTReader(clang::CompilerInstance&, llvm::StringRef, llvm::SmallVector<llvm::MemoryBuffer*, 4u>&, llvm::SmallVector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 4u>&, clang::ASTDeserializationListener*)in libclangFrontend.a(ChainedIncludesSource.o)
"clang::ASTReader::ReadAST(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, clang::serialization::ModuleKind)", referenced from:
clang::CompilerInstance::createPCHExternalASTSource(llvm::StringRef, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, bool, clang::Preprocessor&, clang::ASTContext&, void*, bool)in libclangFrontend.a(CompilerInstance.o)
clang::CompilerInstance::loadModule(clang::SourceLocation, llvm::ArrayRef<std::pair<clang::IdentifierInfo*, clang::SourceLocation> >, clang::Module::NameVisibilityKind, bool)in libclangFrontend.a(CompilerInstance.o)
clang::ASTUnit::LoadFromASTFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine>, clang::FileSystemOptions const&, bool, std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, llvm::PointerUnion<char const*, llvm::MemoryBuffer const*> >*, unsigned int, bool)in libclangFrontend.a(ASTUnit.o)
createASTReader(clang::CompilerInstance&, llvm::StringRef, llvm::SmallVector<llvm::MemoryBuffer*, 4u>&, llvm::SmallVector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 4u>&, clang::ASTDeserializationListener*)in libclangFrontend.a(ChainedIncludesSource.o)
"clang::ASTReader::makeModuleVisible(clang::Module*, clang::Module::NameVisibilityKind)", referenced from:
clang::CompilerInstance::loadModule(clang::SourceLocation, llvm::ArrayRef<std::pair<clang::IdentifierInfo*, clang::SourceLocation> >, clang::Module::NameVisibilityKind, bool)in libclangFrontend.a(CompilerInstance.o)
"clang::ASTReader::getOriginalSourceFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, clang::FileManager&, clang::DiagnosticsEngine&)", referenced from:
ParsePreprocessorArgs(clang::PreprocessorOptions&, clang::driver::ArgList&, clang::FileManager&, clang::DiagnosticsEngine&)in libclangFrontend.a(CompilerInvocation.o)
"clang::ASTDeserializationListener::~ASTDeserializationListener()", referenced from:
(anonymous namespace)::DelegatingDeserializationListener::~DelegatingDeserializationListener()in libclangFrontend.a(FrontendAction.o)
clang::MultiplexASTDeserializationListener::~MultiplexASTDeserializationListener()in libclangFrontend.a(MultiplexConsumer.o)
"vtable for clang::ASTDeserializationListener", referenced from:
clang::ASTDeserializationListener::ASTDeserializationListener()in libclangFrontend.a(FrontendAction.o)
clang::ASTDeserializationListener::ASTDeserializationListener()in libclangFrontend.a(MultiplexConsumer.o)
"clang::PCHGenerator::PCHGenerator(clang::Preprocessor const&, llvm::StringRef, clang::Module*, llvm::StringRef, llvm::raw_ostream*)", referenced from:
clang::GeneratePCHAction::CreateASTConsumer(clang::CompilerInstance&, llvm::StringRef) in libclangFrontend.a(FrontendActions.o)
clang::GenerateModuleAction::CreateASTConsumer(clang::CompilerInstance&, llvm::StringRef) in libclangFrontend.a(FrontendActions.o)
clang::ChainedIncludesSource::create(clang::CompilerInstance&) in libclangFrontend.a(ChainedIncludesSource.o)
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

我想补充一点,当您添加此 clang::CompilerInstance compInst; 代码行时,它就会发生。如果没有这一行,它会正常编译。

我还使用 clang/llvm 3.0,它是使用本教程构建的 http://clang.llvm.org/get_started.html

有什么建议为什么会失败吗?

最佳答案

您可能没有以正确的顺序列出正确的库。我不知道正确的列表是什么,但至少你缺少 -lclangSerialization

关于c++ - 使用 g++ 的 clang/llvm 链接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8607432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com