如何使用eclipse cdt搭建linux内核源码阅读环境

阅读数:426 评论数:0

跳转到新版页面

分类

Linux

正文

说明:这种方式只是用于方便阅读代码,因为可以在源代间快速索引跳跃。但是最后可能会有一些warning,可以不必关心,如果是强迫症,可以使用下面这种方式来去掉。

// @suppress("Unused static function") @suppress("Unused function declaration")

 

1、下载安装eclipse 加 cdt

  cdt全称为c/c++ development tooling。有两种方式安装eclipse-cdt开发环境。

 (1)先安装eclipse基本环境:http://www.eclipse.org/downloads/

    然后安装cdt扩展:http://www.eclipse.org/cdt/

    

 (2)直接安装集成的eclipse c/c++开发环境:https://www.eclipse.org/downloads/packages/

 

2、配置编译内核以生成autoconf.h文件,这个文件里会有CONFIG_*的一些宏定义。

2、进入代码所在目录,make prepare生成kconfig.h,Eclipse的indexer会使用

3、打开eclipse

4、点击File->New->Makefile Project with Existing Code

5、在弹出的窗口中,选择c/c++ -> C project,点击Next。

6、填写一个项目名称,如Linux v5.1

7、去掉勾选Use default location,选择你的Linux源码所在目录

8、在下面的Project type面板中,选择Makefile project中的empty project。

9、在右侧Toolchain ofr Indexer Settings选择Linux GCC,点击Finish。

10、右键工程,点击Properties。

11、注意,现在Eclipse Oxygen已经开始对你的项目建索引,所以会使你的下面配置过程变卡。为了防止变卡,可以临时关闭索引功能:打开c/c++ General部分,选择Indexer,点击Enable project-specific settings,去掉Enable indexer的勾选。

12、打开c/c++ General

13、选择Preprocessor Include Paths

14、在Languages列表中选择GNU C

15、在Setting Entries列表中选择CDT User Setting Entries 

16、点击Add... ,在左侧下拉框中选择Preprocessor Macros File,右侧下拉框中选择Project Path,在File文本框中输入include/linux/kconfig.h。

17、同样的方式 添加你使用的其它宏文件,比gcc目录下的stdbool.h。

18、点击Indexer

19、在Enable project specifi setting框中去掉Index source files not included in the build勾选 

20、选择左侧的Paths and Symbols

21、选择Includes tab页,然后选择GNU C

22、点击Add...

23、点击Workspace...然后选择你的内核的include目录和include/uapi目录 

24、再次添加arch/architecture/include和arch/architecture/include/uapi目录,如arch/powerpc/include和arch/powerpc/include/uapi

25、点击#Symbols tab页

26、添加Add...

27、设置名称__KERNEL__,值为1添加ok.

28、点击 Source Location tab页

29、点击加号或箭头、三角在你的项目上

30、点击Filter,选择Edit Filter...

31、点击Add Multiple...,然后选择arch/下所有不使用的目录(即你不使用的architecture)

32、 点击ok再ok关闭对话框

33、在c/c++ General下选择Preprocessor Include Paths,Macros etc.

34、点击Providers tab页,选择CDT GCC Built-in Compiler Settings

35、去掉Use global provider shared between projects勾选 ,勾选 the Allocate console in the Console View checkbox

36、在Command to get compiler specs中追加-nostdinc,因为kernel是一个使用ISO C99定义的free-standing环境。所以不能引用标准的头文件和库,只以使用指定的。

37、打开一个termial,输入echo -isystem $(gcc -print-file-name=include/),把结果追加到-nostdinc后。虽然kernel不依赖以的标准系统的头文件的库,但是确依赖gcc提供的头文件 ,一般在/usr/lib/gcc/arch/version/include,所以添加 。

另外一种方式是使用-iwithprefix include

38、点击ok关闭对话框

39、点击Finish

40、工程会自动索引,在机械硬盘下大约用时20分钟,固态硬盘大约5分钟

 

https://wiki.eclipse.org/HowTo_use_the_CDT_to_navigate_Linux_kernel_source

