I have a problem with GCC. It's unable to find my global variable. I created a sample C++ project to isolate the problem:
a.cpp:
#include "b.h"
const char * const g_test = "blah blah";
int main(){
test();
return 0;
}
b.cpp:
#include <iostream>
#include "a.h"
using namespace std;
void test(){
cout << g_test;
}
a.h:
extern const char * const g_test;
b.h:
void test();
I compile it like this:
$ g++ -o a.o -c a.cpp
$ g++ -o b.o -c b.cpp
$ g++ -o test a.o b.o
b.o: In function `test()':
b.cpp:(.text+0x7): undefined reference to `g_test'
collect2: error: ld returned 1 exit status
Changing the order of object files in last command doesn't change anything.
Why does linker throw an error? I was expecting to just create an executable printing "blah blah", but somehow the error appears. I don't see any reason it should fail.