always necessary to link to pthread library

2,357 views
Skip to first unread message

deuberger

unread,
Aug 19, 2010, 1:13:50 PM8/19/10
to Google C++ Testing Framework
Hi, is it always necessary to link to the pthread library in addition
to gtest or gtest_main on linux systems that support it? Is there a
reason that the default makefile doesn't just link the pthread library
to the gtest library to avoid having to do this? Also, if it's not
necessary to compile the gtest libraries, why not make it only
necessary when actually using threads. I'm only running a simple test
using TEST() and EXPECT_EQ, so it seems like linking to pthread is
overkill. I would prefer to only have to link to pthread if we
actually use some of those features in Google Test. Thoughts?

Thanks.

- Daniel

P.S. I know that it's possible to edit the makeful to disable thread
support altogether, but I don't want to do that.

Vlad Losev

unread,
Aug 19, 2010, 8:46:37 PM8/19/10
to Google C++ Testing Framework
On Thu, Aug 19, 2010 at 10:13 AM, deuberger <daniel.n...@gmail.com> wrote:
Hi, is it always necessary to link to the pthread library in addition
to gtest or gtest_main on linux systems that support it?  Is there a
reason that the default makefile doesn't just link the pthread library
to the gtest library to avoid having to do this?  Also, if it's not
necessary to compile the gtest libraries, why not make it only
necessary when actually using threads.  I'm only running a simple test
using TEST() and EXPECT_EQ, so it seems like linking to pthread is
overkill.  I would prefer to only have to link to pthread if we
actually use some of those features in Google Test.  Thoughts?

You may run ./configure with --with-pthreads=no. Alternatively, use

CPPFLAGS='-DGTEST_HAS_PTHREAD=0' make ....
 
Thanks.

- Daniel

P.S.  I know that it's possible to edit the makeful to disable thread
support altogether, but I don't want to do that.

Cheers,
Vlad

deuberger

unread,
Aug 20, 2010, 10:36:27 AM8/20/10
to Google C++ Testing Framework
Thanks. Is there a way to compile with pthreads support enabled
without having to link every test binary to the pthread library?
That's what I'm doing now because even for very simple tests gcc
complains if I don't link to pthread.

Thanks.

Vlad Losev

unread,
Aug 20, 2010, 3:26:51 PM8/20/10
to Google C++ Testing Framework
Hi Daniel,

On Fri, Aug 20, 2010 at 7:36 AM, deuberger <daniel.n...@gmail.com> wrote:
Thanks.  Is there a way to compile with pthreads support enabled
without having to link every test binary to the pthread library?
That's what I'm doing now because even for very simple tests gcc
complains if I don't link to pthread.

Let us know if this patch helps:

--- Makefile.am (revision 447)
+++ Makefile.am (working copy)
@@ -175,7 +175,7 @@
 # Modifies compiler and linker flags for pthreads compatibility.
 if HAVE_PTHREADS
   AM_CXXFLAGS = @PTHREAD_CFLAGS@ -DGTEST_HAS_PTHREAD=1
-  AM_LIBS = @PTHREAD_LIBS@
+  GTEST_PTHREAD_LIBS = @PTHREAD_LIBS@
 else
   AM_CXXFLAGS = -DGTEST_HAS_PTHREAD=0
 endif
@@ -184,6 +184,7 @@
 lib_LTLIBRARIES = lib/libgtest.la lib/libgtest_main.la
 
 lib_libgtest_la_SOURCES = src/gtest-all.cc
+lib_libgtest_la_LIBADD = ${GTEST_PTHREAD_LIBS}
 
 pkginclude_HEADERS = include/gtest/gtest.h \
                      include/gtest/gtest-death-test.h \


Thanks.

On Aug 19, 8:46 pm, Vlad Losev <v...@losev.com> wrote:
> You may run ./configure with --with-pthreads=no. Alternatively, use
>
> CPPFLAGS='-DGTEST_HAS_PTHREAD=0' make ....

- Vlad

deuberger

unread,
Aug 24, 2010, 3:45:20 PM8/24/10
to Google C++ Testing Framework
Hi Vlad,

Thanks for the patch. I tried it, but still had to link to pthread.
Any other ideas? Don't worry about it too much though. I guess it's
not that big of a deal to link to pthreads. It does seem a bit
strange since we're not actually using it, but now that we're used to
it, it's okay.

Thanks.

- Daniel
Reply all
Reply to author
Forward
0 new messages