qq335702318 发表于 2019-12-21 10:10:24

Typedef struct应该怎么写才是最标准的用法?

MCU:粤原点
编译器:ORIGINIDE.exe V2.1.0 (粤原点自己的编译器)
问题:

我以前用STM8在IAR环境一直都是

-------------在TEST.h文件内--------------
typedef struct
{
unsigned char data1;
unsigned char data2;
unsigned char data3;
unsigned char data4;
}TEST;
extern TEST test;

-------------在TEST.c文件内--------------
TEST test = {0,0,0,0};


-------------在other.c文件内--------------
#include "TEST.h"
....
test.data1 = xx;
test.data2 = test.data3;


这种用法不标准吗?
在粤原点的编译器上怎么弄都提示错误


qq335702318 发表于 2019-12-21 10:11:40

自己顶一下

qq335702318 发表于 2019-12-21 10:20:28

谁跳过这个坑~~

Tliang 发表于 2019-12-21 10:23:37

我也是这样做...

Excellence 发表于 2019-12-21 10:26:22

typedef struct _TEST
{
unsigned char data1;
unsigned char data2;
unsigned char data3;
unsigned char data4;
}TEST;

再试试。

楼主位也是很常用的写法。

wmm20031015 发表于 2019-12-21 10:33:59

编译器都是PICC,这种用法没问题,用FMD的2.1.4测试没有问题,

qq335702318 发表于 2019-12-21 10:38:23

