1

I have an application, which is written on C. A new functionality I need to add in C++. I build C application with GCC. To add new functionality:

  1. To build an existing application with G++.

  2. Add new functionality.

When I tried to do (1.), I met following problem: In file dev_table.c defined array:

const type_name devices[] = {...};

in file stm.c I export this array by:

extern const type_name devices[];

And when I assign

stm->dev = devices;

linker fail.

If I build the same code by GCC, everything is OK and working well, but with G++ it fails.

If I remove const from both places(dev_table.c and stm.c), it is also working well, so the problem is resolved, but I do not understand what is the reason.

alk
  • 69,737
  • 10
  • 105
  • 255

1 Answers1

2

I suggest you take a look at this post

Um, since const s are implicitly static, you need an extern even on the a_global_var definition (in file.c). Without this, anything that includes file.h will not link because it is looking for a const int a_global_var with external linkage.

Or

You can use them together (extern and const). But you need to be consistent on your use of const because when C++ does name decoration, const is included in the type information that is used to decorate the symbol names. so extern const int i will refer to a different variable than extern int i Unless you use extern "C" {}. C name decoration doesn't pay attention to const.

Community
  • 1
  • 1
pandaman1234
  • 523
  • 7
  • 17