2012-10-04 31 views
7

Hãy tha thứ cho tôi nếu điều này có vẻ là một câu hỏi đã được hỏi nhiều lần nhưng tôi đảm bảo với bạn điều này có một chút khác biệt.C: Việc sử dụng 'extern' trong các tệp tiêu đề là gì?

Tôi sử dụng Codeblocks cho lập trình C và gần đây tôi bắt đầu tự hỏi tại sao ai đó sử dụng tệp tiêu đề trong C. Tôi hiểu nó được sử dụng để khai báo và/hoặc xác định cấu trúc biến. Nhưng đây là một cái gì đó tôi đã cố gắng và bây giờ tôi đang bối rối.

Tôi có một tập tin tiêu đề tên

test1.h

#ifndef TEST1_H_INCLUDED 
#define TEST1_H_INCLUDED 

static int testvar = 233; 
extern int one; 
extern void show(); 

#endif // TEST1_H_INCLUDED 

và hai tập tin khác

headertest.c

#include"test1.h" 
#include<stdio.h> 

int one = 232; 

int main() 
{ 
    testvar = 12; 
    printf("The address of one is %p and value is %d",&testvar,testvar); 
    printf("value of one is %d",one); 
    show(); 
    return 0; 
} 

headertest2.c

#include "test1.h" 
#include<stdio.h> 

extern int one; 
extern void show() 
{ 
    //extern int one; 
    testvar = 34; 
    printf("\nThe address of one is %p and value is %d",&testvar, testvar); 
    printf("The value of one is %d", one); 
} 

Bây giờ quan điểm của tôi là ngay cả nếu tôi nhận xét ra việc kê khai extern int một; hiển thị void bên ngoài(); từ tệp tiêu đề test1.h Tôi không nhận được cảnh báo hoặc lỗi trong quá trình biên dịch và thực thi. Khi tôi có thể trực tiếp truy cập int và void show() từ headertest.c vì chúng có liên kết bên ngoài ngầm thì sử dụng tệp Header ở đây là gì? Tại sao một người nào đó khai báo một số biến hoặc chức năng như extern trong một tập tin tiêu đề mà nếu không có thể được truy cập trực tiếp !!

Cảm ơn bạn

+4

Tiêu đề câu hỏi của bạn dường như không khớp với nội dung câu hỏi của bạn. Bạn có thể làm rõ những gì bạn đang thực sự yêu cầu? Câu hỏi của bạn có liên quan đến tệp tiêu đề hoặc extern không? Thay vì chỉ đơn giản là nói "nếu tôi bình luận ra như vậy và như vậy", bạn có thể cung cấp ví dụ về những gì làm việc, những gì không, và những gì dường như làm việc tốt? Có lẽ chúng ta sẽ có thể thấy sự nhầm lẫn ở đâu. –

+0

Xin lỗi vì sự nhầm lẫn. Đoạn mã trên làm việc có và không có chú thích các khai báo trong tệp tiêu đề. Vì vậy, tôi có thể loại bỏ hoàn toàn tệp tiêu đề. Nó giống như vô dụng ở đây. Câu hỏi của tôi: Tôi đang làm điều gì đó sai coz header doesnt dường như được sử dụng bất kỳ? –

Trả lời

11

Tệp tiêu đề được sử dụng để bạn không lặp lại chính mình. Trong ví dụ của bạn, bạn không cần phải viết

extern int one; 

trong headertest2.c, vì nó sẽ được đưa vào tệp đó thông qua tệp tiêu đề.

Không lặp lại chính bạn không phải là điều nhỏ nhặt. Hãy tưởng tượng bạn có một trăm tệp sử dụng biến toàn cục này (one). Bạn có muốn viết extern int one trong tất cả trong số đó không? Điều gì xảy ra nếu có 20 trong số các biến này và 50 hàm khác? Nếu bạn muốn thay đổi int thành uint32_t thì sao?

Chắc chắn, các định nghĩa trùng lặp là tẻ nhạt và dễ bị lỗi.

Hãy xem ví dụ stdio.h. Nếu không có tiêu đề, bạn sẽ phải sao chép-dán đoạn mã sau vào mỗi tập tin mà muốn làm I/O:

/* Define ISO C stdio on top of C++ iostreams. 
    Copyright (C) 1991, 1994-2008, 2009, 2010 Free Software Foundation, Inc. 
    This file is part of the GNU C Library. 

    The GNU C Library is free software; you can redistribute it and/or 
    modify it under the terms of the GNU Lesser General Public 
    License as published by the Free Software Foundation; either 
    version 2.1 of the License, or (at your option) any later version. 

    The GNU C Library is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
    Lesser General Public License for more details. 

    You should have received a copy of the GNU Lesser General Public 
    License along with the GNU C Library; if not, write to the Free 
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 
    02111-1307 USA. */ 

