linking produces pthread issues

3,185 views
Skip to first unread message

briandkid

unread,
Oct 21, 2011, 9:39:31 PM10/21/11
to Google C++ Testing Framework
I upgraded to Ubuntu 11.10 and I tried linking the google test to my
test suite. It builds alright until it fails while outlining a bunch
of undefined references to the pthreads.

I then retried with one of the samples which produced the same output.
Here it is:

g++ -I../include -g -Wall -Wextra -lpthread sample1.o
sample1_unittest.o gtest_main.a -o sample1_unittest
gtest_main.a(gtest-all.o): In function `~ThreadLocal':
/home/briand/Code/Source/googletest-read-only/make/../include/gtest/
internal/gtest-port.h:1353: undefined reference to
`pthread_getspecific'
/home/briand/Code/Source/googletest-read-only/make/../include/gtest/
internal/gtest-port.h:1357: undefined reference to
`pthread_key_delete'
/home/briand/Code/Source/googletest-read-only/make/../include/gtest/
internal/gtest-port.h:1353: undefined reference to
`pthread_getspecific'
/home/briand/Code/Source/googletest-read-only/make/../include/gtest/
internal/gtest-port.h:1357: undefined reference to
`pthread_key_delete'
gtest_main.a(gtest-all.o): In function
`testing::internal::ThreadLocal<std::vector<testing::internal::TraceInfo,
std::allocator<testing::internal::TraceInfo> > >::GetOrCreateValue()
const':
/home/briand/Code/Source/googletest-read-only/make/../include/gtest/
internal/gtest-port.h:1389: undefined reference to
`pthread_getspecific'
/home/briand/Code/Source/googletest-read-only/make/../include/gtest/
internal/gtest-port.h:1396: undefined reference to
`pthread_setspecific'
gtest_main.a(gtest-all.o): In function
`testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::CreateKey()':
/home/briand/Code/Source/googletest-read-only/make/../include/gtest/
internal/gtest-port.h:1382: undefined reference to
`pthread_key_create'
gtest_main.a(gtest-all.o): In function
`testing::internal::ThreadLocal<std::vector<testing::internal::TraceInfo,
std::allocator<testing::internal::TraceInfo> > >::CreateKey()':
/home/briand/Code/Source/googletest-read-only/make/../include/gtest/
internal/gtest-port.h:1382: undefined reference to
`pthread_key_create'
gtest_main.a(gtest-all.o): In function
`testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::GetOrCreateValue()
const':
/home/briand/Code/Source/googletest-read-only/make/../include/gtest/
internal/gtest-port.h:1389: undefined reference to
`pthread_getspecific'
/home/briand/Code/Source/googletest-read-only/make/../include/gtest/
internal/gtest-port.h:1396: undefined reference to
`pthread_setspecific'
collect2: ld returned 1 exit status
make: *** [sample1_unittest] Error 1

Greg Miller

unread,
Oct 22, 2011, 11:42:08 AM10/22/11
to briandkid, Google C++ Testing Framework
Does it work if you use the provided sample make file?

cd ${GTEST_DIR}/make
make
./sample1_unittest

G~

Greg Miller

unread,
Oct 22, 2011, 8:13:55 PM10/22/11
to briandkid, Google C++ Testing Framework
I saw some references online to a recent version of Ubuntu that changed the location of some system libraries. It said that things moved to something like /usr/lib/$(uname -m)-linux-gnu/ or something. I suggest running [find /lib /usr/lib -name '*pthread*'] to find the location of your pthread library. Then make sure that directory is included in your library search path to g++ with the -L option. Maybe something like the following:

g++ -I../include -g -Wall -Wextra -L/usr/lib/$(uname -m)-linux-gnu -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest

It should be easy to add this flag to CXXFLAGS in the provided Makefile to test. Sorry I don't have an Ubuntu machine to test this on.

G~



On Fri, Oct 21, 2011 at 9:39 PM, briandkid <bria...@gmail.com> wrote:

briandkid

unread,
Oct 27, 2011, 7:44:35 PM10/27/11
to Google C++ Testing Framework
Still does not work. Linking still fails

On Oct 22, 8:13 pm, Greg Miller <j...@google.com> wrote:
> I saw some references online to a recent version of Ubuntu that changed the
> location of some system libraries. It said that things moved to something
> like /usr/lib/$(uname -m)-linux-gnu/ or something. I suggest running [find
> /lib /usr/lib -name '*pthread*'] to find the location of your pthread
> library. Then make sure that directory is included in your library search
> path to g++ with the -L option. Maybe something like the following:
>
> g++ -I../include -g -Wall -Wextra *-L/usr/lib/$(uname
> -m)-linux-gnu*-lpthread sample1.o sample1_unittest.o gtest_main.a -o
> sample1_unittest
>
> It should be easy to add this flag to CXXFLAGS in the provided
> Makefile<http://code.google.com/p/googletest/source/browse/trunk/make/Makefile>to

briandkid

unread,
Nov 20, 2011, 10:40:57 PM11/20/11
to Google C++ Testing Framework
Found the solution. It seems you have to use -pthread instead of -
lpthread (from sample).
Thanks though. Seems that this should be a patch.

On Oct 27, 6:44 pm, briandkid <briand...@gmail.com> wrote:
> Still does not work. Linking still fails
>
> On Oct 22, 8:13 pm, Greg Miller <j...@google.com> wrote:
>
>
>
>
>
>
>
> > I saw some references online to a recent version of Ubuntu that changed the
> > location of some system libraries. It said that things moved to something
> > like /usr/lib/$(uname -m)-linux-gnu/ or something. I suggest running [find
> > /lib /usr/lib -name '*pthread*'] to find the location of your pthread
> > library. Then make sure that directory is included in your library search
> > path to g++ with the -L option. Maybe something like the following:
>
> > g++ -I../include -g -Wall -Wextra *-L/usr/lib/$(uname
> > -m)-linux-gnu*-lpthread sample1.o sample1_unittest.o gtest_main.a -o
> > sample1_unittest
>
> > It should be easy to add this flag to CXXFLAGS in the provided
> > Makefile<http://code.google.com/p/googletest/source/browse/trunk/make/Makefile>to
> > test. Sorry I don't have an Ubuntu machine to test this on.
>
> > G~
>

Greg Miller

unread,
Nov 21, 2011, 8:56:12 AM11/21/11
to briandkid, Google C++ Testing Framework
Thanks. Please file an issue at http://code.google.com/p/googletest/issues/list and include your OS version, compiler version, etc.

Greg
Reply all
Reply to author
Forward
0 new messages