“undefined reference to” error

One of possible causes of this error is incorrect linking order of libraries using gcc/g++.

Linking libraries before object files causes the error:

g++ -o $(LIBS) $(PROGRAM) $(OBJS)

Linking libraries after object files solves the problem:

g++ -o $(PROGRAM) $(OBJS) $(LIBS)

This is a solution for static library linking. Shared libraries should link both ways. Also order of respective libraries is important. If library A uses library B, library B has to linked first (-lB -lA).


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s