/* 
* ISO C99 Standard: 7.19 Input/output <stdio.h> 
*/ 

#ifndef _STDIO_H 

#if !defined __need_FILE && !defined __need___FILE 
# define _STDIO_H 1 
# include <features.h> 

__BEGIN_DECLS 

# define __need_size_t 
# define __need_NULL 
# include <stddef.h> 

# include <bits/types.h> 
# define __need_FILE 
# define __need___FILE 
#endif /* Don't need FILE. */ 


#if !defined __FILE_defined && defined __need_FILE 

/* Define outside of namespace so the C++ is happy. */ 
struct _IO_FILE; 

__BEGIN_NAMESPACE_STD 
/* The opaque type of streams. This is the definition used elsewhere. */ 
typedef struct _IO_FILE FILE; 
__END_NAMESPACE_STD 
#if defined __USE_LARGEFILE64 || defined __USE_SVID || defined __USE_POSIX \ 
    || defined __USE_BSD || defined __USE_ISOC99 || defined __USE_XOPEN \ 
    || defined __USE_POSIX2 
__USING_NAMESPACE_STD(FILE) 
#endif 

# define __FILE_defined 1 
#endif /* FILE not defined. */ 
#undef __need_FILE 


#if !defined ____FILE_defined && defined __need___FILE 

/* The opaque type of streams. This is the definition used elsewhere. */ 
typedef struct _IO_FILE __FILE; 

# define ____FILE_defined 1 
#endif /* __FILE not defined. */ 
#undef __need___FILE 


#ifdef _STDIO_H 
#define _STDIO_USES_IOSTREAM 

#include <libio.h> 

#if defined __USE_XOPEN || defined __USE_XOPEN2K8 
# ifdef __GNUC__ 
# ifndef _VA_LIST_DEFINED 
typedef _G_va_list va_list; 
# define _VA_LIST_DEFINED 
# endif 
# else 
# include <stdarg.h> 
# endif 
#endif 

#ifdef __USE_XOPEN2K8 
# ifndef __off_t_defined 
# ifndef __USE_FILE_OFFSET64 
typedef __off_t off_t; 
# else 
typedef __off64_t off_t; 
# endif 
# define __off_t_defined 
# endif 
# if defined __USE_LARGEFILE64 && !defined __off64_t_defined 
typedef __off64_t off64_t; 
# define __off64_t_defined 
# endif 

# ifndef __ssize_t_defined 
typedef __ssize_t ssize_t; 
# define __ssize_t_defined 
# endif 
#endif 

/* The type of the second argument to `fgetpos' and `fsetpos'. */ 
__BEGIN_NAMESPACE_STD 
#ifndef __USE_FILE_OFFSET64 
typedef _G_fpos_t fpos_t; 
#else 
typedef _G_fpos64_t fpos_t; 
#endif 
__END_NAMESPACE_STD 
#ifdef __USE_LARGEFILE64 
typedef _G_fpos64_t fpos64_t; 
#endif 

/* The possibilities for the third argument to `setvbuf'. */ 
#define _IOFBF 0  /* Fully buffered. */ 
#define _IOLBF 1  /* Line buffered. */ 
#define _IONBF 2  /* No buffering. */ 


/* Default buffer size. */ 
#ifndef BUFSIZ 
# define BUFSIZ _IO_BUFSIZ 
#endif 


/* End of file character. 
    Some things throughout the library rely on this being -1. */ 
#ifndef EOF 
# define EOF (-1) 
#endif 


/* The possibilities for the third argument to `fseek'. 
    These values should not be changed. */ 
#define SEEK_SET 0 /* Seek from beginning of file. */ 
#define SEEK_CUR 1 /* Seek from current position. */ 
#define SEEK_END 2 /* Seek from end of file. */ 


#if defined __USE_SVID || defined __USE_XOPEN 
/* Default path prefix for `tempnam' and `tmpnam'. */ 
# define P_tmpdir "/tmp" 
#endif 


/* Get the values: 
    L_tmpnam How long an array of chars must be to be passed to `tmpnam'. 
    TMP_MAX The minimum number of unique filenames generated by tmpnam 
     (and tempnam when it uses tmpnam's name space), 
     or tempnam (the two are separate). 
    L_ctermid How long an array to pass to `ctermid'. 
    L_cuserid How long an array to pass to `cuserid'. 
    FOPEN_MAX Minimum number of files that can be open at once. 
    FILENAME_MAX Maximum length of a filename. */ 
#include <bits/stdio_lim.h> 


