Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Outside of a dog, a book is man's best friend. Inside of a dog, it is too dark to read.


devel / comp.lang.c / Re: multiple definition problem

SubjectAuthor
* multiple definition problemstdcerr
+* Re: multiple definition problemstdcerr
|+- Re: multiple definition problemBonita Montero
|`- Re: multiple definition problemBart
+- Re: multiple definition problemTim Rentsch
`- Re: multiple definition problemJames Kuyper

1
multiple definition problem

<a5d83d90-29e3-44f6-9919-4760993ae40an@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16768&group=comp.lang.c#16768

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:1185:: with SMTP id m5mr6248130qtk.140.1622260852116;
Fri, 28 May 2021 21:00:52 -0700 (PDT)
X-Received: by 2002:ad4:5bef:: with SMTP id k15mr6168973qvc.22.1622260852002;
Fri, 28 May 2021 21:00:52 -0700 (PDT)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!usenet.pasdenom.info!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 28 May 2021 21:00:51 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=24.87.40.64; posting-account=ITQB8goAAACqwP7vVe7b-QwIWpcBL1Ll
NNTP-Posting-Host: 24.87.40.64
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a5d83d90-29e3-44f6-9919-4760993ae40an@googlegroups.com>
Subject: multiple definition problem
From: ron.egg...@gmail.com (stdcerr)
Injection-Date: Sat, 29 May 2021 04:00:52 +0000
Content-Type: text/plain; charset="UTF-8"
 by: stdcerr - Sat, 29 May 2021 04:00 UTC

Hi,

I have two source files call them a.c & b.c and a header file c.h.
In c.h I declare functions and I have a variable defined like "int my_var". (also tried "int my_var = 0") that should be accessible by both c files. In a.c & b.c I "import" the variable with "extern in my_var".
It compiles fine but upon linking I get errors like:
src.tcpip.proprietary.ftpclient.a.o:(.bss+0x1010): first defined here
src.tcpip.proprietary.ftpclient.b.o:(.bss+0x28): multiple definition of `my_var'
What do I have wrong here?

Re: multiple definition problem

<90421dcf-a7b0-4934-93d6-415fb620bf17n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16769&group=comp.lang.c#16769

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:7f83:: with SMTP id z3mr6160954qtj.239.1622261347659;
Fri, 28 May 2021 21:09:07 -0700 (PDT)
X-Received: by 2002:a05:620a:b09:: with SMTP id t9mr7224351qkg.17.1622261347517;
Fri, 28 May 2021 21:09:07 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mixmin.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 28 May 2021 21:09:07 -0700 (PDT)
In-Reply-To: <a5d83d90-29e3-44f6-9919-4760993ae40an@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=24.87.40.64; posting-account=ITQB8goAAACqwP7vVe7b-QwIWpcBL1Ll
NNTP-Posting-Host: 24.87.40.64
References: <a5d83d90-29e3-44f6-9919-4760993ae40an@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <90421dcf-a7b0-4934-93d6-415fb620bf17n@googlegroups.com>
Subject: Re: multiple definition problem
From: ron.egg...@gmail.com (stdcerr)
Injection-Date: Sat, 29 May 2021 04:09:07 +0000
Content-Type: text/plain; charset="UTF-8"
 by: stdcerr - Sat, 29 May 2021 04:09 UTC

On Friday, May 28, 2021 at 9:00:59 PM UTC-7, stdcerr wrote:
> Hi,
>
> I have two source files call them a.c & b.c and a header file c.h.
> In c.h I declare functions and I have a variable defined like "int my_var". (also tried "int my_var = 0") that should be accessible by both c files. In a.c & b.c I "import" the variable with "extern in my_var".
> It compiles fine but upon linking I get errors like:
> src.tcpip.proprietary.ftpclient.a.o:(.bss+0x1010): first defined here
> src.tcpip.proprietary.ftpclient.b.o:(.bss+0x28): multiple definition of `my_var'
> What do I have wrong here?
I followed up and built the following test application:
A.c
#include "C.h"

