Re: [LAD] G++ trouble

From: Fons Adriaensen <fons@email-addr-hidden>
Date: Mon Nov 16 2020 - 17:31:18 EET

On Mon, Nov 16, 2020 at 01:38:01PM +0000, Will J Godfrey wrote:
 
> I just did a search of the whole of src. It's *used* about a dozen times
> across 5 otherwise unrelated .cpp files, but is only defined here. Just to be
> certain, I did a make clean before trying this again, and it's definitely saying
> multiple defs

As I understand things,

   extern int x;
   extern const int x;

_declare_ x. That means it tells the compiler that x is _defined_ (as
below) somewhere else. It does not create x.

OTOH

   int x;
   int x = 123;
   const int x = 123;
   

_define_ x. That means that a variable 'x' is created. If this is done
at global scope (i.e. not inside a function or class), and there is more
than one such definition, you should get a 'multiple definition' error
from the linker. If there is no such definition, you should get an
'undefined' linker error if any compilation unit refers to x.

So you should have _exactly_ one definition (this is why definitions
should not be in a header file), and you can have as many declarations
as needed.

For things like your ADD_COLOUR, there are two approaches:

* define and initialise it as a 'const int' in single compilation unit
  (e.g. globals.cpp), and declare it as external in a header file (e.g.
  globals.h) that gets included by all other compilation units that need
  it.

or

* Just #define it in a header (e.g. globals.h) instead of creating
  a variable.

 
Ciao,

-- 
FA
_______________________________________________
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
https://lists.linuxaudio.org/listinfo/linux-audio-dev
Received on Tue Nov 17 04:15:02 2020

This archive was generated by hypermail 2.1.8 : Tue Nov 17 2020 - 04:15:02 EET