/* Standard streams. */ 
extern struct _IO_FILE *stdin;  /* Standard input stream. */ 
extern struct _IO_FILE *stdout;  /* Standard output stream. */ 
extern struct _IO_FILE *stderr;  /* Standard error output stream. */ 
/* C89/C99 say they're macros. Make them happy. */ 
#define stdin stdin 
#define stdout stdout 
#define stderr stderr 

__BEGIN_NAMESPACE_STD 
/* Remove file FILENAME. */ 
extern int remove (__const char *__filename) __THROW; 
/* Rename file OLD to NEW. */ 
extern int rename (__const char *__old, __const char *__new) __THROW; 
__END_NAMESPACE_STD 

#ifdef __USE_ATFILE 
/* Rename file OLD relative to OLDFD to NEW relative to NEWFD. */ 
extern int renameat (int __oldfd, __const char *__old, int __newfd, 
      __const char *__new) __THROW; 
#endif 

__BEGIN_NAMESPACE_STD 
/* Create a temporary file and open it read/write. 

    This function is a possible cancellation points and therefore not 
    marked with __THROW. */ 
#ifndef __USE_FILE_OFFSET64 
extern FILE *tmpfile (void) __wur; 
#else 
# ifdef __REDIRECT 
extern FILE *__REDIRECT (tmpfile, (void), tmpfile64) __wur; 
# else 
# define tmpfile tmpfile64 
# endif 
#endif 

#ifdef __USE_LARGEFILE64 
extern FILE *tmpfile64 (void) __wur; 
#endif 

/* Generate a temporary filename. */ 
extern char *tmpnam (char *__s) __THROW __wur; 
__END_NAMESPACE_STD 

#ifdef __USE_MISC 
/* This is the reentrant variant of `tmpnam'. The only difference is 
    that it does not allow S to be NULL. */ 
extern char *tmpnam_r (char *__s) __THROW __wur; 
#endif 




#error Omitted due to post length limit 




__BEGIN_NAMESPACE_STD 
#ifndef __USE_FILE_OFFSET64 
/* Get STREAM's position. 

    This function is a possible cancellation point and therefore not 
    marked with __THROW. */ 
extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos); 
/* Set STREAM's position. 

    This function is a possible cancellation point and therefore not 
    marked with __THROW. */ 
extern int fsetpos (FILE *__stream, __const fpos_t *__pos); 
#else 
# ifdef __REDIRECT 
extern int __REDIRECT (fgetpos, (FILE *__restrict __stream, 
       fpos_t *__restrict __pos), fgetpos64); 
extern int __REDIRECT (fsetpos, 
       (FILE *__stream, __const fpos_t *__pos), fsetpos64); 
# else 
# define fgetpos fgetpos64 
# define fsetpos fsetpos64 
# endif 
#endif 
__END_NAMESPACE_STD 

#ifdef __USE_LARGEFILE64 
extern int fseeko64 (FILE *__stream, __off64_t __off, int __whence); 
extern __off64_t ftello64 (FILE *__stream) __wur; 
extern int fgetpos64 (FILE *__restrict __stream, fpos64_t *__restrict __pos); 
extern int fsetpos64 (FILE *__stream, __const fpos64_t *__pos); 
#endif 

__BEGIN_NAMESPACE_STD 
/* Clear the error and EOF indicators for STREAM. */ 
extern void clearerr (FILE *__stream) __THROW; 
/* Return the EOF indicator for STREAM. */ 
extern int feof (FILE *__stream) __THROW __wur; 
/* Return the error indicator for STREAM. */ 
extern int ferror (FILE *__stream) __THROW __wur; 
__END_NAMESPACE_STD 

#ifdef __USE_MISC 
/* Faster versions when locking is not required. */ 
extern void clearerr_unlocked (FILE *__stream) __THROW; 
extern int feof_unlocked (FILE *__stream) __THROW __wur; 
extern int ferror_unlocked (FILE *__stream) __THROW __wur; 
#endif 


__BEGIN_NAMESPACE_STD 
/* Print a message describing the meaning of the value of errno. 

    This function is a possible cancellation point and therefore not 
    marked with __THROW. */ 
extern void perror (__const char *__s); 
__END_NAMESPACE_STD 

/* Provide the declarations for `sys_errlist' and `sys_nerr' if they 
    are available on this system. Even if available, these variables 
    should not be used directly. The `strerror' function provides 
    all the necessary functionality. */ 
#include <bits/sys_errlist.h> 


#ifdef __USE_POSIX 
/* Return the system file descriptor for STREAM. */ 
extern int fileno (FILE *__stream) __THROW __wur; 
#endif /* Use POSIX. */ 

#ifdef __USE_MISC 
/* Faster version when locking is not required. */ 
extern int fileno_unlocked (FILE *__stream) __THROW __wur; 
#endif 


#if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \ 
    defined __USE_MISC) 
