C program multiple definition error during linking time


 
Thread Tools Search this Thread
Top Forums Programming C program multiple definition error during linking time
# 1  
Old 03-26-2013
C program multiple definition error during linking time

Hi,

I have the following files:

// file.h
HTML Code:
void foo();
int i = 5; // should be just declared as extern int i;
// file1.c
Code:
#include <stdio.h>
#include "file.h"

void foo() {
    i = 10;
    printf("%d\n", i);
}

// file2.c
Code:
#include <stdio.h>
#include "file.h"

int main() {
    foo();
    printf("%d\n", i);
}

When I compiled the above files using gcc compiler, I got the following linker error

gcc -c file1.c
gcc -c file2.c
gcc -o a.out file*.o // linker error: file2.o: multiple definition of 'i'

After searching in the google, I got below links where I found the solution
Solving multiple definition - C++ Forum
multiple definition of 'variable_name'
"multiple definition" error - C++ Forum

Now, I have changed the code in the file.h as:
Code:
extern int i;

Now, my question is, if I want to print the value of i before calling the foo() function in the file2.c, what should I do in order to not to end up in the linker error?

// file2.c
Code:
#include <stdio.h>
#include "file.h"

i = 20; // treats as an altogether a new definition of i, implicitly adds int keyword before i
int main() {
        i = 20; // multiple definition error
        printf("%d\n", i);
        foo();
        printf("%d\n", i);
}


Last edited by royalibrahim; 03-27-2013 at 02:54 AM..
# 2  
Old 03-26-2013
That implicit declaration looks very cagey to me... I'm not sure it's doing what you think it is. Do a full-fledged int i=20; outside main in file2.c.

You shouldn't need the 'i=20' in main anymore, since it will start at that value when the program's run anyway, but neither will it hurt anything.

Code:
#include <stdio.h>
#include "file.h"

void foo() {
    i = 10;
    printf("%d\n", i);
}

Code:
#include <stdio.h>
#include "file.h"

int i = 20; // treats as a altogether a new definition of i, implicitly adds int keyword before i
int main() {
        i = 20;
        printf("%d\n", i);
        foo();
        printf("%d\n", i);
}

# 3  
Old 03-26-2013
If you have the "extern int i" declaration in your file.h then I do not see why you would still get the multiple definition error...because the i in file2.c inside main is unrelated to the i in file1.c inside foo which in turn is unrelated to the global i that is defined in file2.c.

You have to look at the way the different variables are setup in the segments to understand the linkages between them. The global i defined in file2.c lies in the data segment and is unrelated to the local i inside main within the file2.c which lies on the stack frame created for main...similarly the local i inside foo of file1.c lies in the stack frame created for foo and is unrelated to the global i or the i that is local to main...let me know if this helps or if i have managed to confuse you even more Smilie
# 4  
Old 03-26-2013
Quote:
Originally Posted by shamrock
If you have the "extern int i" declaration in your file.h then I do not see why you would still get the multiple definition error...
I suspect the multiple definition errors became undefined external errors at some point...
# 5  
Old 03-26-2013
Quote:
Originally Posted by Corona688
I suspect the multiple definition errors became undefined external errors at some point...
Yes i too suspect that especially since the op hasnt been clear about the entire code...
# 6  
Old 03-26-2013
Quote:
Originally Posted by shamrock
You have to look at the way the different variables are setup in the segments to understand the linkages between them. The global i defined in file2.c lies in the data segment and is unrelated to the local i inside main within the file2.c which lies on the stack frame created for main...similarly the local i inside foo of file1.c lies in the stack frame created for foo and is unrelated to the global i or the i that is local to main...let me know if this helps or if i have managed to confuse you even more Smilie
Your analysis is incorrect. There are no versions of i which are local to a block (function or otherwise). All of those identifiers refer to the same externally-linked, global storage allocation.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 03-27-2013
Quote:
Originally Posted by alister
Your analysis is incorrect. There are no versions of i which are local to a block (function or otherwise). All of those identifiers refer to the same externally-linked, global storage allocation.

