2

Or I have two files, each with one global initialization. One depends on the other.

Simplified example:

file1.h:

#include <string>
extern const std::string PREFIX;

file1.cpp:

#include "file1.h"
const std::string PREFIX  = "prefix,";

file2.cpp:

#include "file1.h"
std::string MSG = PREFIX + "body";
int main(){}

I compile them as such:

/usr/local/bin/g++-4.6.2 -c -Wall -g -o file1.o file1.cpp
/usr/local/bin/g++-4.6.2 -c -Wall -g -o file2.o file2.cpp
/usr/local/bin/g++-4.6.2 -Wall -g  -o example file1.o file2.o

When I run this, it segfaults. gdb trace:

Starting program: example

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b7ae0b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) ()
  from /usr/local/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/libstdc++.so.6 
(gdb) bt
#0  0x00007ffff7b7ae0b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) ()
   from /usr/local/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/libstdc++.so.6
#1  0x00000000004009b5 in std::operator+<char, std::char_traits<char>, std::allocator<char> > (__lhs=Traceback (most recent call last):
   File "/usr/local/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../share/gcc-4.6.2/python/libstdcxx/v6/printers.py", line 587, in to_string
     return ptr.lazy_string (length = len)
RuntimeError: Cannot access memory at address 0xffffffffffffffe8
, __rhs=0x400af0 "body") at /usr/local/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/include/c++/bits/basic_string.h:2346
#2  0x000000000040095f in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at file2.cpp:3
#3  0x000000000040098b in _GLOBAL__sub_I_MSG () at file2.cpp:6
#4  0x0000000000400ab6 in __do_global_ctors_aux ()
#5  0x00000000004006e3 in _init ()
#6  0x00007ffff7ffa5a0 in ?? ()
#7  0x0000000000400a45 in __libc_csu_init ()
#8  0x00007ffff72dcbe0 in __libc_start_main () from /lib/libc.so.6
#9  0x00000000004007c9 in _start ()

Is there anyway to do what I'm trying to do (keeping in mind that this is an overly simplified example)? Or am I at the mercy of the initialization order chosen by the compiler/linker?

sshannin
  • 2,767
  • 1
  • 22
  • 25

3 Answers3

5

You need to make the prefix string be there before the other strings. One way is to change it to a C string

// header
#include <string>
extern const char * const PREFIX;

// .cpp file
const char * const PREFIX = "prefix,";

Another way is to return the prefix from a function and use PREFIX() where you previously used PREFIX.

// header
inline const string& PREFIX() { 
  static const string value = "prefix,";
  return value;
}

At last, just a hint: Names with all uppercase letters are used for macros only in most coding conventions, so I would avoid such names for variables and functions.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
0

I guess this appears because the code attempts to initialize your globals in the wrong order. There is a SO question about initialisation order, its answers should prove useful.

Community
  • 1
  • 1
MvG
  • 57,380
  • 22
  • 148
  • 276
0

It's obviously not a portable solution, but for gcc you can use function attributes - with the __attribute__ syntax. i.e., the __init_priority__ (priority) attribute. The initialization priorities cross translation units. link

Brett Hale
  • 21,653
  • 2
  • 61
  • 90