/* Create a new stream connected to a pipe running the given command. 

    This function is a possible cancellation point and therefore not 
    marked with __THROW. */ 
extern FILE *popen (__const char *__command, __const char *__modes) __wur; 

/* Close a stream opened by popen and return the status of its child. 

    This function is a possible cancellation point and therefore not 
    marked with __THROW. */ 
extern int pclose (FILE *__stream); 
#endif 


#ifdef __USE_POSIX 
/* Return the name of the controlling terminal. */ 
extern char *ctermid (char *__s) __THROW; 
#endif /* Use POSIX. */ 


#ifdef __USE_XOPEN 
/* Return the name of the current user. */ 
extern char *cuserid (char *__s); 
#endif /* Use X/Open, but not issue 6. */ 


#ifdef __USE_GNU 
struct obstack;   /* See <obstack.h>. */ 

/* Write formatted output to an obstack. */ 
extern int obstack_printf (struct obstack *__restrict __obstack, 
       __const char *__restrict __format, ...) 
    __THROW __attribute__ ((__format__ (__printf__, 2, 3))); 
extern int obstack_vprintf (struct obstack *__restrict __obstack, 
       __const char *__restrict __format, 
       _G_va_list __args) 
    __THROW __attribute__ ((__format__ (__printf__, 2, 0))); 
#endif /* Use GNU. */ 


#if defined __USE_POSIX || defined __USE_MISC 
/* These are defined in POSIX.1:1996. */ 

/* Acquire ownership of STREAM. */ 
extern void flockfile (FILE *__stream) __THROW; 

/* Try to acquire ownership of STREAM but do not block if it is not 
    possible. */ 
extern int ftrylockfile (FILE *__stream) __THROW __wur; 

/* Relinquish the ownership granted for STREAM. */ 
extern void funlockfile (FILE *__stream) __THROW; 
#endif /* POSIX || misc */ 

#if defined __USE_XOPEN && !defined __USE_XOPEN2K && !defined __USE_GNU 
/* The X/Open standard requires some functions and variables to be 
    declared here which do not belong into this header. But we have to 
    follow. In GNU mode we don't do this nonsense. */ 
# define __need_getopt 
# include <getopt.h> 
#endif /* X/Open, but not issue 6 and not for GNU. */ 

/* If we are compiling with optimizing read this file. It contains 
    several optimizing inline functions and macros. */ 
#ifdef __USE_EXTERN_INLINES 
# include <bits/stdio.h> 
#endif 
#if __USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline 
# include <bits/stdio2.h> 
#endif 
#ifdef __LDBL_COMPAT 
# include <bits/stdio-ldbl.h> 
#endif 

__END_DECLS 

#endif /* <stdio.h> included. */ 

#endif /* !_STDIO_H */ 

Đó, là lý do tại sao chúng tôi đã tập tin tiêu đề.

+0

Lưu ý rằng mã ở trên không hoàn thành vì nó làm cho bài đăng tràn quá giới hạn ký tự 30000 của nó. – Shahbaz

+0

Cảm ơn tôi bắt đầu nhận được nó. Nó không quan trọng với chương trình đơn giản của tôi, nhưng chắc chắn sẽ xảy ra nếu chúng ta đối phó với 100 tập tin. Cảm ơn nhiều. –

+1

@YogenderSingh. Chính xác. Nói chung, nếu bạn phải viết một cái gì đó hai lần, bạn đang làm điều gì đó sai trái. Có lẽ bạn cần phải đặt một số mã trong một hàm, viết một macro hoặc typedef một cái gì đó. Khai báo một biến extern là một ví dụ như vậy, nếu bạn phải viết nó trong hai tệp, thì bạn nên chuyển nó sang một tiêu đề. – Shahbaz

3

Cung cấp tờ khai chức năng trong một tiêu đề ít nhất sẽ bảo vệ bạn khỏi những lỗi ngớ ngẩn như qua các đối số của loại tương thích.

Hơn nữa, tiêu đề được sử dụng không chỉ cho khai báo chức năng. Có gì về các định nghĩa struct/enum, toàn cầu typedef s, macro, hàm nội tuyến, v.v ...?

Cuối cùng, tiêu đề có thể được coi là API bên ngoài cho mô-đun của bạn. Người ta có thể nhanh chóng xem xét một tiêu đề để hiểu cách sử dụng nó mà không phải đối phó với việc triển khai (đôi khi không có sẵn).

1

Tệp tiêu đề C là cách để chia sẻ con trỏ chung, macro (#define ...), các loại cấu trúc chung được khai báo là cấu trúc chưa được đặt hoặc typedef. Một trong những cách sử dụng lớn nhất của một tệp tiêu đề là chia sẻ các khai báo hàm trên các mô-đun C.

Các vấn đề liên quan