zoukankan      html  css  js  c++  java
  • ArcCore重构-头文件引用问题的初步解决

    基于官方arc-stable-9c57d86f66be,AUTOSAR版本3.1.5
     
    基本问题
    1. 头文件引用混乱,所有头文件通过从搜索路径(-I)中引用,存在名称污染问题,需加入路径信息;
     
    ArcCore代码的编译系统中,定义了大量的inc-y路径,最后使用-I参数将这些路径加入到编译器的命令行参数中。而代码文件中,引用头文件时都是直接使用 #include "xxx.h" 的形式。
     
    这样有几个问题:
    1. 编译系统负担过重,需要加入大量的头文件搜索路径;
    2. 导致头文件名称污染问题,设想如果存在两个相同名称的头文件,则会出现引用混乱,先找到哪个算哪个?
    3. 导致代码结构不清,不清楚所引用的头文件的存放位置。不易阅读代码,不易掌握代码结构;
     
    重构的目标为:
    1. 头文件引用,基于一个相对路径,使用尖括号引用,如: #include <xxx.h> 
    2. 大量缩减inc-y的量,只需要少量的几个,如TOPDIR/include, ARCHDIR/include等;
     
    反过来看,名称污染问题的可能性是存在的,而当前的代码没有出现问题,在于没有出现同名的头文件。所以有一个简单的解决办法,就是统一基于TOPDIR/include路径,将所有代码文件中的头文件引用进行替换。
     
    可以使用脚本来完成这一动作,如下:
    #!/bin/bash
    # $1: TOPDIR if specified
    
    CURDIR=$(pwd)
    TOPDIR=$CURDIR
    TMPHDR=/tmp/.tmp.headers
    [ -n "$1" ] && TOPDIR=$1
    
    header_files=$(find ${TOPDIR} -name "*.h")
    echo -n > $TMPHDR
    for h in $header_files
    do
        echo ${h#${TOPDIR}/} >> $TMPHDR
    done
    
    unhandled_headers=
    target_files=$(find ${CURDIR} -name "*.c" -o -name "*.h")
    for t in $target_files
    do
        echo "Handling ${t#${CURDIR}/}..."
        _headers=$(grep "#include" $t | awk '{print $2}')
        for h in ${_headers}
        do
            echo -n "  $h"
            [ "${h:0:1}" == "<" ] && echo && continue
    
            # 1 for the double-quote
            nh=$(grep -w ${h:1:0-1} $TMPHDR 2>/dev/null)
            if [ -z "$nh" ]; then
                unhandled_headers="$unhandled_headers $h"
                echo
                continue
            fi
            [ "${nh:0:8}" == "include/" ] && nh=${nh#"include/"}
            echo " ---> <$nh>"
            sed -i "s@$h@<$nh>@g" $t
        done
    done
    
    echo "Header files not handled: $unhandled_headers"
    
    #rm -f $TMPHDR
  • 相关阅读:
    C/C++编程可用的Linux自带工具
    安装gcc及其依赖
    Linux上编译hadoop-2.7.1的libhdfs.so和libhdfs.a
    gcc链接参数--whole-archive的作用
    jdb调试程序
    Exception in thread "main" java.lang.Error: Unresolved compilation problem
    动态规划与分治、备忘录的区别
    leetcode-unique paths
    LeetCode总结 -- 一维动态规划篇
    编程技巧
  • 原文地址:https://www.cnblogs.com/wjcdx/p/8998587.html
Copyright © 2011-2022 走看看