在一个函数中用到一个全局变量,risc32e_regs[]
如果在这个函数所在文件中没有声明这个变量,则错误如下:
F:\studio\RDView_tmp\display-utils.c(68) : warning C4013: 'fatal_error' undefined; assuming extern returning int
F:\studio\RDView_tmp\display-utils.c(83) : error C2065: 'risc32e_regs' : undeclared identifier
F:\studio\RDView_tmp\display-utils.c(83) : error C2109: subscript requires array or pointer type
F:\studio\RDView_tmp\display-utils.c(83) : error C2224: left of '.value' must have struct/union type
F:\studio\RDView_tmp\display-utils.c(86) : error C2109: subscript requires array or pointer type
F:\studio\RDView_tmp\display-utils.c(86) : error C2224: left of '.value' must have struct/union type
F:\studio\RDView_tmp\display-utils.c(89) : error C2109: subscript requires array or pointer type
F:\studio\RDView_tmp\display-utils.c(89) : error C2224: left of '.value' must have struct/union type
F:\studio\RDView_tmp\display-utils.c(92) : error C2109: subscript requires array or pointer type
F:\studio\RDView_tmp\display-utils.c(92) : error C2224: left of '.value' must have struct/union type
Error executing cl.exe.
Creating browse info file...如果加上声明,则错误如下:
display-utils.c
F:\studio\RDView_tmp\display-utils.c(29) : error C2061: syntax error : identifier 'risc32e_regs'
F:\studio\RDView_tmp\display-utils.c(29) : error C2059: syntax error : ';'
F:\studio\RDView_tmp\display-utils.c(29) : error C2059: syntax error : '['
F:\studio\RDView_tmp\display-utils.c(68) : warning C4013: 'fatal_error' undefined; assuming extern returning int
F:\studio\RDView_tmp\display-utils.c(83) : error C2065: 'risc32e_regs' : undeclared identifier
F:\studio\RDView_tmp\display-utils.c(83) : error C2109: subscript requires array or pointer type
F:\studio\RDView_tmp\display-utils.c(83) : error C2224: left of '.value' must have struct/union type
F:\studio\RDView_tmp\display-utils.c(86) : error C2109: subscript requires array or pointer type
F:\studio\RDView_tmp\display-utils.c(86) : error C2224: left of '.value' must have struct/union type
F:\studio\RDView_tmp\display-utils.c(89) : error C2109: subscript requires array or pointer type
F:\studio\RDView_tmp\display-utils.c(89) : error C2224: left of '.value' must have struct/union type
F:\studio\RDView_tmp\display-utils.c(92) : error C2109: subscript requires array or pointer type
F:\studio\RDView_tmp\display-utils.c(92) : error C2224: left of '.value' must have struct/union type
Error executing cl.exe.该函数如下:
#ifdef __STDC__
char *
registers_as_string (char *buf, int* max_buf_len, int* string_len)
#else
char *
registers_as_string (buf, max_buf_len, string_len)
char *buf;
int *max_buf_len;
int *string_len;
#endif
{
  int i;
  char *bufp;
  char *grstr;  
  char *grfill;    
  static char *reg_names[] =
  {"r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
   "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
   "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
   "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"};  if (buf == NULL)
    fatal_error ("NULL buf pointer");
  *buf = '\0';
  bufp = buf;  // printf a seperator line
sprintf (bufp, " ------------------------------------------------------------------------\n");
bufp += strlen (bufp);  //printf cp0 registers  // printf general registers
  grstr = "R%-2d (%2s) = %08x", grfill = "  ";
  sprintf (bufp, "\t\t\t\t General Registers\n"); bufp += strlen (bufp);
  for (i = 0; i < 8; i++)
  {
    sprintf (bufp, grstr, i, reg_names[i], risc32e_regs[i].value);
    bufp += strlen (bufp);
    sprintf (bufp, grfill); bufp += strlen (bufp);
    sprintf (bufp, grstr, i+8, reg_names[i+8], risc32e_regs[i+8].value);
    bufp += strlen (bufp);
    sprintf (bufp, grfill); bufp += strlen (bufp);
    sprintf (bufp, grstr, i+16, reg_names[i+16], risc32e_regs[i+16].value);
    bufp += strlen (bufp);
    sprintf (bufp, grfill); bufp += strlen (bufp);
    sprintf (bufp, grstr, i+24, reg_names[i+24], risc32e_regs[i+24].value);
    bufp += strlen (bufp);
    sprintf (bufp, "\n");
    bufp += 1;
  }
 
//-------------------------------------------------------------------
  *string_len = bufp - buf;
  if (*max_buf_len <= *string_len)
    fatal_error ("Exceeded buffer size");  return (buf);
}变量在另一文件中定义,如下:
core_register_t risc32e_regs[] = {
{"R0", "zero", 0x0L}, {"R1", "at", 0x0L}, {"R2", "v0", 0x0L},  {"R3", "v1", 0x0L},
{"R4", "a0", 0x0L},  {"R5", "a1", 0x0L},  {"R6", "a2", 0x0L},  {"R7", "a3", 0x0L},
{"R8", "t0", 0x0L},  {"R9", "t1", 0x0L},  {"R10", "t2", 0x0L}, {"R11", "t3", 0x0L},
{"R12", "t4", 0x0L}, {"R13", "t5", 0x0L}, {"R14", "t6", 0x0L}, {"R15", "t7", 0x0L},
{"R16", "s0", 0x0L}, {"R17", "s1", 0x0L}, {"R18", "s2", 0x0L}, {"R19", "s3", 0x0L},
{"R20", "s4", 0x0L}, {"R21", "s5", 0x0L}, {"R22", "s6", 0x0L}, {"R23", "s7", 0x0L},
{"R24", "t8", 0x0L}, {"R25", "t9", 0x0L}, {"R26", "k0", 0x0L}, {"R27", "k1", 0x0L},
{"R28", "gp", 0x0L}, {"R29", "sp", 0x0L}, {"R30", "s8", 0x0L}, {"R31", "ra", 0x0L},
{NULL, NULL, 0x0L} /* the last element must be NULL */
};在别的文件中也有用到这个全局数组,进行extern声明后,可以使用,这是怎么回事

