0

Im very unexperienced with CMAKE. Me and my friend wanted to make a programming language. The basic Lexer and Parser works fine. Now we wanted to include LLVM in the project to compile the language. But im get so much errors and i don't really now what and how to include LLVM.

That's my CMAKE file:

cmake_minimum_required(VERSION 3.22)
project(absturz)

set(CMAKE_CXX_STANDARD 14)
set(LLVM_PATH /usr/local/Cellar/llvm@13/13.0.1)

link_directories(${LLVM_PATH}/bin)
include_directories(${LLVM_PATH}/include)

add_definitions(
        -D__STDC_LIMIT_MACROS
        -D__STDC_CONSTANT_MACROS
)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -std=c++11")

add_executable(absturz main.cpp lang/Lexer.h lang/Token.h lang/src/Token.cpp lang/src/Lexer.cpp lang/Compiler.h lang/src/Compiler.cpp)

target_link_libraries(absturz
        /usr/local/Cellar/llvm@13/13.0.1/lib/libLLVMCore.a
        /usr/local/Cellar/llvm@13/13.0.1/lib/libLLVMSupport.a
        )

when I compile the project I get:

[1/1] Linking CXX executable absturz
FAILED: absturz 
: && /Library/Developer/CommandLineTools/usr/bin/c++ -fno-rtti -std=c++11 -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/absturz.dir/main.cpp.o CMakeFiles/absturz.dir/lang/src/Token.cpp.o CMakeFiles/absturz.dir/lang/src/Lexer.cpp.o CMakeFiles/absturz.dir/lang/src/Compiler.cpp.o -o absturz -L/usr/local/Cellar/llvm@13/13.0.1/bin -Wl,-rpath,/usr/local/Cellar/llvm@13/13.0.1/bin  /usr/local/Cellar/llvm@13/13.0.1/lib/libLLVMCore.a  /usr/local/Cellar/llvm@13/13.0.1/lib/libLLVMSupport.a  /usr/local/Cellar/llvm@13/13.0.1/lib/libLLVMDemangle.a && :
Undefined symbols for architecture x86_64:
  "llvm::EngineBuilder::selectTarget()", referenced from:
      llvm::EngineBuilder::create() in Compiler.cpp.o
  "llvm::EngineBuilder::create(llvm::TargetMachine*)", referenced from:
      llvm::EngineBuilder::create() in Compiler.cpp.o
  "llvm::EngineBuilder::EngineBuilder(std::__1::unique_ptr<llvm::Module, std::__1::default_delete<llvm::Module> >)", referenced from:
      Compiler::compile() in Compiler.cpp.o
  "llvm::EngineBuilder::~EngineBuilder()", referenced from:
      Compiler::compile() in Compiler.cpp.o
  "llvm::dwarf::MacinfoString(unsigned int)", referenced from:
      WriteMDNodeBodyInternal(llvm::raw_ostream&, llvm::MDNode const*, (anonymous namespace)::TypePrinting*, llvm::SlotTracker*, llvm::Module const*) in libLLVMCore.a(AsmWriter.cpp.o)
  "llvm::dwarf::LanguageString(unsigned int)", referenced from:
      WriteMDNodeBodyInternal(llvm::raw_ostream&, llvm::MDNode const*, (anonymous namespace)::TypePrinting*, llvm::SlotTracker*, llvm::Module const*) in libLLVMCore.a(AsmWriter.cpp.o)
  "llvm::dwarf::ConventionString(unsigned int)", referenced from:
      WriteMDNodeBodyInternal(llvm::raw_ostream&, llvm::MDNode const*, (anonymous namespace)::TypePrinting*, llvm::SlotTracker*, llvm::Module const*) in libLLVMCore.a(AsmWriter.cpp.o)
  "llvm::dwarf::AttributeEncodingString(unsigned int)", referenced from:
      writeDIExpression(llvm::raw_ostream&, llvm::DIExpression const*, (anonymous namespace)::TypePrinting*, llvm::SlotTracker*, llvm::Module const*) in libLLVMCore.a(AsmWriter.cpp.o)
      WriteMDNodeBodyInternal(llvm::raw_ostream&, llvm::MDNode const*, (anonymous namespace)::TypePrinting*, llvm::SlotTracker*, llvm::Module const*) in libLLVMCore.a(AsmWriter.cpp.o)
  "llvm::dwarf::OperationEncodingString(unsigned int)", referenced from:
      writeDIExpression(llvm::raw_ostream&, llvm::DIExpression const*, (anonymous namespace)::TypePrinting*, llvm::SlotTracker*, llvm::Module const*) in libLLVMCore.a(AsmWriter.cpp.o)
  "llvm::dwarf::TagString(unsigned int)", referenced from:
      (anonymous namespace)::MDFieldPrinter::printTag(llvm::DINode const*) in libLLVMCore.a(AsmWriter.cpp.o)
  "llvm::remarks::parseFormat(llvm::StringRef)", referenced from:
      llvm::setupLLVMOptimizationRemarks(llvm::LLVMContext&, llvm::StringRef, llvm::StringRef, llvm::StringRef, bool, llvm::Optional<unsigned long long>) in libLLVMCore.a(LLVMRemarkStreamer.cpp.o)
      llvm::setupLLVMOptimizationRemarks(llvm::LLVMContext&, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef, bool, llvm::Optional<unsigned long long>) in libLLVMCore.a(LLVMRemarkStreamer.cpp.o)
  "llvm::remarks::RemarkStreamer::matchesFilter(llvm::StringRef)", referenced from:
      llvm::LLVMRemarkStreamer::emit(llvm::DiagnosticInfoOptimizationBase const&) in libLLVMCore.a(LLVMRemarkStreamer.cpp.o)
  "llvm::remarks::RemarkStreamer::setFilter(llvm::StringRef)", referenced from:
      llvm::setupLLVMOptimizationRemarks(llvm::LLVMContext&, llvm::StringRef, llvm::StringRef, llvm::StringRef, bool, llvm::Optional<unsigned long long>) in libLLVMCore.a(LLVMRemarkStreamer.cpp.o)
      llvm::setupLLVMOptimizationRemarks(llvm::LLVMContext&, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef, bool, llvm::Optional<unsigned long long>) in libLLVMCore.a(LLVMRemarkStreamer.cpp.o)
  "llvm::remarks::RemarkStreamer::RemarkStreamer(std::__1::unique_ptr<llvm::remarks::RemarkSerializer, std::__1::default_delete<llvm::remarks::RemarkSerializer> >, llvm::Optional<llvm::StringRef>)", referenced from:
      llvm::setupLLVMOptimizationRemarks(llvm::LLVMContext&, llvm::StringRef, llvm::StringRef, llvm::StringRef, bool, llvm::Optional<unsigned long long>) in libLLVMCore.a(LLVMRemarkStreamer.cpp.o)
      llvm::setupLLVMOptimizationRemarks(llvm::LLVMContext&, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef, bool, llvm::Optional<unsigned long long>) in libLLVMCore.a(LLVMRemarkStreamer.cpp.o)
  "llvm::remarks::createRemarkSerializer(llvm::remarks::Format, llvm::remarks::SerializerMode, llvm::raw_ostream&)", referenced from:
      llvm::setupLLVMOptimizationRemarks(llvm::LLVMContext&, llvm::StringRef, llvm::StringRef, llvm::StringRef, bool, llvm::Optional<unsigned long long>) in libLLVMCore.a(LLVMRemarkStreamer.cpp.o)
      llvm::setupLLVMOptimizationRemarks(llvm::LLVMContext&, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef, bool, llvm::Optional<unsigned long long>) in libLLVMCore.a(LLVMRemarkStreamer.cpp.o)
  "_del_curterm", referenced from:
      llvm::sys::Process::FileDescriptorHasColors(int) in libLLVMSupport.a(Process.cpp.o)
  "_set_curterm", referenced from:
      llvm::sys::Process::FileDescriptorHasColors(int) in libLLVMSupport.a(Process.cpp.o)
  "_setupterm", referenced from:
      llvm::sys::Process::FileDescriptorHasColors(int) in libLLVMSupport.a(Process.cpp.o)
  "_tigetnum", referenced from:
      llvm::sys::Process::FileDescriptorHasColors(int) in libLLVMSupport.a(Process.cpp.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

OS: MacOS (Intel chip)

IDE: CLion

1

1 Answer 1

1

Linking directly to a library path in CMake is maybe the single strongest code-smell there is. It's the surströmming of CMake code. Don't open that can!

Use LLVM's own CMake support:

cmake_minimum_required(VERSION 3.22)
project(absturz)

set(CMAKE_CXX_STANDARD 14)

find_package(LLVM 13 REQUIRED)
llvm_map_components_to_libnames(
  LLVM_LIBRARIES
  Core
  Support
)

add_executable(absturz main.cpp lang/Lexer.h lang/Token.h lang/src/Token.cpp lang/src/Lexer.cpp lang/Compiler.h lang/src/Compiler.cpp)
target_include_directories(absturz PRIVATE ${LLVM_INCLUDE_DIRS})
target_compile_definitions(absturz PRIVATE ${LLVM_DEFINITIONS})
target_link_libraries(absturz PRIVATE ${LLVM_LIBRARIES})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.