简体   繁体   English

跨c ++转换单元的初始化

[英]Initialization across c++ translation units

Or I have two files, each with one global initialization. 或者我有两个文件,每个文件都有一个全局初始化。 One depends on the other. 一个取决于另一个。

Simplified example: 简化示例:

file1.h: file1.h:

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

file1.cpp: file1.cpp:

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

file2.cpp: 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: gdb跟踪:

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? 还是我受制于编译器/链接器选择的初始化顺序?

You need to make the prefix string be there before the other strings. 您需要在其他字符串之前添加前缀字符串。 One way is to change it to a C string 一种方法是将其更改为C字符串

// 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 . 另一种方法是从函数返回前缀,并使用先前使用PREFIX 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. 最后,只有一个提示:所有大写字母的名称仅在大多数编码约定中才用于宏,因此我将避免为变量和函数使用此类名称。

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. 关于初始化顺序有一个SO问题 ,它的答案应该很有用。

It's obviously not a portable solution, but for gcc you can use function attributes - with the __attribute__ syntax. 这显然不是便携式解决方案,但是对于gcc,您可以使用函数属性 -带有__attribute__语法。 ie, the __init_priority__ (priority) attribute. __init_priority__ (priority)属性。 The initialization priorities cross translation units. 初始化优先级跨翻译单元。 link 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM