Deutsch English Français Italiano |
<657f1dd4$0$10079$426a74cc@news.free.fr> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!2.eu.feeder.erje.net!feeder.erje.net!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!cleanfeed1-b.proxad.net!nnrp2-2.free.fr!not-for-mail Newsgroups: fr.comp.lang.c From: Thomas Nemeth <tnemeth@free.fr.invalid> Subject: =?UTF-8?Q?D=C3=A9claration?= extern Organization: Discheveled Reply-To: Thomas Nemeth <tnemeth@free.fr> User-Agent: slrn/1.0.3 (Linux) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Date: 17 Dec 2023 16:12:04 GMT Lines: 74 Message-ID: <657f1dd4$0$10079$426a74cc@news.free.fr> NNTP-Posting-Date: 17 Dec 2023 17:12:04 CET NNTP-Posting-Host: 82.64.171.187 X-Trace: 1702829524 news-3.free.fr 10079 82.64.171.187:48498 X-Complaints-To: abuse@proxad.net Bytes: 3358 Bonjour à tous. Récemment je suis tombé sur une remarque d'un collègue à propos du mot-clé "extern". Il me soutenait qu'il faut le mettre sur les déclarations de fonctions exportées dans les headers. ie: Fichier .c : int toto(void) { return 0; } Fichier .h : extern int toto(void); Ayant conscience de la limite de mes connaissances relatives à ce mot-clé, je suis allé piocher dans la doc de GCC (puisqu'on compile avec)... En effet je préfère me baser sur une documentation officielle plutôt que sur stack overflow, bien que je lui reconnaisse une certaine utilité. Évidemment beaucoup de résultats de recherche correspondaient à mon usage habituel : déclarer une variable non statique -- et initialisée -- dans un module, et la déclarer extern dans les autres modules. Donc la doc de gcc(1) dit : « [...] extern is useful for declaring variables that you want to be visible to all source files that are linked into your project. [...] » Nulle part il n'est question de fonction. Cependant, dans la section "Scope", il est dit : « [...] A declared object can be visible only within a particular function, or within a particular file, or may be visible to an entire set of files by way of including header files and using extern declarations. Unless explicitly stated otherwise, declarations made at the top-level of a file (i.e., not within a function) are visible to the entire file, including from within functions, but are not visible outside of the file. [...] » De plus la documentation pour les fonctions statiques indique : « You can define a function to be static if you want it to be callable only within the source file where it is defined: [...] Functions which are defined in this way are said to have static linkage. [...] » Du coup je ne suis pas beaucoup plus avancé. Alors je suis allé regarder du côté des includes de la libc... et en effet il y a plein de fonctions déclarées comme "extern". L'un de vous aurait-il un pointeur vers une description réelle de "extern" ? Est-ce un mot-clé dont l'utilisation est compilo-dépendante ? (1) https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html Thomas. -- BOFH excuse #26: First Saturday after first full moon in Winter.