LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2009, 02:36 PM   #1
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Rep: Reputation: 40
C prog, using extern in .h file


Hi group,
This seems like a simple problem but I can't figure it out.

I am trying to simplify the sharing of variables bewteen program modules. I have thse source files:
a.c:
Code:
#define MAIN
#include <a.h>
int main(){
  B = calloc(1,200);
  strcpy( B, "Hello" );
  printf("B: %s\n", B );
  SUB();
  printf("B: %s\n", B );
  return; }
b.c
Code:
#include <a.h>
int SUB(){
  strcpy( B, "Good bye" );
  return; }
a.h:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <ctype.h>
#include <errno.h>
int SUB();
#ifndef MAIN
extern "C" {
#endif

 char *B;

#ifndef MAIN
}
#endif
Where I compile the sources, I get this error:
Quote:
In file included from b.c:1:
./a.h:15: error: expected identifier or ‘(’ before string constant
b.c: In function ‘SUB’:
b.c:4: error: ‘B’ undeclared (first use in this function)
b.c:4: error: (Each undeclared identifier is reported only once
b.c:4: error: for each function it appears in.)
Any suggestion??

Thanks for your time.
 
Old 05-16-2009, 02:55 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MrUmunhum View Post
Hi group,
This seems like a simple problem but I can't figure it out.

I am trying to simplify the sharing of variables bewteen program modules. I have thse source files:
a.c:
Code:
#define MAIN
#include <a.h>
int main(){
  B = calloc(1,200);
  strcpy( B, "Hello" );
  printf("B: %s\n", B );
  SUB();
  printf("B: %s\n", B );
  return; }
b.c
Code:
#include <a.h>
int SUB(){
  strcpy( B, "Good bye" );
  return; }
a.h:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <ctype.h>
#include <errno.h>
int SUB();
#ifndef MAIN
extern "C" {
#endif

 char *B;

#ifndef MAIN
}
#endif
Where I compile the sources, I get this error:


Any suggestion??

Thanks for your time.
What suggestions do you expect regarding

Code:
./a.h:15: error: expected identifier or ‘(’ before string constant
?
 
Old 05-16-2009, 04:15 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
The immediate problem is that extern "C" is only valid when compiling using a C++ compiler (it indicates that the symbols should not be mangled). You're generally supposed to wrap the line (and its associated closing brace) with #ifdef __cplusplus. What you probably want is just "extern char *B".

However, there's a lot of style issues here. What are you trying to turn C into? Every file should ideally #include exactly what it directly depends on, no more and no less (a.h has that big giant mass of includes). Except for certain special cases, all header files should use a multiple-inclusion guard (a.h lacks one). All upper-case means that the it's a constant (SUB is a function). Though there's disagreement on where to put an opening brace, pretty much every widely used style puts a closing brace on its own line.
 
Old 05-16-2009, 08:01 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Code:
// a.h
#ifndef _A_H
#define _A_H

#ifdef __cplusplus
extern "C" {
#endif
int SUB();
#ifdef __cplusplus
}
#endif

extern char *B;

#endif /*_A_H */
Code:
// a.cpp
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "a.h"

char *B = NULL;

int 
main(int argc, char *argv[])
{
  B = (char *)calloc(1,200);
  strcpy (B, "Hello" );
  printf ("B: %s\n", B);
  SUB ();
  printf ("B: %s\n", B);
  return 0; 
}
Code:
// b.cpp
#include <string.h>
#include "a.h"

int 
SUB()
{
  strcpy (B, "Good bye");
  return 0; 
}
Some notes:
1. To "share a variable between modules", just:
a) Declare the "extern" (EXAMPLE: in "a.h")
b) Define the variable (EXAMPLE: in "a.cpp")

2. You don't need "#define MAIN". That used to be necessary in very, very old C compilers. Most modern compilers now allow you to have the "extern" declaration visible in the same file as your actual definition.

3. Your local header should be quoted:
Code:
//Good
#include "a.h"

//Poor
#include <a.h>
You should only use "<>" with system headers (like "<stdio.h>").

4. You should define a "guard" for each header (EXAMPLE: "#ifndef _A_H/#define _A_H/.../#endif")

5. Like tuxdev suggested, "extern "C" {}" is only needed if you plan on calling a subroutine from both C and C++. You should "guard" your extern "C" with "#ifdef __cplusplus", so it will compile in either/both environments.

6. If you define "int main()",then you need to "return" some integer (else you'll probably get a compiler error).

7. When you define your global pointer, "char *B", you should *always* initialize it to NULL.

8. You should only "#include" those headers actually needed by the source file (in contrast to Microsoft style, which likes to put everything under the sun into one big "stdfx.h" header). For example, in "a.h", you don't need *any* #include's.

'Hope that helps .. PSM

Last edited by paulsm4; 05-16-2009 at 08:16 PM.
 
Old 05-17-2009, 12:05 PM   #5
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by paulsm4 View Post
Some notes:
...
2. You don't need "#define MAIN". That used to be necessary in very, very old C compilers. Most modern compilers now allow you to have the "extern" declaration visible in the same file as your actual definition.
...
'Hope that helps .. PSM
This is the answer I needed, thanks.

I'm a very, very old programmer! Just very, very confused.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ prog on unix to convert PGM file into RGB singh.vikas85 Programming 2 05-14-2009 11:35 PM
Problem with extern struct object, in header file. Undefined type. RHLinuxGUY Programming 7 07-24-2006 12:01 AM
Should I Use The VI prog to change an File HooX Linux - Newbie 2 01-11-2005 01:52 AM
EXTERN.h: No such file or directory prj_7883 Linux - Networking 1 05-30-2004 11:26 PM
problem with extern file pointer in library linuxping Programming 5 02-08-2004 09:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration