I have trouble with these static members initialization of c++ classes. See my code for more info.
Source
header.h
#ifndef HEADER_H
#define HEADER_H
#include <string>
using namespace std;
class Staff{ public: static string str;};
class Boss{ public: static string str;};
#endif
staff.cpp
#include "header.h"
string Staff::str = "(staff)";
boss.cpp
#include "header.h"
string Boss::str = "I call " + Staff::str;
main.cpp
#include <iostream>
#include "header.h"
int main(){cout << Boss::str << endl;}
And here are many compile codes with different results:
Pre-compile:
g++ -c boss.cpp
g++ -c staff.cpp
ar rcs lib.a boss.o staff.o
ar rcs rlib.a staff.o boss.o
Compile, run and result:
g++ main.cpp staff.cpp boss.cpp ; ./a.out
==> I call (staff)
g++ main.cpp boss.cpp staff.cpp ; ./a.out
==> segmentation fault (core dumped)
g++ main.cpp lib.a ; ./a.out
==> segmentation fault (core dumped)
g++ main.cpp rlib.a ; ./a.out
==>segmentation fault (core dumped)
I want to use library archive instead of confusing with giant objects order when compiling. Help me to solve them.