int main(void){
int a = 0;
printf("A.c a %d\n",a);
Myexp();
printf("A.c a %d\n",a);
return 0;
B.c

#include "C.h"

int Myexp(void){
extern int a;
a=100;
printf("B.c a %d\n",a);
}
C.h
#ifndef __C_H__
#define __C_H__
#include <stdio.h>
int a;
int Myexp(void);
#endif

And compilation would give me:
gcc -o Myexp A.c B.c
/usr/bin/ld: /tmp/cczogQht.o:(.bss+0x0): multiple definition of `a'; /tmp/ccVRKYfb.o:(.bss+0x0): first defined here

Re: multiple definition problem

<867djiduxf.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16770&group=comp.lang.c#16770

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: multiple definition problem
Date: Fri, 28 May 2021 21:29:32 -0700
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <867djiduxf.fsf@linuxsc.com>
References: <a5d83d90-29e3-44f6-9919-4760993ae40an@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="f62d6ce4c79955a1e8e1b504d80c2a29";
logging-data="9219"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/xWq4FdW2wt9OPnArg6UiFvu9vf7aTRJQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:t70r6oXtw00oCxq1+HxGK388RKM=
sha1:U+NTnb2tJxwn4Kk+jFslir4ZLGw=
 by: Tim Rentsch - Sat, 29 May 2021 04:29 UTC

stdcerr <ron.eggler@gmail.com> writes:

> I have two source files call them a.c & b.c and a header file c.h.
> In c.h I declare functions and I have a variable defined like "int
> my_var". (also tried "int my_var = 0") that should be accessible by
> both c files. In a.c & b.c I "import" the variable with "extern in
> my_var".
> It compiles fine but upon linking I get errors like:
> src.tcpip.proprietary.ftpclient.a.o:(.bss+0x1010): first defined here
> src.tcpip.proprietary.ftpclient.b.o:(.bss+0x28): multiple definition of `my_var'
> What do I have wrong here?

In .h files, shared variables should be declared using 'extern'.

Such variables then need to be defined in one, and only
one, .c file.

Example:

c.h:
#ifndef HAVE_c_h_HEADER
#define HAVE_c_h_HEADER

extern int shared_variable;

#endif

a.c:
#include "c.h"

...

b.c:
#include "c.h"

...

c.c:
#include "c.h"

int shared_variable; // no 'extern' => definition

Make sense?

Re: multiple definition problem

<s8sgun$doo$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16771&group=comp.lang.c#16771

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: multiple definition problem
Date: Sat, 29 May 2021 00:46:14 -0400
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <s8sgun$doo$1@dont-email.me>
References: <a5d83d90-29e3-44f6-9919-4760993ae40an@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 29 May 2021 04:46:15 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="60805a886b836fa9ffb175f29f7043e3";
logging-data="14104"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/NwfIxXFoAQZXweKttdJnlMBmBcmaPCBI="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.8.1
Cancel-Lock: sha1:igF8A4rmviimKEMBiwGwHl7Itew=
In-Reply-To: <a5d83d90-29e3-44f6-9919-4760993ae40an@googlegroups.com>
Content-Language: en-US
 by: James Kuyper - Sat, 29 May 2021 04:46 UTC

On 5/29/21 12:00 AM, stdcerr wrote:
>
> Hi,
>
> I have two source files call them a.c & b.c and a header file c.h.
> In c.h I declare functions and I have a variable defined like "int my_var". (also tried "int my_var = 0") that should be accessible by both c files. In a.c & b.c I "import" the variable with "extern in my_var".
> It compiles fine but upon linking I get errors like:
> src.tcpip.proprietary.ftpclient.a.o:(.bss+0x1010): first defined here
> src.tcpip.proprietary.ftpclient.b.o:(.bss+0x28): multiple definition of `my_var'
> What do I have wrong here?
>

The combination of a given source file and all of the other files
incorporated into it by #include is called a translation unit. In your
case, there's two translation units, one associated with a.c which
#includes the contents of c.h, and a second associated with b.c which
also #includes the contents of c.h

Because of the initializer, "int my_var=0;" is a definition of my_var.
Even without the initializer, it would qualify as a "tentative
definition" - if no other external definition occurs in the same
translation unit, it will be treated as an actual definition with an
implicit value of 0. Because you have such a definition in c.h, every
translation unit that #includes c.h will have such a definition.

If your program contains two or more external definitions of an
identifier, it has undefined behavior. Your program therefore runs afoul
of this rule.

"extern int my_var;" is only a declaration of my_var, not a definition.
You can (and should) have such a declaration in every file where my_var
is either defined or used. That is what you should have in c.h.

However, if your program makes any actual use of a object declared with
external linkage, it must have exactly one external definition of that
object. You could provide that definition by adding "int my_var=0;" to
either a.c or b.c, but not both.

However, as a matter of convention, rather than the rules of the
language, it would be more conventional for functions and objects with
external linkage that are declared in "c.h" to be defined in "c.c",
rather than either a.c or b.c.

Re: multiple definition problem

<s8sje1$khn$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16773&group=comp.lang.c#16773

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: multiple definition problem
Date: Sat, 29 May 2021 07:28:33 +0200
Organization: A noiseless patient Spider
Lines: 4
Message-ID: <s8sje1$khn$1@dont-email.me>
References: <a5d83d90-29e3-44f6-9919-4760993ae40an@googlegroups.com>
<90421dcf-a7b0-4934-93d6-415fb620bf17n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 29 May 2021 05:28:33 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="348548000a7b1a4378c3e02fcf9aa423";
logging-data="21047"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX182BlgDfZ8DVmNZxWzouqM1oj9mQ577cj4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.10.2
Cancel-Lock: sha1:zJ6Jn5YAklYEbickgrU7P/gDu0A=
In-Reply-To: <90421dcf-a7b0-4934-93d6-415fb620bf17n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Sat, 29 May 2021 05:28 UTC

> #ifndef __C_H__
> #define __C_H__

Use #pragma once - any relevant compiler supports it.

Re: multiple definition problem

<rDnsI.138504$2a1.110246@fx05.ams4>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16775&group=comp.lang.c#16775

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx05.ams4.POSTED!not-for-mail
Subject: Re: multiple definition problem
Newsgroups: comp.lang.c
References: <a5d83d90-29e3-44f6-9919-4760993ae40an@googlegroups.com>
<90421dcf-a7b0-4934-93d6-415fb620bf17n@googlegroups.com>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.10.0
MIME-Version: 1.0
In-Reply-To: <90421dcf-a7b0-4934-93d6-415fb620bf17n@googlegroups.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
X-Antivirus: AVG (VPS 210528-8, 28/05/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 80
Message-ID: <rDnsI.138504$2a1.110246@fx05.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Sat, 29 May 2021 09:16:07 UTC
Organization: virginmedia.com
Date: Sat, 29 May 2021 10:15:55 +0100
X-Received-Bytes: 2997
 by: Bart - Sat, 29 May 2021 09:15 UTC

On 29/05/2021 05:09, stdcerr wrote:
> On Friday, May 28, 2021 at 9:00:59 PM UTC-7, stdcerr wrote:
>> Hi,
>>
>> I have two source files call them a.c & b.c and a header file c.h.
>> In c.h I declare functions and I have a variable defined like "int my_var". (also tried "int my_var = 0") that should be accessible by both c files. In a.c & b.c I "import" the variable with "extern in my_var".
>> It compiles fine but upon linking I get errors like:
>> src.tcpip.proprietary.ftpclient.a.o:(.bss+0x1010): first defined here
>> src.tcpip.proprietary.ftpclient.b.o:(.bss+0x28): multiple definition of `my_var'
>> What do I have wrong here?
> I followed up and built the following test application:
> A.c
> #include "C.h"
>
> int main(void){
> int a = 0;
> printf("A.c a %d\n",a);
> Myexp();
> printf("A.c a %d\n",a);
> return 0;
> B.c
>
> #include "C.h"
>
> int Myexp(void){
> extern int a;
> a=100;
> printf("B.c a %d\n",a);
> }
> C.h
> #ifndef __C_H__
> #define __C_H__
> #include <stdio.h>
> int a;
> int Myexp(void);
> #endif
>
> And compilation would give me:
> gcc -o Myexp A.c B.c
> /usr/bin/ld: /tmp/cczogQht.o:(.bss+0x0): multiple definition of `a'; /tmp/ccVRKYfb.o:(.bss+0x0): first defined here
>

If you want 'a' to be shared between A and B, then declare the files
like this:

----------------------
A.c
----------------------

#include <stdio.h>
#include "C.h"

int a; // can optionally be initialised here

int main(void){
printf("A.c a %d\n",a);
Myexp();
printf("A.c a %d\n",a);
return 0;
}

----------------------
B.c
----------------------
#include <stdio.h>
#include "C.h"

void Myexp(void){ // ret value not used, not given anyway, use 'void'

a=100;
printf("B.c a %d\n",a);
}

----------------------
C.h
----------------------
extern int a;
extern void Myexp(void);


devel / comp.lang.c / Re: multiple definition problem

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor