Search code examples
c++compiler-constructionllvmllvm-c++-api

LLVM | codegen for program entry function with command line options


For my programming language, the entry of a program is like C/C++ main function:

int main(int argc, char **argv) {
    return 0
}

Suppose:

  • The IR code for main is generated into llvm::Function, using llvm::IRBuilder
  • I have llvm::LLVMContext and llvm::Module created

Question:

If I want to compile this code into binary prog, how do use llvm to generate the part that passes the commandline arguments to argc and argv and then call main, which are provided by users when running prog?


Solution

  • Thanks to arnt's comment:

    If that function actually is called main() and actually has those arguments, then it should just work. If it isn't, then you'll need to generate a bridge function that actually is called main() and calls the function you want to be called, in the way you want it to be called. – arnt

    It works!