Eclipse使用相对路径指定项目外部包含路径的方法
注:本问题是此前未获解答的Eclipse项目目录外包含路径配置问题的跟进问题,补充了更多实操细节。
在Eclipse环境中,需要添加与Eclipse项目目录同级的包含路径,出于可移植性要求,该路径必须使用相对路径而非绝对路径。本次实操使用的是MCUExpresso 11.5.1(基于Eclipse CDT 10.3.3构建),该问题在其他Eclipse衍生版本中也同样存在。
当前目录结构如下,my_includes/文件夹与Eclipse项目文件夹frdm_k64f/处于同一层级:
frdm_k64f/ .settings/ CMSIS/ ... my_includes/ foo.h
需求是将my_includes/添加到编译器的包含路径中,且必须使用相对路径,不能使用绝对路径。
方案1
依次进入Projects => Properties => C/C++ Build => Settings => MCU C Compiler => Includes配置页,添加路径"${workspace_loc:/${ProjName}/../my_includes}",但该方案失效:编译阶段,添加的"/../my_includes"条目从-I参数列表中消失,实际命令行参数如下:
... -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\board" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\source" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\drivers" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\utilities" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\serial_manager" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\uart" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\lists" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\device" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\CMSIS" -O0 -fno-common -g3 -Wall ...
方案2
直接指定绝对路径时配置可以正常生效,实际命令行参数如下:
-I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\board" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\source" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\drivers" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\utilities" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\serial_manager" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\uart" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\lists" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\device" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\CMSIS" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\my_includes" -O0 -fno-common -g3 -Wall ...
但出于项目可移植性要求,绝对路径方案不适用。
方案3
尝试配置路径"${workspace_loc}/my_includes",但该变量会展开为工作空间目录,而非项目目录,实际解析路径错误:
-I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\board" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\source" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\drivers" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\utilities" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\serial_manager" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\uart" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\lists" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\device" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\CMSIS" -I"C:\Users\r\Documents\MCUXpressoIDE_11.5.1_7266\workspace/my_includes" -O0 -fno-common -g3 -Wall -c -ffunction-sections ...
方案4(可生效)
尝试直接配置相对路径"../../my_includes",虽然写法不符合常规配置习惯,但可以正常生效,能够正确解析到目标包含目录:
-I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\board" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\source" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\drivers" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\utilities" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\serial_manager" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\uart" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\lists" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\device" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\CMSIS" -I"../../my_includes" -O0 -fno-common ...
方案5(推荐方案)
掌握Eclipse变量解析规则后,更规范的方案是使用ProjDirPath系统变量,配置路径为"${ProjDirPath}/../my_includes",该路径可正确展开为目标目录:
-I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\board" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\source" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\drivers" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\utilities" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\serial_manager" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\uart" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\component\lists" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\device" -I"C:\Users\r\Projects\mulib-examples\examples\test_stdbsp\frdm_k64f\CMSIS" -I"C:/Users/r/Projects/mulib-examples/examples/test_stdbsp/frdm_k64f/../my_includes" -O0 -fno-common -g3 -Wall ...
在Eclipse(含CDT衍生版本如MCUExpresso)中配置项目目录外的相对头文件包含路径,以下两种方式均可正常生效,满足项目可移植性要求:
- 直接填写相对路径:相对路径的计算基准是编译时的输出目录,根据实际目录层级调整
../的数量即可,本次同级目录场景下使用../../my_includes可正常工作 - 使用内置变量配置(优先选择):使用
${ProjDirPath}内置变量,该变量会自动解析为当前项目的根目录路径,在其后拼接../跳转至上级目录,再指向目标包含文件夹即可。本次场景下配置${ProjDirPath}/../my_includes即可稳定生效,配置可读性更强,不会因为构建输出目录调整出现路径失效问题。
避坑提示:不要使用${workspace_loc}类变量拼接相对路径,这类变量要么会被CDT的路径规范化逻辑直接过滤掉../片段导致配置失效,要么会解析到Eclipse全局工作空间根目录而非当前项目根目录,出现路径匹配错误。
内容的提问来源于stack exchange,提问作者fearless_fool

