Using non-system compiler and undefined references to pthread

On a Centos which has gcc 4.8.5 by default, I have built gcc-6.1.0 from source and want to tell pytorch to use that.

I have specified CC and CXX as below:

$ export CC=/storage/users/mnaderan/tools/gcc-6.1.0/bin/gcc
$ export CXX=/storage/users/mnaderan/tools/gcc-6.1.0/bin/g++

However, I get this error:

Performing C SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output:
Change Dir: /storage/users/mnaderan/pt/pytorch/build/CMakeFiles/CMakeTmp

Run Build Command(s):/bin/gmake cmTC_bceed/fast && gmake: Warning: File `Makefile' has modification time 19 s in the future
/bin/gmake -f CMakeFiles/cmTC_bceed.dir/build.make CMakeFiles/cmTC_bceed.dir/build
gmake[1]: Entering directory `/storage/users/mnaderan/pt/pytorch/build/CMakeFiles/CMakeTmp'
gmake[1]: Warning: File `CMakeFiles/cmTC_bceed.dir/flags.make' has modification time 19 s in the future
Building C object CMakeFiles/cmTC_bceed.dir/src.c.o
/storage/users/mnaderan/tools/gcc-6.1.0/bin/gcc   -DCMAKE_HAVE_LIBC_PTHREAD -fPIE   -o CMakeFiles/cmTC_bceed.dir/src.c.o   -c /storage/users/mnaderan/pt/pytorch/build/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_bceed
/storage/users/mnaderan/tools/cmake-3.15.4/bin/cmake -E cmake_link_script CMakeFiles/cmTC_bceed.dir/link.txt --verbose=1
/storage/users/mnaderan/tools/gcc-6.1.0/bin/gcc  -DCMAKE_HAVE_LIBC_PTHREAD   -rdynamic  CMakeFiles/cmTC_bceed.dir/src.c.o  -o cmTC_bceed
CMakeFiles/cmTC_bceed.dir/src.c.o: In function `main':
src.c:(.text+0x2f): undefined reference to `pthread_create'
src.c:(.text+0x3b): undefined reference to `pthread_detach'
src.c:(.text+0x4c): undefined reference to `pthread_join'
src.c:(.text+0x60): undefined reference to `pthread_atfork'
collect2: error: ld returned 1 exit status

Any idea?