编译《自制编程语言 基于c语言》 郑钢 书中代码 idea

编译《自制编程语言 基于c语言》 郑钢 书中代码

之前挖的坑,既然有一些小哥有很多问号,今天过来把这个坑填上

编译器

  1. 之前用IDEA习惯了,这边就用jb公司的同类编译器CLion
  2. 这个有个好处,就是他里面自带编译环境,不用单独下载其他东西了
  3. 安装就用默认的,下一步下一步就好
  4. 地址: https://www.jetbrains.com/clion/ 或者qq群文件上下载(1046848140)

代码获取

正规途径

  1. 根据书中地址获取源码
  2. 把最后的CMakeLists文件和main.c补上

其他途径

  1. csdn资源下载 : 自制编程语言 基于c语言郑钢 书中代码,可以直接编译运行

  2. qq群文件上下载,包括编译器也有 1046848140

运行

hello world

请直接按照这个步骤操作,搞个hello world出来,确认你的环境是可以运行c语言的

  1. CMakeLists.txt 和 main.c 这两个文件,复制到c2同级目录下面
  2. 然后打开项目 stepByStep
  3. 打开main.c,应该会有个绿色小箭头,单击,run
  4. 看到有hello world,起码你的环境是可以的了

运行你的代码

  1. 运行书里的代码,按照我那个注释来就好
  2. 要运行c2模块/或者其中某个小模块的代码,那就把cmake中这些类的注释打开
  3. c2-f对应的是下面这些文件
#include_directories(c2/f/cli)
#include_directories(c2/f/include)
#include_directories(c2/f/vm)
#include_directories(c2/f/parser)

#        c2/f/cli/cli.c
#        c2/f/cli/cli.h
#        c2/f/include/common.h

#       c2/f/include/utils.c
#        c2/f/include/utils.h
#        c2/f/vm/vm.c
#        c2/f/vm/vm.h
#        c2/f/vm/core.c
#        c2/f/vm/core.h

#       c2/f/include/unicodeUtf8.c
#       c2/f/include/unicodeUtf8.h
#       c2/f/parser/parser.c
#       c2/f/parser/parser.h

  1. reload一下cmake文件
  2. 把 main.c 中的main函数改成其他名字,如果有多个main函数,运行起来报错
  3. 找到 c2/f/cli/cli.c 中的main函数,将文件地址改成对应的
int main(int argc, const char** argv) {
   if (argc == 1) {
       runFile("/Users/lizhao/workdir/code/c/liz/c2/sample.sp");
//       runFile("D:\\workdir\\lizhao\\code\\c\\liz\\c2\\sample.sp");
   } else {
      runFile(argv[1]);
   }
   return 0;
}
  1. 运行就好

其他模块

  1. 就不多说了,能运行代码,后面瞎搞瞎搞都能运行起来了

相关过程图片

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

相关地址

CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(stepByStep)

set(CMAKE_C_STANDARD 99)

include_directories(.)

#include_directories(c2/f/cli)
#include_directories(c2/f/include)
#include_directories(c2/f/vm)
#include_directories(c2/f/parser)


#include_directories(c2/cli)
#include_directories(c2/include)
#include_directories(c2/vm)
#include_directories(c2/parser)

#include_directories(c3/cli)
#include_directories(c3/include)
#include_directories(c3/vm)
#include_directories(c3/parser)
#include_directories(c3/object)

#include_directories(c4/cli)
#include_directories(c4/compiler)
#include_directories(c4/include)
#include_directories(c4/vm)
#include_directories(c4/parser)
#include_directories(c4/object)

#include_directories(c6/cli)
#include_directories(c6/compiler)
#include_directories(c6/include)
#include_directories(c6/vm)
#include_directories(c6/parser)
#include_directories(c6/object)

#include_directories(c8/cli)
#include_directories(c8/compiler)
#include_directories(c8/include)
#include_directories(c8/vm)
#include_directories(c8/parser)
#include_directories(c8/object)

