GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Apr-2005, 21:52
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough

Linker errors with multiple file progam


Hi,

I am trying to compile a program having multiple files. I have a common header file which i am including in 3 source files. Multiple inclusion is protected by wrapping the contents of .h file using "#ifndef"

For e.g. I have following in my header file

CPP / C++ / C Code:
#ifndef PPTPCLIENT_H
#define PPTPCLIENT_H

const char control_reply_result_codes[6][100] = {	
	" ",
	"Successful channel establishment",
	"General error -- Error Codeindicates the problem",
	"Command channel already exists",
	"Requester is not authorized to establish a command channel",
	"The protocol version of the requester is not supported"
};

const char general_error_codes[6][100] = { 
	"No General Error",
	"No Control Connection exits yet for this PAC-PNS pair",
	"Length is wrong or Magic Cookie value is incorrect",
	"One of the field values was out of range or reserved field was non-zero",
	"The Call ID is invalid in this context",
	"A generic vendor-specific error occured in the PAC"
};

const char stop_reason_codes[4][100] = { 
	" ",
	"General request to clear control connection",
	"Can't support peer's version of the protocol",
	"Requester is being shutdown"
};

const char og_result_code [8][150] = {	
	" ",
	"Call established with no errors",
	"Outgoing call not established for the reason indicated in error code",
	"Outgoing call failed due to no carrier detected",
	"Outgoing call failed due to detection of busy signal",
	"Outgoing Call failed due to lack of a dial tone",
	"Outgoing Call was not established within time allotted by PAC",
	"Outgoing Call administratively prohibited"
};
....../*Some other similar declarations */
#endif

How ever I am getting linker errors for multiple inclusion of above array and similar other arrays.

Quote:
gcc -g -o pptpclient pclientmain.c pclientfunction.c pptputils.c
/tmp/ccMMMtIa.o(.rodata+0x0): In function `printHelp':
/home/snoopy/pptp/pclientfunction.c:4: multiple definition of `control_reply_result_codes'
/tmp/cccyJsV6.o(.rodata+0x0):/home/snoopy/pptp/pclientmain.c:4: first defined here
/tmp/ccMMMtIa.o(.rodata+0x260): multiple definition of `general_error_codes'
/tmp/cccyJsV6.o(.rodata+0x260): first defined here
/tmp/ccMMMtIa.o(.rodata+0x4c0): multiple definition of `stop_reason_codes'
/tmp/cccyJsV6.o(.rodata+0x4c0): first defined here
/tmp/ccMMMtIa.o(.rodata+0x660): multiple definition of `og_result_code'
/tmp/cccyJsV6.o(.rodata+0x660): first defined here

Can some one help me to figure out where the problem is?

Thanks,
  #2  
Old 24-Apr-2005, 00:46
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by nkhambal
Hi,

I am trying to compile a program having multiple files. I have a common header file which i am including in 3 source files. Multiple inclusion is protected by wrapping the contents of .h file using "#ifndef"

For e.g. I have following in my header file

CPP / C++ / C Code:
#ifndef PPTPCLIENT_H
#define PPTPCLIENT_H

externalconst char control_reply_result_codes[6][100];
const char general_error_codes[6][100] = { 
	"No General Error",
	"No Control Connection exits yet for this PAC-PNS pair",
	"Length is wrong or Magic Cookie value is incorrect",
	"One of the field values was out of range or reserved field was non-zero",
	"The Call ID is invalid in this context",
	"A generic vendor-specific error occured in the PAC"
};

const char stop_reason_codes[4][100] = { 
	" ",
	"General request to clear control connection",
	"Can't support peer's version of the protocol",
	"Requester is being shutdown"
};

const char og_result_code [8][150] = {	
	" ",
	"Call established with no errors",
	"Outgoing call not established for the reason indicated in error code",
	"Outgoing call failed due to no carrier detected",
	"Outgoing call failed due to detection of busy signal",
	"Outgoing Call failed due to lack of a dial tone",
	"Outgoing Call was not established within time allotted by PAC",
	"Outgoing Call administratively prohibited"
};
....../*Some other similar declarations */
#endif

How ever I am getting linker errors for multiple inclusion of above array and similar other arrays.



Can some one help me to figure out where the problem is?

Thanks,

Never define data in your header file. They are for delarations only. Put the contents of your header file in one of the source files and change all definitions to externals:
CPP / C++ / C Code:
extern char control_reply_result_codes[6][100];
extern char general_error_codes[6][100];
...

The wrapping, called header guards only prevent inclusion in the same source file. They are used when you have a complicated set of headers, and the possibility of a header being including more than once because of nested headers (headers including headers which include more headers).

What you tried to do is prevent the multiple definitions in the other files. If it worked the way you expected, none of the variables would have been defined in 2 of the three files. You might as well just leave the .h file out.

What you in fact did is define the variables control_reply_result_codes, general_error_codes, et al in each source file. Complete with your string definitions.

The .h files should not "reserve variable space", only declare the variable exists. The source files reserve the space.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #3  
Old 24-Apr-2005, 02:37
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough

Thanks. That worked.


Hi Walt,

Thanks. That did the trick. It is my first attempt to write a program that spans more than 2 source files. Good I got things cleared now.

Much appricated.

Thanks,
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
Re: Programming Techniques WaltP C Programming Language 0 09-Mar-2004 23:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:16.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.