英语原文:

  1. Download and install Eclipse plus the CDT.
  2. Configure and build your kernel to define CONFIG_* and generate autoconf.h.
  3. Start up Eclipse.
  4. Click File -> New -> Project
  5. In the pop-up window, choose C/C++-> C Project. Click Next
  6. Fill in a project name like Linux v5.1
  7. Uncheck the Use default location box and type in the root directory of your kernel into the Location box.
  8. In the Project type: pane, click the Makefile project and select Empty Project
  9. On the right side, select Linux GCC. Click Next
  10. Click Advanced settings... and a Properties dialog will pop up.
  11. Note: At this point, and starting from Eclipse Oxygen, Eclipse will aggressively start indexing your project, which can make Eclipse painfully slow for the rest of the configuration steps below. To mitigate that, temporarily disable indexing now by opening C/C++ General section, click on Indexer, click on Enable project-specific settings, then unmark the Enable indexer option.
  12. Open the C/C++ General selection on the left.
  13. Click on Preprocessor Include Paths
  14. Select GNU C in the Languages list
  15. Select CDT User Setting Entries in the Setting Entries list
  16. Click on Add.... Choose Preprocessor Macros File from the top left dropdown, Project Path from the top right dropdown, and enter "include/linux/kconfig.h" into the File text box.
  17. Also add any other macros files you are using.
  18. Click on Indexer
  19. Checkmark the Enable project specific settings box.
  20. Uncheck Index source files not included in the build
  21. Click on Paths and Symbols on the left.
  22. Select the Includes tab and then select GNU C
  23. Click Add...
  24. Click Workspace... then select your kernel's include, and include/uapi directories
  25. Do another Add, Workspace and add both arch/architecture/include, and arch/architecture/include/uapi directories. e.g., arch/powerpc/include and arch/powerpc/include/uapi (The UAPI directories are due to the kernel's user/kernel header split covered here in-detail)
  26. Click the # Symbols tab
  27. Click Add...
  28. Set the name to __KERNEL__
  29. Set the value to 1 and click OK
  30. Click the Source Location tab
  31. Click the plus sign (or arrow/triangle) next to your project name.
  32. Select the Filter item and click Edit Filter...
  33. Click Add Multiple... and then select all of the arch/* directories in your kernel source that will not be used (i.e. all the ones that are not for the architecture you are using)
  34. Click OK and OK again to dismiss that dialog.
  35. Under C/C++ General, select Preprocessor Include Paths, Macros etc.
  36. Click the Providers tab and select CDT GCC Built-in Compiler Settings
  37. Uncheck Use global provider shared between projects
  38. Append -nostdinc to the curretly-existing Command to get compiler specs. The kernel is a free-standing environment by ISO C99 definition. That is, it does not want to be polluted, and obviously cannot work with, the "host" header files and libraries.
  39. Open a terminal, and type "echo -isystem $(gcc -print-file-name=include/)". Append the resulting output to the Command to get compiler specs mentioned above. If you're using a cross-toolchain to compile the kernel, use the full path of that cross GCC compiler, instead of just typing gcc in the command mentioned. Rationale for this step: -nostdinc already asked gcc to not search the standard system directories for header files. But the Linux Kernel depends on GCC-provided "freestanding environment" headers like stdarg.h, stdbool.h and so on, which are typically hosted by GCC under /usr/lib/gcc/<arch>/<version>/include. Thus this step.
  40. Click OK on the Properties dialog.
  41. Note: If you temporarily disabled indexing as earlier recommended. This is the right time to re-enable it. Under C/C++ General, click on Indexer, and mark the Enable indexer option.
  42. Click Finish on the C Project dialog.
  43. The Project will index automatically.
  44. On a platter drive indexing will take upwards of 20 minutes to complete, on a SSD indexing will take about 5 minutes to complete.



相关推荐

一、概述 1、为什么Linus不使用GPLv3 在 PC 上,只要你得到了某个程序的源代码,就可以自行编译生成二进制程序,然后替换掉原有的二进制程序,你的程序自由很容易得到保证。然而 iPod、iPh

一、概述 在Linux系统中,/usr/bin和/usr/local/bin是两个常见的目录,用于存放可执行文件(二进制文件)。 很多应用都安装在/usr/local下面,先看一下automake工具

  一、概述 vmstat命令是最常见的Linux/Unix监控工具,可以监控给定时间间隔服务器的CPU使用率、内存使用、IO情况。相比top命令,可以查看到整个机器的CPU、内存、IO的使用情况,而

一、概述 sar,System Activity Reporter。是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况、系统调用的使用情况、磁

一、简介 简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。 二、使用方法 1、基本语法  awk '条件类型1 {动作1} 条件类型2{动作2} ...

一、概述 列出目标目录中所有的子目录和文件。 二、 语法 ls [选项] [目录名] -a, –all 列出目录下的所有文件,包括以 . 开头的隐含文件 -A 同-a,但不列出“.”(表示当前目录)

一、概述 cd全称是change directory,用于切换当前工作目录。 注意的是,cd命令是一个内建命令,它是由 shell 提供的。因此,不同的 shell 可能会有一些差异,但基本的用法和功

一、概述 全称为print working directory,查看”当前工作目录“的完整路径,一般情况下不带任何参数 二、语法 pwd [选项] -L 即logical,逻辑路径 -P 即

一、概述 通过 mkdir 命令可以实现在指定位置创建以 DirName(指定的文件名)命名的文件夹或目录。要创建文件夹或目录的用户必须对所创建的文件夹的父文件夹具有写权限。并且,所创建的文件夹(目录

一、概述 删除一个目录中的一个或多个文件或目录,如果没有使用- r选项,则rm不会删除目录。如果使用 rm 来删除文件,通常仍可以将该文件恢复原状。 删掉文件其实只是将指向数据块的索引点(inform