g++ linking libs instead of *.o containing static members leads to segfault

I observed segfaults if I used linking libraries instead of linking all the primary compiler output with g++. This problem occurs with libraries with classes with static members. And I don't find a solution with libs. What went wrong?

Simplified: I created two libraries called alpha and beta. Alpha contains a static member (race conditions were excluded). The library alpha depends on beta. And alpha and beta depend on a third library, the public library gamma (in my case Cairo).

The static member is defined as private within a class Alpha and access via getters and setters with mutex locks.

I compile my the source code for alpha

g++ -fPIC -DPIC `pkg-config --cflags gamma` alpha*.cpp -c
ar rcs alpha.a alpha*.o

and beta, respectively

g++ -fPIC -DPIC `pkg-config --cflags gamma` beta*.cpp -c
ar rcs beta.a beta*.o

Now, I want to use these libs (alpha, beta, and gamma) in my program delta. I tried two different ways to compile and link in two steps. First, compile and simply link the compiled objects:

g++ -fPIC -DPIC `pkg-config --cflags gamma` delta.cpp -c -o delta.o
g++ -fPIC -DPIC alpha*.o beta*.o delta.o `pkg-config --libs gamma` -o delta

This compiles, links and runs. Without any problems.

But if I want to use the .a libs instead and try:

g++ -fPIC -DPIC `pkg-config --cflags gamma` delta.cpp -c -o delta.o
g++ -fPIC -DPIC -lalpha -lbeta delta.o `pkg-config --libs gamma` -o delta

This compiles and links. But produces a segfault on running. During the very first access to the static member.



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation