Search code examples
linuxboostreferenceundefinedlinux-mint

Cannot compile with makefile - undefined reference to `boost::re_detail


I got an assignment to improve running time of some code. The only problem is, I can't even compile it to run it on my machine in the first place. Every time I try, it stops somewhere in the midst of compiling saying this:

"undefined reference to `boost::re_detail::put_mem_block(void*)' collect2: ld returned 1 exit status make: * [cpu] Error 1"

This is how makefile looks:

SHELL = /bin/bash

OBJECTS = main.o atom.o molecule.o charges.o pdb.o asa.o amino.o chain.o addition.o string_operation.o pdb_peptide.o protein_chain.o residue_atom.o chain_residue.o residue_contact.o atom_grid.o circles.o atom_space_calculations.o

OBJDIR = obj

VPATH = src:src/ext:$(OBJDIR)

CFLAGS = -O3 -Wall -lm -lboost_regex -L/usr/local/boost/lib


HDIRS = src,src/ext,src/qt_redistributable, usr/lib, usr/local/lib, usr/local/lib/include/boost, /usr/local/lib/lib/
IOPTS = $(addprefix -I, $(HDIRS))

cpu : $(addprefix $(OBJDIR)/, $(OBJECTS) $(CPUOBJS))
    g++ $(CFLAGS) -o mcpu $^ 

$(OBJDIR)/%.o : %.cpp
    g++ $(CFLAGS) $(IOPTS) -c $< -o $@

clean : 
    rm obj/*.o $(PROG)

I'm using Linux Mint x64 and I have tried everything I googled out. Installed the whole boost library in usr/local/lib (for no obvious reason because it didn't help), tried to edit LD PATH (I'm very new to Linux and I have no idea if that went right) and lots of stuff, but this thing doesn't seem to go through. Any help appreciated.


Solution

  • One problem with your makefile happens when you link your program. As you can see in these questions with g++ the order of your arguments at link time is really important. You need to put your libraries after your object files. One easy way to solve would be separating your linker flags (LDFLAGS) from the compiler flags (CFLAGS), and then putting LDFLAGS after $^ (your object files) in the link command.

    CFLAGS = -O3 -Wall 
    
    LDFLAGS = -L/usr/local/boost/lib -lm -lboost_regex  
    
    cpu : $(addprefix $(OBJDIR)/, $(OBJECTS) $(CPUOBJS))
            g++ $(CFLAGS) -o mcpu $^ $(LDFLAGS)
    
    $(OBJDIR)/%.o : %.cpp
            g++ $(CFLAGS) $(IOPTS) -c $< -o $@