如何使用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
英语原文:
- Download and install Eclipse plus the CDT.
- Configure and build your kernel to define CONFIG_* and generate autoconf.h.
- Start up Eclipse.
- Click File -> New -> Project
- In the pop-up window, choose C/C++-> C Project. Click Next
- Fill in a project name like Linux v5.1
- Uncheck the Use default location box and type in the root directory of your kernel into the Location box.
- In the Project type: pane, click the Makefile project and select Empty Project
- On the right side, select Linux GCC. Click Next
- Click Advanced settings... and a Properties dialog will pop up.
- 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.
- Open the C/C++ General selection on the left.
- Click on Preprocessor Include Paths
- Select GNU C in the Languages list
- Select CDT User Setting Entries in the Setting Entries list
- 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.
- Also add any other macros files you are using.
- Click on Indexer
- Checkmark the Enable project specific settings box.
- Uncheck Index source files not included in the build
- Click on Paths and Symbols on the left.
- Select the Includes tab and then select GNU C
- Click Add...
- Click Workspace... then select your kernel's include, and include/uapi directories
- 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)
- Click the # Symbols tab
- Click Add...
- Set the name to __KERNEL__
- Set the value to 1 and click OK
- Click the Source Location tab
- Click the plus sign (or arrow/triangle) next to your project name.
- Select the Filter item and click Edit Filter...
- 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)
- Click OK and OK again to dismiss that dialog.
- Under C/C++ General, select Preprocessor Include Paths, Macros etc.
- Click the Providers tab and select CDT GCC Built-in Compiler Settings
- Uncheck Use global provider shared between projects
- 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.
- 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.
- Click OK on the Properties dialog.
- 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.
- Click Finish on the C Project dialog.
- The Project will index automatically.
- On a platter drive indexing will take upwards of 20 minutes to complete, on a SSD indexing will take about 5 minutes to complete.