0

I am learning how to write a compiler, and I used llvm for code generating. But it seems that some error occurred while linking.

The C++ compiler is the default one of XCode.

LLVM version: LLVM 16.0.0git

cmake version: 3.24.0

My CMakeList.txt(Cmake configure succeeded):

cmake_minimum_required(VERSION 3.21)
project(Seserot)

set(CMAKE_CXX_STANDARD 23)

find_package(LLVM REQUIRED CONFIG)

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

# Set your project compile flags.
# E.g. if using the C++ header files
# you will need to enable C++11 support
# for your compiler.

include_directories(${LLVM_INCLUDE_DIRS})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})

# Find the libraries that correspond to the LLVM components
# that we wish to use
llvm_map_components_to_libnames(llvm_libs support core irreader)

# Link against LLVM libraries

add_executable(Seserot
        src/main.cpp
        src/Lexer.cpp
        src/ErrorTable.cpp
        src/CompilerError.cpp
        src/Parser.cpp
        src/BuildIn.cpp
        src/AbstractSyntaxTreeNode.cpp
        src/ByteCodeWriter.cpp
        src/Symbol.cpp)

Compiler errors:

====================[ Build | Seserot | Debug ]=================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/zhaoliyan/CLionProjects/Seserot-gen0/cmake-build-debug --target Seserot -j 6
[1/1] Linking CXX executable Seserot
FAILED: Seserot 
: && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/Seserot.dir/src/main.cpp.o CMakeFiles/Seserot.dir/src/Lexer.cpp.o CMakeFiles/Seserot.dir/src/ErrorTable.cpp.o CMakeFiles/Seserot.dir/src/CompilerError.cpp.o CMakeFiles/Seserot.dir/src/Parser.cpp.o CMakeFiles/Seserot.dir/src/BuildIn.cpp.o CMakeFiles/Seserot.dir/src/AbstractSyntaxTreeNode.cpp.o CMakeFiles/Seserot.dir/src/ByteCodeWriter.cpp.o CMakeFiles/Seserot.dir/src/Symbol.cpp.o -o Seserot   && :
Undefined symbols for architecture arm64:
  "llvm::LLVMContext::LLVMContext()", referenced from:
      _main in main.cpp.o
  "llvm::LLVMContext::~LLVMContext()", referenced from:
      _main in main.cpp.o
  "llvm::IRBuilderFolder::~IRBuilderFolder()", referenced from:
      llvm::ConstantFolder::~ConstantFolder() in main.cpp.o
  "llvm::IRBuilderDefaultInserter::~IRBuilderDefaultInserter()", referenced from:
      llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter>::~IRBuilder() in main.cpp.o
  "llvm::Module::~Module()", referenced from:
      std::__1::default_delete<llvm::Module>::operator()(llvm::Module*) const in main.cpp.o
  "vtable for llvm::ConstantFolder", referenced from:
      llvm::ConstantFolder::ConstantFolder() in main.cpp.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for llvm::IRBuilderFolder", referenced from:
      llvm::IRBuilderFolder::IRBuilderFolder() in main.cpp.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for llvm::IRBuilderDefaultInserter", referenced from:
      llvm::IRBuilderDefaultInserter::IRBuilderDefaultInserter() in main.cpp.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

My code:

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <filesystem>
#include <llvm/IR/LLVMContext.h>
#include "Lexer.h"
#include "Parser.h"
#include "utils/ByteOrder.h"
//...
int main(int _argc, char **_argv) {
    //...
    static llvm::LLVMContext thisContext;
    static llvm::IRBuilder<> Builder(thisContext);
    static std::unique_ptr<llvm::Module> thisModule;
    static std::map<std::string, llvm::Value *> NamedValues;
    //...
}

My Project: https://github.com/zly2006/Seserot

I want to know where is wrong and how can I fix this error, Thank you!

zly2006
  • 1
  • 1
  • This error seems to be because you are on an `arm64` cpu. You may be able to find a version of llvm compiled for arm, but you might have to build it from the source. – cs1349459 Sep 29 '22 at 13:50
  • 1
    Your comment in the code "# Link against LLVM libraries" seems to correspond to **none line**. There is no linkage command in your code. And you can cleanly observe that in the command line `c++ -g -arch arm64 -isysroot ...` - it doesn't contain any linkage. Note to the `target_link_libraries` call e.g. in [that question](https://stackoverflow.com/questions/30867712/add-llvm-to-project-using-cmake). – Tsyvarev Sep 29 '22 at 14:34

0 Answers0