1

I'd like to use boost regex library and created a very short program to test my makefile

#include <iostream>
#include <boost/regex.hpp>

using namespace std;
using namespace boost;

int main() {

    regex exp("test");

    cout << "Hello World" << endl;
}

Here is my makefile (I've got the boost includes from this thread Including boost libraries in make files)

EXEC = main
SOURCES = $(wildcard *.cpp)
HEADERS = $(wildcard *.h*)
OBJECTS = $(SOURCES:.cpp=.o)

all: $(EXEC)

main: $(OBJECTS)
    g++ -L/usr/local/Cellar/boost/1.54.0/lib -lboost_filesystem-mt -lboost_thread-mt $(OBJECTS) -o $(EXEC)

%.o: %.cpp $(HEADERS)
    g++ -I/usr/local/Cellar/boost/1.54.0/include -c $< -o $@

clean:
    rm -f $(EXEC) $(OBJECTS)

When I compile my program a get the following error message:

g++ -I/usr/local/Cellar/boost/1.54.0/include -c main.cpp -o main.o
g++ -L/usr/local/Cellar/boost/1.54.0/lib -lboost_filesystem-mt -lboost_thread-mt main.o -o main
Undefined symbols for architecture x86_64:
  "boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)", referenced from:
      boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [main] Error 1

What is missing? I've installed boost with homebrew under Mac OS X 10.8.

4

1 回答 1

3

您缺少指向 boost_regex_mt 库的链接

-lboost_filesystem-mt -lboost_thread-mt

在你的makefile...

main: $(OBJECTS)
    g++ -L/usr/local/Cellar/boost/1.54.0/lib -lboost_regex-mt -lboost_filesystem-mt -lboost_thread-mt     $(OBJECTS) -o $(EXEC)
于 2013-08-26T19:01:26.477 回答