C header file and extern


 
Thread Tools Search this Thread
Top Forums Programming C header file and extern
# 1  
Old 09-09-2013
C header file and extern

In the header file data.h i got:
Code:
const char ack_msg[] = "ack: received your msg\n";

In the code file server.c i got:
Code:
extern const char ack_msg[];

And else it is only used in a function call:
Code:
user$ grep ack_msg *c *h
server.c:extern const char ack_msg[]; 
server.c:    n = write(clientsfd, ack_msg, strlen(ack_msg)); 
data.h:const char ack_msg[] = "ack: received your msg\n"; 
user$

But i get this error when compiling:
Code:
user$ ls
checkr.c  checkr.h  client  client.c  data.h  server.c
user$ gcc checkr.c server.c -Wall -Wextra --pedantic -o server
/tmp/ccdKHyIr.o:(.rodata+0x0): multiple definition of `ack_msg'
/tmp/ccUnDvRc.o:(.rodata+0x0): first defined here
collect2: ld returned 1 exit status
user$

I got data.h included in server.c:
Code:
user$ grep data.h server.c 
#include "data.h"
user$

data.h:
Code:
#ifndef DATA_H
#define DATA_H
const char *progname;
#define BUF_SIZE 512
const char ack_msg[] = "ack: received your msg\n";
#endif

Thanks.
# 2  
Old 09-09-2013
You got it exactly backwards.

You should put this in the header file:

Code:
extern const char ack_msg[];


...and this in one and only one C file, call it "data.c" perhaps:

Code:
#include "data.h"

const char ack_msg[] = "ack: received your msg\n";

The string can be used in any C file which does #include "data.h"
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-09-2013
Thanks so much, it seems clear now. I got another question about header files. Say i got checkr.h with:
Code:
void check_argc(int, int);

and the function in checkr.c:
Code:
 void check_argc(int argcount, int n) {         if ( argcount != n ) {                 fprintf(stderr, "ERROR: wrong argument count\n");                 usage();                 exit(EXIT_FAILURE);         } }

From what you say i think i would need to use "extern" in checkr.h ? Can i also use it in the main file (called server.c) ? I know that i don't have to, but it helps me a bit to keep track of the functions without opening and closing files all the time (or using :vsplit). I think it has to do with definition and declaration? I read about it quite often, but never really understand it and usually forget it soon.
# 4  
Old 09-10-2013
Here is how extern works.

You have three C files. File1 declares the variable foo (we are just doing variables, functions work the same).

Code:
int foo;

File2 and File3 both use the variable. It "lives" in File1. So it is "extern"[al] to the scope of those two C files.

To reference foo and have the linker do its magic, each of File2 and File3 has to have an

Code:
extern int foo;

Somewhere. That somewhere can be directly in the code of both files: file2 and file3. It can also be in a header file that is common to both files.

Using
Code:
#define

operations you can make all 3 files: file1 file2 and file3 see it correctly. Just using myheader.h

myheader.h:
Code:
#ifndef MAINFILE
int foo;
#else
extern int foo;
#endif

File1.c:
Code:
// prevent File1.c from "seeing" the extern version of foo by defieing MAINFILE
#define MAINFILE
#include "myheader.h"

File2.c:
File3.c:
Code:
#include "myheader.h"

This is an example, not best practice necessarily.
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 09-10-2013
Thanks. Your explanation and example makes it more clear (also thanks for pointing out it may not be best praxis, but for now i just try to understand the general idea).

I think you say that: header files are (or can be ) used _instead of declaring the variable foo as extern in File2 and File3. foo is defined in File1 (and storage is set aside for it). File1 includes myheader.h too, and if foo is declared (File2 and File3) or defined (File1) is decided by the #ifndef Mainfile and #define Mainfile in File1.

I read in §The C Programming langauge"; (As "C A modern approach" was not clear on the subject) and in chapter 1 "External variables and scope" (page 33 for me) it says the following (or that is what i understand):
- a definition creates a variable and sets aside storage
- a declaration only announces the nature of a variable
- extern says that the definition is elsewhere and only declares
- header files include declarations, not definitions
Is both correct (What i understood of your post and what i understood of the book)?

I think i am getting closer to understand it. Let me rethink and read again.Really thanks.
# 6  
Old 09-12-2013
Quote:
Originally Posted by tornow
From what you say i think i would need to use "extern" in checkr.h ?
If you don't define a function's contents with { } right then and there, it'll be assumed to be extern. This is very unlike variables, which never assume they're external unless you say so.

extern used to be a bigger deal for functions when there were many methods of linking -- there could be several kinds of extern functions, and you'd use extern to tell the compiler which they are with extern dllexport functionname(); or what have you. Sometimes this is still important when mixing C with other languages, to guarantee your function calls are generated in a way foreign code can understand.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. Programming

Extern keyward on function in C

I saw a header (.h) file with mixture of "regular" function declarations and other extern function declarations. As I was told all function declarations are implicitly external and the extern on functions declarations is superfluous. Here my focus is on function declaration, not variable yet. int... (2 Replies)
Discussion started by: yifangt
2 Replies

3. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

4. Shell Programming and Scripting

Comparing one file header with another file header

Hi Experts, In our project we have requirement where in we have to compare header of one file with header in the parameter file. There are 20 files which we ftp from one site. All this files have different header. We are comapring this file with our parameter file(which is having the header... (2 Replies)
Discussion started by: Amey Joshi
2 Replies

5. Programming

segmentation fault for extern

Why this is happening when both of them compiled together and run? I am getting segmentation fault SIGSEGV. File1.c: int arr; File2.c: extern int *arr; int main() { arr = 100; } (3 Replies)
Discussion started by: royalibrahim
3 Replies

6. UNIX for Dummies Questions & Answers

fetchmail and forward to an extern address

Hi, I'm searching for an solution for the following problem. I want fetch some mails via pop3 from a@a.com with fetchmail. That works perfectly. Now any incoming mail should forwarded to b@b.com via smtp obv. But I don't know how to configure that. All online tutorials describe forwarding to... (0 Replies)
Discussion started by: mcW
0 Replies

7. Linux

Problem mounting extern hd (fedora 9)

Hi there, I'm having a bit of a strange problem which I would appreciate some help with. The Problem: I have two external hard drives, but I'm borrowing one off my parents to copy data too (one of mine, which is identical to theirs - WD MyBook 300g - is on its way out). Fedora 9 recognizes... (3 Replies)
Discussion started by: lasthidingplace
3 Replies

8. Programming

Extern variable.

file1.c int a1; int main() { a1 = 2; printf("\na1 = %d\n", a1); next(); printf("\na1 = %d\n", a1); next1(); printf("\na1 = %d\n", a1); } file2.c #include <stdio.h> int b1 = 0; void next(void) (1 Reply)
Discussion started by: Tanvirk
1 Replies

9. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies

10. Programming

extern for functions

Hi, Please let me know if the extern keyword is necessary for using functions which is defined and declared in another file and and used in a different file where both these files are linked together. thanks (8 Replies)
Discussion started by: naan
8 Replies
Login or Register to Ask a Question