ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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
[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).
/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
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.