I am not able to understand the compilation errors below.
First file is a header, test_weak.h
:
#ifndef TEST_WEAK_H
#define TEST_WEAK_H
#ifndef __ASSEMBLER__
const char* const TUTU __attribute__((weak)) ="TUTU";
const char* TUTU_DATE __attribute__((weak)) = __DATE__;
const char* const tutu ="tutu";
#endif /*ASSEMBLER*/
#endif /*TEST_WEAK_H*/
Second file is the main test.cpp
:
int main ()
{
return 42;
}
To compile I run: g++ -include test_weak.h test.cpp -o test
Compilation result is :
In file included from <command-line>:0:0:
./test_weak.h:5:44: error: weak declaration of ‘TUTU’ must be public
I am able to run successfully this code by replacing cpp extension by c extension on test source file and using gcc instead of g++. I am also able to fix this error by removing the weak attribute or removing the second const. So yeah I am able to fix the compilation error but no to able to understand the reason of the problem here.
For example this line compile without trouble:
const char* TUTU __attribute__((weak)) ="TUTU";
Why I cannot use a const char* const
+ weak attribute with c++ ?