#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 되어 진행되는지 확인할 필요가 있어보이는군요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.