Excellence 发表于 2019-12-21 10:26
typedef struct _TEST
{
unsigned char data1;


谢谢回答,试过不行,报错提示缺失结构体类型定义
不过我改了一下,一个可行一个不可行

******** 在TEST.C***************
#include        "TEST.h";
....
typedef struct
{
unsigned char data1;
unsigned char data2;
unsigned char data3;
unsigned char data4;
}TEST;
TEST test = {0,0,0,0,};


******** 在TEST.h***************
#include "TEST.c";
extern TEST test;

******** 在other.c***************
test.data1 = 2;

这样没报错
但另一份.c .h上使用完全一样的方法定义一个新结构体
在other.c上用完全一样的方法使用却报错...
检查过第二个结构体并无拼错


Excellence 发表于 2019-12-21 10:43:10

******** 在TEST.h***************

    #include "TEST.c";
    extern TEST test;



这是哪门子写法?????

Excellence 发表于 2019-12-21 10:43:36

C中定义,H中声明,OTHER包含头文件。。。。。

qq335702318 发表于 2019-12-21 10:46:59

Excellence 发表于 2019-12-21 10:43
******** 在TEST.h***************

    #include "TEST.c";


我也不想这样做{:lol:}
偏偏这样写
起码有一个结构体能被Other.c使用
以前在IAR上我都会把typedef struct写在h,也不会在.h里面include **.c

Excellence 发表于 2019-12-21 10:48:58

qq335702318 发表于 2019-12-21 10:46
我也不想这样做
偏偏这样写
起码有一个结构体能被Other.c使用


能用也行。呵呵。

zf12862177 发表于 2019-12-21 10:50:27

你的 test.c 里面没有include test.h

qq335702318 发表于 2019-12-21 10:53:09

zf12862177 发表于 2019-12-21 10:50
你的 test.c 里面没有include test.h

实际代码是有的
并且我都是保存每一份.c都会include 它本身.h
除非.h文件啥内容都没有

kebaojun305 发表于 2019-12-21 15:12:22

就是HI-TECH PICC 的编译器

yoz 发表于 2019-12-21 15:44:29

把extern从h文件挪到需要extern的c文件里,然后两个c文件都include h文件

qq335702318 发表于 2019-12-21 18:59:06

yoz 发表于 2019-12-21 15:44
把extern从h文件挪到需要extern的c文件里,然后两个c文件都include h文件

特别花了一整天整理了整份源程序,好像正常了
1. 将所有.c内的宏定义全部移到对应的.h文件
2. 将所有.h文件需要include的其他.h文件全部合并到一份.h上(命名为FILESINC.h),然后所有.h的include一律只用 include "FILESINC.h"
3. 将所有.c内的extern声明全部删除
4. 将所有typedef struct全部放在.h上定义
5. 检查所有变量和函数,每份.c都尽可能各司其职,减少函数功能和.c混乱的情况

周一继续优化,谢谢各位

adongliu 发表于 2019-12-21 20:47:05

好像是结构定义在变量声明之后的问题,在定义变量的时候前面加struct 应该就可以了。

xtwhf 发表于 2019-12-21 21:09:39

奇怪!主流的编译器用楼主位代码都没问题!

xtwhf 发表于 2019-12-21 23:39:49

-----test.h-----
typedef struct
{
unsigned char data1;
unsigned char data2;
unsigned char data3;
unsigned char data4;
}TEST;

------test.c--------
#include "test.h"

TEST test;

------other.h------
void Test_init(void);

-------other.c-------
#include "test.h"
#include "other.h"

extern TEST test;

void Test_init(void)
{
test.data1=1;
test.data2=2;
test.data3=3;
test.data4=4;
}

------main.c------
#include "test.h"
#include "other.h"

extern TEST test;

void Test_Add(void)
{
test.data1 +=1;
test.data2 +=2;
test.data3 +=3;
test.data4 +=4;
}

main()
{
Test_init();
Test_Add();
while(1);
}

用你的编译器试试!

lzyr 发表于 2019-12-21 23:50:06

楼主位的定义没问题gcc编译通过

xtwhf 发表于 2019-12-21 23:51:56


-----test.h-----
typedef struct
{
unsigned char data1;
unsigned char data2;
unsigned char data3;
unsigned char data4;
}TEST;
extern TEST test;
------test.c--------
#include "test.h"

TEST test;

------other.h------
void Test_init(void);

-------other.c-------
#include "test.h"
#include "other.h"

void Test_init(void)
{
test.data1=1;
test.data2=2;
test.data3=3;
test.data4=4;
}

------main.c------
#include "test.h"
#include "other.h"

void Test_Add(void)
{
test.data1 +=1;
test.data2 +=2;
test.data3 +=3;
test.data4 +=4;
}

main()
{
Test_init();
Test_Add();
while(1);
}

再用你的编译器试试!

xtwhf 发表于 2019-12-21 23:54:14

lzyr 发表于 2019-12-21 23:50
楼主位的定义没问题gcc编译通过

本来主流编译器都没问题。。。。

xtwhf 发表于 2019-12-22 00:03:01

qq335702318 发表于 2019-12-21 18:59
特别花了一整天整理了整份源程序,好像正常了
1. 将所有.c内的宏定义全部移到对应的.h文件
2. 将所有.h文 ...

本来主流编译器都没问题。。。。 楼主用什么圆点编译器新建项目运行下看看。上面的两个都没问题话!编译器表示不背锅{:lol:}

qq335702318 发表于 2019-12-22 00:36:10

xtwhf 发表于 2019-12-22 00:03
本来主流编译器都没问题。。。。 楼主用什么圆点编译器新建项目运行下看看。上面的两个都没问题话!编译 ...

谢谢
我已经重整了代码
应该跟编译器无关
不过还是要吐槽一下
那编译器真JB烂{:titter:}

get500wan 发表于 2019-12-22 08:04:38

到底报的什么错也没见楼主说。另外,头文件里,最好加上防止重复定义的那个宏检查吧。

qq335702318 发表于 2019-12-22 10:29:29

get500wan 发表于 2019-12-22 08:04
到底报的什么错也没见楼主说。另外,头文件里,最好加上防止重复定义的那个宏检查吧。 ...

#ifndef _TEST_H_
#define _TEST_H_

#include <xx.h>
#include “YY.h”
...

#endif
头文件都是有这些的
我重整了源码现在没有报错了
应该是项目文件混乱和写法不标准造成的
{:smile:}

RAMILE 发表于 2019-12-22 17:26:41

typedef struct{
unsigned char data1;
unsigned char data2;
}TEST_t;

TEST_t stTest;

这样比较科学,先定义类型,然后实例化,搞指针也比较方便

dr2001 发表于 2019-12-22 19:09:44

最通用的做法是
struct foo_s {
bla bla bla
};
typedef struct foo_s foo_t;

这样foo_s/t的类型都是struct foo_s。packed属性,GCC只能加在struct上。
而,typedef struct定义出来的xxx_t,类型是匿名的struct{}。

但是,这个和C++就不是太通用:不精确的说,C++的struct和typedef的命名空间是一个。

细节上说,有点区别。
页: [1]
查看完整版本: Typedef struct应该怎么写才是最标准的用法?