#ifndef 사용법에 대한 질문입니다.

superkkt의 이미지

#include <stdio.h>
#include <sys/types.h>

#ifndef	uint_t_define
typedef unsigned char	uint8_t;
typedef unsigned short	uint16_t;
typedef unsigned int	uint32_t;
#define	uint_t_define
#endif

struct ip_header {
	uint8_t		version_ihl;
	uint8_t		tos;
	uint16_t	len;
	uint16_t	ident;
	uint16_t	flags_fragment;
	uint8_t		ttl;
	uint8_t		protocol;
	uint16_t	cksum;
	uint32_t	src;
	uint32_t	dst;
};

struct tcp_pseudo_header {
	uint32_t	src;
	uint32_t	dst;
	uint8_t		not_use;
	uint8_t		protocol;
	uint16_t	len;
};

struct tcp_header {
	uint16_t	src_p;
	uint16_t 	dst_p;
	uint32_t	seq;
	uint32_t	ack;
	uint8_t		offset_reserve;
	uint8_t		ecn_control;
	uint16_t	window;
	uint16_t	cksum;
	uint16_t	urgent;
};


int
main(void)
{
	printf("ip: %d, tcp_pseudo: %d, tcp: %d\n", sizeof(struct ip_header), sizeof(struct tcp_pseudo_header), sizeof(struct tcp_header));
	
	return 0;
}

uint8_t, uint16_t 등이 정의가 되어있으면 컴파일 옵션을 아래와 같이해서 typedef의 중복 선언을 막도록 만들어봤습니다.

# gcc -Wall -Duint_t_define -o test test.c

근데 이렇게 에러가 나네요...

Quote:
a.c:12: error: parse error before "uint8_t"
a.c:12: warning: no semicolon at end of struct or union
a.c:13: warning: type defaults to `int' in declaration of `tos'
a.c:13: warning: data definition has no type or storage class
a.c:14: error: parse error before "len"
a.c:14: warning: type defaults to `int' in declaration of `len'
a.c:14: warning: data definition has no type or storage class

길어서 뒤에는 생략했습니다. 그런데 uint8_t 형이 선언이 안되어있는 시스템에서 gcc의 -D 옵션을 빼고 컴파일하면 잘됩니다.

제가 #ifndef 구문을 잘못 사용한건가요?

Prentice의 이미지

#ifdef와 #ifndef등은 전처리기를 위한 것이라서, 컴파일러를 위한 typedef 구문과 섞어쓰시는 것은 불가능하지 않을까 생각합니다.

ktd2004의 이미지

위 구문은 정상적이라고 생각합니다. 혹시 typedef로 되어 있지 않을까요?

pynoos의 이미지

-E 를 넣어 전처리한 것들만 비교해보세요.
uint8_t 가 선언되어있는 시스템이라고 해서 정말 사용할 때도 그것이 제대로 typedef 되어 진행되는지 확인할 필요가 있어보이는군요.