我为Makefile使用以下结构:

OBJECTS := time.o
PROGRAM := time
CPPFLAGS += -Iusr/include/boost_1_49_0 -Lusr/include/boost_1_49_0/stage/lib
CXXFLAGS := -Wall -ggdb3
LDFLAGS += -lboost_date_time
$(PROGRAM) : $(OBJECTS)
g++ -o time time.cpp

clean :
rm $(PROGRAM) $(OBJECTS)


但是当我输入make时,它给了我很多错误-包括:

undefined reference to `boost::gregorian::greg_month::as_short_string() const' time.cpp:
(.text._ZN5boost9date_time15month_formatterINS_9gregorian10greg_monthENS0_13simple_formatIcEEcE12format_monthERKS3_RSo[boost::date_time::month_formatter<boost::gregorian::greg_month, boost::date_time::simple_format<char>, char>::format_month(boost::gregorian::greg_month const&, std::basic_ostream<char, std::char_traits<char> >&)]+0x56):
    undefined reference to `boost::gregorian::greg_month::as_long_string() const'
collect2: ld returned 1 exit status


当我使用g++ -Wall -Iusr/include/boost_1_49_0 -Lusr/include/boost_1_49_0/stage/lib -o time time.cpp -lboost_date_time时,代码可以正常编译,因此我的Makefile出了点问题。请帮忙。注意:我尝试将-L <directory>标志放置在LDFLAG变量中,但这也不起作用。

最佳答案

在您的Makefile中不使用LDFLAGS-既不直接也不通过内置规则隐式使用。以下应该工作:

$(PROGRAM): $(OBJECTS)
    $(CXX) $(LDFLAGS) $(TARGET_ARCH) $^ $(LOADLIBES) $(LDLIBS) -o $@


make是强大的工具。但是,使用它并不简单。如果您不被迫使用make,我建议您仔细看看替代方案。我个人建议使用bjam / boost-build。这是一个简单的Jamroot的示例:

project
    : requirements <cflags>-Wall <cflags>-ggdb3
    ;

using gcc ;

lib boost_date_time
    :
    : <search>/usr/include/boost_1_49_0/stage/lib
    :
    : <include>/usr/include/boost_1_49_0
    ;

exe time
    : time.cpp foo.cpp bar.cpp boost_date_time
    ;

关于c++ - 使文件无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10241867/

10-11 22:00
查看更多