Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8210399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:03:48+00:00 2026-06-07T10:03:48+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:03:49+00:00Added an answer on June 7, 2026 at 10:03 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two files independant on each other. Let's just call it Class1 and
Greeting, I have two flash files myVideo1.swf and myVideo2.swf Each one contents FLVPLAYBack component
I would like to have two .css files. One for each master\content page and
Let's say I have two files, each one has a class. How can I
I have two files. File A has a list of words, one on each
Hi I have two text files that each contain different information about certain structures.The
i have two files:(localhost/template/) index.php template.php each time when i create an article(an article
I have two files A - nodes_to_delete and B - nodes_to_keep . Each file
I have two CSV files which use @ to divide each column. The first
I have two text files with various columns, each column is separated by a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.