解决方案 »

  1.   

    声明语句为:extern core_register_t risc32e_regs[];
    忘写了
      

  2.   

    core_register_t 这个定义的头文件,你也需要包含,extern申明的cpp文件要包含,不然不认识这个定义
      

  3.   

    core_register_t 在RISC32E.h定义
    typedef struct {
    char *name;
    char *alias;
    u32 value;
    }core_register_t;函数中加上
    #include "RISC32E.h"
    及声明extern core_register_t risc32e_regs[],错误更多
    f:\studio\rdview_tmp\ejtag.h(110) : error C2061: syntax error : identifier 'CEJTAG'
    f:\studio\rdview_tmp\ejtag.h(110) : error C2059: syntax error : ';'
    f:\studio\rdview_tmp\ejtag.h(111) : error C2449: found '{' at file scope (missing function header?)
    f:\studio\rdview_tmp\ejtag.h(140) : error C2059: syntax error : '}'
    f:\studio\rdview_tmp\risc32e.h(151) : error C2061: syntax error : identifier 'CRISC32E'
    f:\studio\rdview_tmp\risc32e.h(151) : error C2059: syntax error : ';'
    f:\studio\rdview_tmp\risc32e.h(152) : error C2449: found '{' at file scope (missing function header?)
    f:\studio\rdview_tmp\risc32e.h(198) : error C2059: syntax error : '}'
    f:\studio\rdview_tmp\display-utils.c(69) : warning C4013: 'fatal_error' undefined; assuming extern returning int
    f:\studio\rdview_tmp\display-utils.c(84) : error C2065: 'risc32e_regs' : undeclared identifier
    f:\studio\rdview_tmp\display-utils.c(84) : error C2109: subscript requires array or pointer type
    f:\studio\rdview_tmp\display-utils.c(84) : error C2224: left of '.value' must have struct/union type
    f:\studio\rdview_tmp\display-utils.c(87) : error C2109: subscript requires array or pointer type
    f:\studio\rdview_tmp\display-utils.c(87) : error C2224: left of '.value' must have struct/union type
    f:\studio\rdview_tmp\display-utils.c(90) : error C2109: subscript requires array or pointer type
    f:\studio\rdview_tmp\display-utils.c(90) : error C2224: left of '.value' must have struct/union type
    f:\studio\rdview_tmp\display-utils.c(93) : error C2109: subscript requires array or pointer type
    f:\studio\rdview_tmp\display-utils.c(93) : error C2224: left of '.value' must have struct/union type
    RISC32E.cpp
    F:\studio\RDView_tmp\RISC32E.cpp(225) : warning C4715: 'CRISC32E::risc32e_exec_instruction' : not all control paths return a value
    F:\studio\RDView_tmp\RISC32E.cpp(600) : warning C4715: 'CRISC32E::risc32e_register_read' : not all control paths return a value
    Error executing cl.exe.
    Creating browse info file...
    其中CEJTAG在如下文件中定义,// EJTAG.h: interface for the CEJTAG class.
    //
    //////////////////////////////////////////////////////////////////////#if !defined(AFX_EJTAG_H__5ADB96CE_7E4E_4F9E_A1C0_649BBCF769BF__INCLUDED_)
    #define AFX_EJTAG_H__5ADB96CE_7E4E_4F9E_A1C0_649BBCF769BF__INCLUDED_#include "types.h"
    #include "mylpt_lib.h" // Added by ClassView
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000# define  MAX_LENGTH 3...
    typedef struct {
    int bitnr; 
    u32 writein[MAX_LENGTH];
    u32 readout[MAX_LENGTH];
    }testdata_reg_t;class CEJTAG  
    {
    public:
    testdata_reg_t all;
    testdata_reg_t fastdata;
    testdata_reg_t data;
    testdata_reg_t address;
    testdata_reg_t control;
    testdata_reg_t instruction;
    testdata_reg_t impcode;
    testdata_reg_t idcode;
    testdata_reg_t bypass;
    int jtag_reset(void);
    int jtag_bypass(void);
    int jtag_idcode(u32 *cpu_id);
    int jtag_write_ireg(u32 jtag_instruction);
    int jtag_rw_dreg(int bitnr, const u32 *write_buf, u32 *read_buf);
    bool m_bPortOpened;
    MYLPT_HANDLE hMYLPT;
    CEJTAG();
    virtual ~CEJTAG();protected:
    BYTE dataport_value;
    int jtag_access_reg(int bitnr, const u32 *write_buf, u32 *read_buf);
    void enter_next_state(int tms);
    void set_tdi(int tdi_val);
    int read_tdo(void);
    void pulse_tck(void);
    void set_tms(int tms);
    };#endif

    // RISC32E.h: interface for the CRISC32E class.
    //
    //////////////////////////////////////////////////////////////////////
    #if !defined(AFX_RISC32E_H__B6AA83CB_1A6C_4386_8875_18E9D6FBA6CF__INCLUDED_)
    #define AFX_RISC32E_H__B6AA83CB_1A6C_4386_8875_18E9D6FBA6CF__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000#include "types.h"
    #include "EJTAG.h"
    .../*
     * normal test data registers, such as: BYPASS register,
     * IDCODE register, INSTRUCTION register and SCAN PATH
     * SELECT register, etc.
     */
    typedef struct {
    char *name;
    char *alias;
    u32 value;
    }core_register_t;/* cp0 registers */typedef struct 
    {
    char *name;
    char *alias;
    u32 value;
    }cp0_register_t;typedef struct
    {
    u32 reg_num;
    u32 value;
    }lw_sw_data_t;/* the register of the drseg logic */typedef struct {
    char *name;
    char *desc; /* long name string */
    u32 addr;
    u32 value;
    }drseg_register_t;
    class CRISC32E  
    {
    public:
    int risc32e_step(void);
    int risc32e_restart();
    int risc32e_halt(void);
    drseg_register_t  * drseg_regs;
    cp0_register_t  * cp0_regs;
    core_register_t * core_regs;
    CEJTAG* m_LPT;
    int halt_reason;
    int status;
    CRISC32E();
    virtual ~CRISC32E();private:
    int risc32e_memory_write32(u32 * buf, u32 address, u32 length);
    int risc32e_memory_read32(u32 * buf, u32 address, u32 length);
    int risc32e_memory_write16(u16 *buf, u32 address, u32 length);
    int risc32e_memory_read16(u16 *buf, u32 address, u32 length);
    int risc32e_memory_write8(u8 *buf, u32 address, u32 length);
    int risc32e_memory_read8(u8 *buf, u32 address, u32 length);
    int risc32e_clear_sfbreakpt(void);
    int risc32e_set_sfbreakpt(void);
    int risc32e_clear_dtbrkpt(u32 dt_addr, u32 mask, u8 asid, u32 dt_crtl, int num);
    int risc32e_set_insbrkpt(u32 ins_addr, u32 mask, u8 asid, int num);
    int risc32e_clear_insbrkpt(u32 ins_addr, u32 mask, u8 asid, int num);
    int risc32e_set_dtbrkpt(u32 dt_addr, u32 mask, u8 asid, u32 dt_crtl, int num);
    int risc32e_drseg_write(u32 reg_addr, u32 reg_val);
    int risc32e_drseg_read(u32 reg_addr, u32 *reg_val);
    int risc32e_cp0_write(cp0_register_t *cp0_reg, u32 value);
    int risc32e_LO_read(cp0_register_t *cp0_reg);
    int risc32e_HI_read(cp0_register_t *cp0_reg);
    int risc32e_cp0_read(cp0_register_t *cp0_reg);
    int risc32e_register_write(core_register_t *reg, u32 value);
    int strncasecmp(char *s1, char *s2, u32 len);
    int str2int(char *strval, u32 *value);
    int risc32e_register_read(core_register_t *reg);
    int strcasecmp(char *s1, char *s2);
    drseg_register_t * drseg_register_index(char *regname);
    cp0_register_t * cp0_register_index(char *regname);
    core_register_t * register_index(char *regname);
    int risc32e_sw_nop(void);
    int risc32e_lw_nop(int value);
    int risc32e_exec_instruction(u32 *writein, u32 *readout);
    int risc32e_r6_save();};#endif 
      

  4.   

    'CEJTAG' 'CRISC32E'  等这些定义的头文件包含没呢
      

  5.   

    这个函数在display-utils.c中定义,是不是由于后缀的问题,把display-utils.c改为display-utils.cpp是不是就可以解决