LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-20-2015, 12:17 PM   #1
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Rep: Reputation: 10
confusion on variables in c


I'm writing a program as an exercise out of a book. I keep getting an error that says "multiple definition of variable". I put the program w/ the compiler errors in a tar file and attached it.

Thanks for any help.

I'm having trouble attaching a file..can anyone point me in the right direction?

Last edited by doughyi8u; 04-20-2015 at 12:19 PM.
 
Old 04-20-2015, 12:33 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
How big is the program? If it's less than, say, a hundred lines you can just paste it between [code][/code] tags.
 
Old 04-20-2015, 12:43 PM   #3
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
Here's the pertinent files. Hopefullly this is enough.

Code:
#include "hotel.h" 


int
findfree()
{
	off_t offset;
	int j;
	ssize_t nread;

	for(j = 1; j <= NROOMS; j++)
	{
		offset = (j-1) * NAMELENGTH;
		if ( lseek(infile, offset, SEEK_SET) == -1) {
			perror("lseek");
			exit(1);
		} else {
			if ( (nread = read(infile, namebuf, NAMELENGTH)) <= 0) {
				perror("read?");
				exit(1);
			} else 
				namebuf[nread-1] = '\0';
		}
		if ( (isempty()) == 1) 
			return(j);

	}
	puts("No empty rooms");
	return -1;
}

#include "hotel.h"


int
freeroom(int roomno)
{
	off_t offset;
	ssize_t nread;

	offset = (roomno - 1) * NAMELENGTH;
	if ( lseek(infile, offset, SEEK_SET) == -1) {
		perror("lseek");
		exit(1);
	} else {
		if ( (nread = read(infile,namebuf, NAMELENGTH)) <= 0) {
			perror("read");
			exit(1);
		} else if (isempty())
				return -1;
		else {
			write(infile, empty, strlen(empty));
		}
	}
}

#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

#define NAMELENGTH 21
#define NROOMS 10

#define DBUG

char *getoccupier(int);
int openfile();
int findfree();
int freeroom(int);
int isempty(); 

extern int infile;
extern char namebuf[NAMELENGTH];
const char *empty = "EMPTY";

Here's the errors I get when I try to compile this:
/tmp/cclQs7ZG.o:(.data+0x0): multiple definition of `empty'
/tmp/cc4F45dN.o:(.data+0x0): first defined here
/tmp/ccuGRWWA.o:(.data+0x0): multiple definition of `empty'
/tmp/cc4F45dN.o:(.data+0x0): first defined here
/tmp/ccQ7Tq4u.o:(.data+0x0): multiple definition of `empty'
/tmp/cc4F45dN.o:(.data+0x0): first defined here
/tmp/ccevNsmp.o:(.data+0x0): multiple definition of `empty'
/tmp/cc4F45dN.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
Hope this is enough.
 
Old 04-20-2015, 12:49 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
You are defining (rather than just declaring) the variable empty in your .h file, then including that .h file in multiple .c files, causing the multiple definition error.

The correct way is to only declare the variable (using extern) in the .h file and define it in only one .c file.
 
Old 04-20-2015, 02:33 PM   #5
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
I'm having some trouble. I declared the variable in the .h file but am having trouble assigning to it.

hotel.h file:

Code:
extern const empty[6];
plain .c file:
Code:
empty = "EMPTY"; //this makes errors
How do I assign to this variable w/o errors?
 
1 members found this post helpful.
Old 04-20-2015, 02:41 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
hotel.h:
Code:
extern const char *empty;
main.c:
Code:
const char *empty = "EMPTY";
 
1 members found this post helpful.
Old 04-20-2015, 02:51 PM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by doughyi8u View Post
Code:
extern const empty[6];
In your .c file, you declared empty as char*. Here, however, you don't specify the type, so, depending on what standard you use, this will either default to int, or refuse to compile.

Quote:
Originally Posted by doughyi8u View Post
[code]
plain .c file:
Code:
empty = "EMPTY"; //this makes errors
You can only "assign" to an array in initialization (in the .c file where it is defined).
 
Old 04-20-2015, 02:54 PM   #8
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
I tried that and am getting the following error:

Code:
/tmp/ccp52ey8.o:(.data+0x0): multiple definition of `empty'
/tmp/ccQeNdUZ.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
doughy@zoe:~/sourcery/c/usp/ch2/2_9$ exit
 
Old 04-20-2015, 02:56 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Tried what?
The code I posted?

How exactly did you implement it?
That code I have listed for main.c should ONLY be in main.c, you should not re-declare the variable "empty" ANYWHERE else, just use it. Eg:

inc.h
Code:
#include <stdio.h>
extern const char *empty;
void other(void);
main.c
Code:
#include "inc.h"
const char *empty = "EMPTY";
int main(void) {
   printf("main: %s\n",empty);
   other();
   return 0;
}
other.c
Code:
#include "inc.h"
void other(void) {
   printf("other: %s\n",empty);
}
Code:
$ gcc -Wall *.c
$ ./a.out 
main: EMPTY
other: EMPTY

Last edited by suicidaleggroll; 04-20-2015 at 03:00 PM.
 
1 members found this post helpful.
Old 04-20-2015, 02:56 PM   #10
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
tried your posted code....I mistakingly declared it in two files hence the error. Now I'm getting a seg fault.

Last edited by doughyi8u; 04-20-2015 at 03:00 PM.
 
Old 04-20-2015, 03:02 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Compile with "-Wall -g -O0 -fbounds-check", then run in gdb and see if it sheds some light on where it's failing.
 
Old 04-20-2015, 03:12 PM   #12
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
I'm having trouble reading the compiler command...is that 0(zero)o?
 
Old 04-20-2015, 03:16 PM   #13
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
That's an O (a letter) followed by a zero.
 
Old 04-20-2015, 03:22 PM   #14
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
I'm clueless as I've never used gdb. Any advice?
 
Old 04-20-2015, 03:24 PM   #15
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
run
Code:
gdb programname
then
Code:
r
to run it inside the debugger, and when it crashes
Code:
bt
to get a backtrace.
 
  


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
Bash to find all placeholder variables in a file and replace will real variables spadez Programming 6 11-26-2013 12:24 PM
confusion about permanent env variables toma20082010 Linux - Newbie 3 03-10-2011 01:03 PM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 12:30 PM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM

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

All times are GMT -5. The time now is 07:38 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