add_executable(stepByStep main.c

              #  c2/f/cli/cli.c
            #    c2/f/cli/cli.h
           #     c2/f/include/common.h

          #     c2/f/include/utils.c
           #     c2/f/include/utils.h
           #     c2/f/vm/vm.c
        #        c2/f/vm/vm.h
      #          c2/f/vm/core.c
         #       c2/f/vm/core.h
#
     #         c2/f/include/unicodeUtf8.c
            #   c2/f/include/unicodeUtf8.h
        #       c2/f/parser/parser.c
     #          c2/f/parser/parser.h


        #        c2/cli/cli.c
        #        c2/cli/cli.h
        #        c2/include/common.h
        #
        #        c2/include/utils.c
        #        c2/include/utils.h
        #        c2/vm/vm.c
        #        c2/vm/vm.h
        #        c2/vm/core.c
        #        c2/vm/core.h
        #
        #        c2/include/unicodeUtf8.c
        #        c2/include/unicodeUtf8.h
        #        c2/parser/parser.c
        #        c2/parser/parser.h

        #        c3/cli/cli.c
        #        c3/cli/cli.h
        #        c3/include/common.h
        #
        #        c3/include/utils.c
        #        c3/include/utils.h
        #        c3/vm/vm.c
        #        c3/vm/vm.h
        #        c3/vm/core.c
        #        c3/vm/core.h
        #
        #        c3/include/unicodeUtf8.c
        #        c3/include/unicodeUtf8.h
        #        c3/parser/parser.c
        #        c3/parser/parser.h
        #
        #        c3/object/header_obj.c
        #        c3/object/header_obj.h
        #        c3/object/class.c
        #        c3/object/class.h
        #        c3/object/obj_fn.h
        #        c3/object/obj_string.h
        #        c3/object/obj_string.c
        #        c3/object/meta_obj.c
        #        c3/object/meta_obj.h
        #        c3/object/obj_fn.c
        #        c3/object/obj_list.c
        #        c3/object/obj_list.h
        #        c3/object/obj_range.c
        #        c3/object/obj_range.h
        #        c3/object/obj_map.c
        #        c3/object/obj_map.h
        #        c3/object/obj_thread.c
        #        c3/object/obj_thread.h

        #        c4/cli/cli.c
        #        c4/cli/cli.h
        #        c4/include/common.h
        #
        #        c4/include/utils.c
        #        c4/include/utils.h
        #        c4/vm/vm.c
        #        c4/vm/vm.h
        #        c4/vm/core.c
        #        c4/vm/core.h
        #
        #        c4/include/unicodeUtf8.c
        #        c4/include/unicodeUtf8.h
        #        c4/parser/parser.c
        #        c4/parser/parser.h
        #
        #        c4/object/header_obj.c
        #        c4/object/header_obj.h
        #        c4/object/class.c
        #        c4/object/class.h
        #        c4/object/obj_fn.h
        #        c4/object/obj_string.h
        #        c4/object/obj_string.c
        #        c4/object/meta_obj.c
        #        c4/object/meta_obj.h
        #        c4/object/obj_fn.c
        #        c4/object/obj_list.c
        #        c4/object/obj_list.h
        #        c4/object/obj_range.c
        #        c4/object/obj_range.h
        #        c4/object/obj_map.c
        #        c4/object/obj_map.h
        #        c4/object/obj_thread.c
        #        c4/object/obj_thread.h
        #        c4/compiler/compiler.c
        #        c4/compiler/compiler.h

#        c6/cli/cli.c
#        c6/cli/cli.h
#        c6/include/common.h
#
#        c6/include/utils.c
#        c6/include/utils.h
#        c6/vm/vm.c
#        c6/vm/vm.h
#        c6/vm/core.c
#        c6/vm/core.h
#
#        c6/include/unicodeUtf8.c
#        c6/include/unicodeUtf8.h
#        c6/parser/parser.c
#        c6/parser/parser.h
#
#        c6/object/header_obj.c
#        c6/object/header_obj.h
#        c6/object/class.c
#        c6/object/class.h
#        c6/object/obj_fn.h
#        c6/object/obj_string.h
#        c6/object/obj_string.c
#        c6/object/meta_obj.c
#        c6/object/meta_obj.h
#        c6/object/obj_fn.c
#        c6/object/obj_list.c
#        c6/object/obj_list.h
#        c6/object/obj_range.c
#        c6/object/obj_range.h
#        c6/object/obj_map.c
#        c6/object/obj_map.h
#        c6/object/obj_thread.c
#        c6/object/obj_thread.h
#        c6/compiler/compiler.c
#        c6/compiler/compiler.h

#        c8/cli/cli.c
#        c8/cli/cli.h
#        c8/include/common.h
#
#        c8/include/utils.c
#        c8/include/utils.h
#        c8/vm/vm.c
#        c8/vm/vm.h
#        c8/vm/core.c
#        c8/vm/core.h
#
#        c8/include/unicodeUtf8.c
#        c8/include/unicodeUtf8.h
#        c8/parser/parser.c
#        c8/parser/parser.h
#
#        c8/object/header_obj.c
#        c8/object/header_obj.h
#        c8/object/class.c
#        c8/object/class.h
#        c8/object/obj_fn.h
#        c8/object/obj_string.h
#        c8/object/obj_string.c
#        c8/object/meta_obj.c
#        c8/object/meta_obj.h
#        c8/object/obj_fn.c
#        c8/object/obj_list.c
#        c8/object/obj_list.h
#        c8/object/obj_range.c
#        c8/object/obj_range.h
#        c8/object/obj_map.c
#        c8/object/obj_map.h
#        c8/object/obj_thread.c
#        c8/object/obj_thread.h
#        c8/compiler/compiler.c
#        c8/compiler/compiler.h

        )

main.c

#include <stdio.h>

//int mainaa() {
// 测试程序cmake能否运行,打开这个。
// 如果要运行上面模块的代码,就把这个方法名称改成 main方法名字改成其他的,
int maina() {
    printf("Hello, World!\n");
    return 0;
}

重点

百年不更新的公众号,欢迎关注: 小王老店

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读