Regards,
Alister
Thanks alister, but when you say so then why in file2.c, the identifier 'i' is not referring to the same externally-linked, global storage allocation as how the file1.c does? that is,
Code:
#include <stdio.h>
#include "file.h"

int main() {
    printf("%d\n", i); // undefined reference to `_i'
    foo();
    printf("%d\n", i);
}

Why I have to allocate a memory for i (declaring an int i) in the global scope in file2.c to get rid of this problem?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Help with Linux linking error

Hi, Currently I am migrating codes from unix to Linux. When doing linking(ld) getting the below error. Error is :ld: invalid BFD target `--shared' Providing the script of the ld: ld -b -G -o lib/libatk.so ./src/atk_db.o ./src/atk_gcl.o ./src/atk_options.o ./src/atk_exception.o... (1 Reply)
Discussion started by: jrkar
1 Replies

2. Programming

C++ Linking Error: invalid DSO for symbol

I am on a FreeBSD system trying to build a piece of software that I wrote. I had built it in the past but recently reinstalled FreeBSD. I am not sure what is going on. The software depends on and wxgtk2.8. Using g++: $ make g++ -c `wx-config --cxxflags`... (9 Replies)
Discussion started by: AntumDeluge
9 Replies

3. UNIX for Advanced & Expert Users

error:- sh: error importing function definition for `module

Hi, We have installed linux6(RHEL) OS and installed datastage application on that. First time installation worked fine and our all services related to datastage was up and running. When we stopped the datastage and restarted its giving below error while restart:- ./uv -admin -start ... (0 Replies)
Discussion started by: prasson_ibm
0 Replies

4. Programming

Linking .so into C program

I have a C binary file(.so as extension) which is delivered by a product. How do i use this functionalities of this .so file in my C program? How can in link the .so to my C program? (1 Reply)
Discussion started by: vkca
1 Replies

5. Programming

GCC compile help - "multiple definition" when using -g

gcc -g -o MY_ELF test1.c test2.c -lm I get this error multiple times... /tmp/cc5TzMPo.o:(.data+0x0): multiple definition of 'XYZ' (1 Reply)
Discussion started by: dragonpoint
1 Replies

6. Programming

Control multiple program instances - open multiple files problem

Hello. This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine. So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files. My system (ubuntu 10.10), when I right click a file and I... (3 Replies)
Discussion started by: hakermania
3 Replies

7. Programming

Problems while linking Fortran program (-> undefined reference to...)

Hello, we are a group of students (mechanical engineering) who are trying to port UNIX-software to a PC Linux system during a study project. The first goals were achieved: compiling the Fortran code and creating object files. However, we encounter errors during the linking process. This is... (0 Replies)
Discussion started by: Dynamo
0 Replies

8. Shell Programming and Scripting

Shell program to accept multiple request at the same time

Hi, I got a script which sends the Email to the user based on certain variables received from Tivoli Server Monitoring 6.1. Now to keep track of the mails I have wrote a stored procedure in DB2 as we use DB2 UDB as back end which take the variables that were used to send the mail and store it... (3 Replies)
Discussion started by: tcskurra
3 Replies

9. UNIX for Dummies Questions & Answers

Link error while linking a shared library in unix

Getting the following error , ld: /opt/syncsort39/lib/libsyncsort.sl: Mismatched ABI. 64-bit PA shared library found in 32-bit link. Is there any difference in the ld options in opt file while linking a 64 bit shared library ? Or is the problem because we are trying to link both 32 bit and 64... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

10. Programming

Linking problem while linking to shared library

Hi I'm getting ld: fatal: option -h and building a dynamic executable are incompatible ld: fatal: Flags processing errors When I run ld -shared -L/usr/dt/lib -lDtSvc -o builtin.so Workspace.o after running gcc -fPIC -I/usr/X11R6/include -I/usr/dt/include -c Workspace.c I'm... (6 Replies)
Discussion started by: laho
6 Replies
Login or Register to Ask a Question