1

I'm new to the syntax of Makefiles and I'm not sure how can I properly link to boost library thread definitions.

In the .cpp file, I have this code:

boost::thread(&MessageBus::readBus, this, _bus).detach();

I have included following:

#include <boost/thread.hpp>

But while compiling I get the following error which seems to be because of linking issues:

../../../../bsl1/boost/boost/thread/detail/thread.hpp:255: undefined reference to `boost::thread::detach()'
Consolidator.o: In function `~thread_data':
../../../../bsl1/boost/boost/thread/detail/thread.hpp:93: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
../../../../bsl1/boost/boost/thread/detail/thread.hpp:93: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
Consolidator.o: In function `thread_data_base':
../../../../bsl1/boost/boost/thread/pthread/thread_data.hpp:144: undefined reference to `vtable for boost::detail::thread_data_base'
Consolidator.o: In function `thread_data':
../../../../external/bsl1/boost/boost/thread/detail/thread.hpp:111: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
Consolidator.o: In function `boost::thread::start_thread()':
../../../../bsl1/boost/boost/thread/detail/thread.hpp:180: undefined reference to `boost::thread::start_thread_noexcept()'

In Makefile, I have added boost lib in both CFLAGS and CXXFLAGS:

PROG = msgbusd

NO_STRIP = yes

.PATH: \
    ${.CURDIR}/server \

SRCS += \
    main.cpp \
    oc.cpp \
    UdpReceiver.cpp \
    ${OTHER_SRCS} \
    ${LIB_PROTOS_SRCS} \


CXXFLAGS += \
    -fpermissive \
    -I${SRCTOP_EXTERNAL}/bsl1/boost


CFLAGS += \
    -DBOOST_SYSTEM_NO_DEPRECATED \
    -fno-inline \
    -I${.CURDIR}/server \
    -I${.CURDIR}/lib/h \
    -I. \
    -I${SRCTOP_EXTERNAL}/bsl1/boost

DPLIBS += \
    ${LIBXSLT} \
    ${LIBISC} \
    ${LIBBSDXML} \
    ${LIBXML2} \
    ${LIBEVENT} \
    ${LIBPTHREAD} \
    ${LIBZ}

.include <bsd.prog.mk>

I'm developing in FreeBSD 6 and has gcc 4.2.

What other changes I need to do in order to be able to compile it successfully?

Already went through this thread, and didn't find anything specific to Makefile and Makefile syntax to include boost libraries

void
  • 338
  • 5
  • 19
  • `boost::thread` is one of the (few) boost libraries which are not header-only and requires -L/-l rules to the linker. Without seeing your Makefile I can't say how exactly you add those, but looks like you need to modify DPLIBS to include -L/-l rules for boost. By the way, why are you using `boost::thread` at all? Since C++11 (for 8 years now!) C++ has built-in threads. – SergeyA Mar 08 '19 at 15:20
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Alan Birtles Mar 08 '19 at 15:27
  • https://stackoverflow.com/questions/10029437/which-gcc-and-g-version-support-which-standard-of-c-and-c The gcc version that is available for use (to build the OS that is built on top of bsd6) doesn't support C++11 it seems. So, have to use `boost::thread`. As of now, my Makefile doesn't use the -L/-l syntax so I'm not sure how to do that..and what to include with these rules.. – void Mar 08 '19 at 15:31
  • @AlanBirtles, Already went through that thread, and didn't find anything specific to Makefile and Makefile syntax to include boost libraries. – void Mar 08 '19 at 15:33
  • 1
    Because there is nothing else to say than `-L${BoostPath} -lboost_thread`. – Matthieu Brucher Mar 08 '19 at 15:54
  • There is no specific makefile syntax for adding libraries to linkage. You just learn the GCC syntax and use it in the linkage recipe in makefile. We can't tell you exactly how because you have't posted the makefile. – Mike Kinghan Mar 08 '19 at 16:34
  • @MikeKinghan, updated the question with makefile content. I'm not sure where to add the boost specific entries.. – void Mar 08 '19 at 18:44

1 Answers1

1

This question is about FreeBSD Makefiles infrastructure. If you aren't developing your project as part of FreeBSD (I doubt you do, since FreeBSD 6 is ancient), you don't really need to poke around bsd.prog.mk. I'd suggest using modern build system for this task, like CMake.

However, if you really required to use plain BSD Makefiles, you should define LDFLAGS variable and pass -lboost_thread there.

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • I am, actually, developing my project as part of FreeBSD 6. We need to support this OS as some of our products run an OS who's base OS is FreeBSD 6. – void Mar 09 '19 at 08:59
  • But do you really need to constrain yourself to use only base system components (e.g. make)? If you switch to CMake, it would be still a build-time dependency. – arrowd Mar 09 '19 at 09:57
  • Btw, I added `LDFLAGS += -lboost_thread` in my Makefile and I get this error `../../../bin/ld: cannot find -lboost_thread` – void Mar 09 '19 at 10:15
  • You also need `-L/path/to/where/boost_thread_lirary/located`. – arrowd Mar 09 '19 at 11:22