0

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.

Obaid
  • 1,407
  • 19
  • 36
darxsys
  • 1,560
  • 4
  • 20
  • 34

2 Answers2

1

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 $@
Community
  • 1
  • 1
  • 2
    Standard practice goes a bit further by separating linker flags from libraries into `LDFLAGS` and `LDLIBS`. Same for preprocessor and compiler: `CPPFLAGS`, `CFLAGS` and `CXXFLAGS`. – Maxim Egorushkin Sep 20 '12 at 08:21
0

As can be seen in the Catalogue of Built-In Rules:

Linking a single object file

n is made automatically from n.o by running the linker (usually called ld) via the C compiler. The precise recipe used is:

$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)

and Variables Used by Implicit Rules:

LDFLAGS

Extra flags to give to compilers when they are supposed to invoke the linker, ld, such as -L. Libraries (-lfoo) should be added to the LDLIBS variable instead.

So in this case -lboost_regex should be set or added to LDLIBS, not LDFLAGS.